import { UserData } from "./UserData";

interface Mail {
    id: string;
    sys: number;
    expire: string;
    read: string;//读取状态
    awards: any;//奖励附件
}

class Data {
    private _mails: { [id: string]: Mail } = {};
    private _mailMaxIdTab: { [sys: number]: number } = { 0: 0, 1: 0 };

    init(): void {
        this.reset();
    }

    reset(): void {
        this._mails = {};
        this._mailMaxIdTab = { 0: 0, 1: 0 };
    }

    purge(): void {
        this.reset();
    }

    setData(data: Mail[]): void {
        if (data) {
            for (const v of data) {
                this._mails[v.id] = v;
            }
            for (const v of data) {
                if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
                    this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
                }
            }
        }
    }

    setMailMax(data: number[]): void {
        for (const v of data) {
            if (v > this._mailMaxIdTab[1]) {
                this._mailMaxIdTab[1] = v;
            }
        }
    }

    setNewMail(data: Mail[]): void {
        for (const v of data) {
            this._mails[v.id] = v;
        }
        for (const v of data) {
            if (parseInt(v.id, 10) > this._mailMaxIdTab[v.sys]) {
                this._mailMaxIdTab[v.sys] = parseInt(v.id, 10);
            }
        }
    }

    getData(): { [id: string]: Mail } {
        return this._mails;
    }

    getMaxMailId(type: number): number {
        return this._mailMaxIdTab[type];
    }

    orderMail(): Mail[] {
        const mailList: Mail[] = [];
        const nowTime = UserData.getServerTime();

        for (const key in this._mails) {
            const v = this._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;
    }

    getLeftDays(time: number): number {
        let leftTime = time - UserData.getServerTime();
        leftTime = Math.ceil(leftTime / (24 * 60 * 60));
        return leftTime <= 0 ? 0 : leftTime;
    }

    haveItemToGet(): boolean {
        for (const key in this._mails) {
            const v = this._mails[key];
            if (v.awards && v.awards.length > 0) {
                return true;
            }
        }
        return false;
    }

    setMailReadById(id: string): void {
        for (const key in this._mails) {
            const v = this._mails[key];
            if (parseInt(v.id, 10) === parseInt(id, 10)) {
                v.read = '1';
                break;
            }
        }
    }

    setMailRead(ids: string[]): void {
        for (const k1 of ids) {
            for (const key in this._mails) {
                const v = this._mails[key];
                if (parseInt(v.id, 10) === parseInt(k1, 10)) {
                    v.read = '1';
                }
            }
        }
    }

    haveMailNotRead(): boolean {
        const nowTime = UserData.getServerTime();
        for (const key in this._mails) {
            const v = this._mails[key];
            if (parseInt(v.expire, 10) >= nowTime && parseInt(v.read, 10) === 0) {
                return true;
            }
        }
        return false;
    }

    getShowAward(id: string): any {
        for (const key in this._mails) {
            const v = this._mails[key];
            if (parseInt(v.id, 10) === parseInt(id, 10)) {
                if (v.awards && v.awards.length > 0) {
                    if (this._mails[parseInt(v.id, 10)].read === '1') {
                        return null;
                    } else {
                        return v.awards[0];
                    }
                }
            }
        }
        return null;
    }

    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;
            // }
        // }
    }

    removeMail(mailList: string[]): void {
        for (const k1 of mailList) {
            for (const key in this._mails) {
                const v = this._mails[key];
                if (parseInt(v.id, 10) === parseInt(k1, 10)) {
                    delete this._mails[key];
                }
            }
        }
    }

    removeMailById(id: string): void {
        for (const key in this._mails) {
            const v = this._mails[key];
            if (parseInt(v.id, 10) === parseInt(id, 10)) {
                delete this._mails[key];
            }
        }
    }
}

export let MailData = new Data;