import { LoginMgr } from "../common/LoginManager";
import { AccountData } from "../data/AccountData";
import { Mail, MailData } from "../data/MailData";

export class MailManager {
    //邮件功能是否开放
    static isOpenMail() {
        return false;
    }

    //排序
    static orderMail(): Mail[] {
        let mails = MailData.getData()
        const mailList: Mail[] = [];
        const nowTime = AccountData.serverTime;

        for (const key in mails) {
            const v = mails[key];
            // if (parseInt(v.expire, 10) >= nowTime) {
            mailList.push(v);
            // }
        }

        mailList.sort((a, b) => {
            const x = parseInt(a.read, 10) === 0 ? 1 : 0;
            const y = parseInt(b.read, 10) === 0 ? 1 : 0;

            if (x > y) {
                return -1;
            } else if (x < y) {
                return 1;
            }
            return parseInt(a.expire, 10) > parseInt(b.expire, 10) ? -1 : 1;
        });

        return mailList;
    }

    //获取邮件剩余保留时间
    static getLeftDays(time: number): number {
        let leftTime = time - AccountData.serverTime;
        leftTime = Math.ceil(leftTime / (24 * 60 * 60));
        return leftTime <= 0 ? 0 : leftTime;
    }

    //是否有邮件未读
    static haveMailNotRead(): boolean {
        let mails = MailData.getData()
        const nowTime = AccountData.serverTime;
        for (const key in mails) {
            const v = mails[key];
            if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
                return true;
            }
        }
        return false;
    }

    //获取邮件附件（首个外部展示用）
    static getShowAward(id: string): any {
        let mails = MailData.getData()
        for (const key in mails) {
            const v = mails[key];
            if (parseInt(v.id, 10) === parseInt(id, 10)) {
                if (v.awards && v.awards.length > 0) {
                    if (mails[parseInt(v.id, 10)].read === '1') {
                        return null;
                    } else {
                        return v.awards[0];
                    }
                }
            }
        }
        return null;
    }

    //获取邮件文本
    static getMailText(content: any, isTitle: boolean): string {
        let jsonData = content;
        let text: string;

        // if (isTitle) {
        //     if (typeof jsonData === 'number') {
        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData.toString()].Title;
        //     } else {
        //         text = content;
        //     }
        //     return Util.getString(text);
        // } else {
        //     if (Array.isArray(jsonData)) {
        //         const count = jsonData.length;
        //         text = ConfData.getMapData('maillocaltext.dat')[jsonData[0].toString()].Desc;

        //         if (count === 2) {
        //             const tcConfNew = ConfData.getMapData('manorcity.dat');
        //             const tcsConfNew = ConfData.getMapData('manorschedule.dat');
        //             const tcConf = ConfData.getMapData('territorycity.dat');
        //             const tcsConf = ConfData.getMapData('territoryschedule.dat');

        //             if (parseInt(jsonData[0], 10) === 14) {
        //                 const str = ' Lv.' + tcConf[jsonData[1]].CityLevel + ' ' + Util.getString(tcConf[jsonData[1]].Name);
        //                 text = Util.getString(text).replace('%s', str);
        //             } else {

        //             }
        //         }

        // return text;
        // } else {
        return content;
        // }
        // }
    }

    //是否有附件未领
    static haveItemToGet(): boolean {
        let mails = MailData.getData()
        for (const key in mails) {
            const v = mails[key];
            if (v.awards && v.awards.length > 0) {
                return true;
            }
        }
        return false;
    }

    
    //消息-獲取郵件
    static sendGetMailMsg(args: { ids: {} }, callback) {
        LoginMgr.sendPost('user', 'get_mails', (data) => {
            console.log(data);
            MailData.setNewMail(data.get_mails)
            callback();
        }, args)
    }

    //消息-批量刪除
    static sendBatchDelMailMsg(args: { ids: any[] }, callback) {
        LoginMgr.sendPost('user', 'batch_del_mail', (data) => {
            console.log(data);
            MailData.removeMail(data.del_mails);
            callback();
        }, args)
    }

    //消息-批量領獎
    static sendBatchMaillAwardsMsg(args: { ids: any[] }, callback) {
        LoginMgr.sendPost('user', 'batch_mail_awards', (data) => {
            console.log(data);
            for (const key in data.read_mails) {
                if (Object.prototype.hasOwnProperty.call(data.read_mails, key)) {
                    const element = data.read_mails[key];
                    MailData.setMailReadById(key);
                    callback();
                }
            }
        }, args)
    }

    //消息-讀郵件
    static sendReadMailMsg(args: { id: string }, callback) {
        LoginMgr.sendPost('user', 'read_mail', (data) => {
            console.log(data);
            MailData.setMailReadById(args.id)
        }, args)
    }
}
