import { GM_MAIL_TYPE, MAIL_STATUS, SEND_NAME } from "../../consts";
import { ItemReward } from '../dbGeneral';
import { MailType } from "../../db/Mail";
import { GroupMailType } from "../../db/GroupMail";
import { ServerMailType } from '../../db/ServerMail';
import { gameData } from "../../pubUtils/data";

export class MailParam {
    id: string;                 // mongodb生成objectId
    mailType: GM_MAIL_TYPE;     // 邮件类型
    sendName: string = '系统';      // 发件人
    content: string = '';       // 内容
    title: string = '系统邮件';
    sendTime: number = 0;       // 发送时间
    endTime: number = 0;        // 过期时间
    goods: ItemReward[] = [];   // 奖励
    status: MAIL_STATUS = MAIL_STATUS.CREATE;        // 状态

    constructor(mailType: GM_MAIL_TYPE, mail: MailType|GroupMailType|ServerMailType, roleId?: string) {
        this.setMail(mailType, mail);
        if(mailType == GM_MAIL_TYPE.SINGLE) {
            this.setSingleMail(<MailType>mail, roleId);
        } else if (mailType == GM_MAIL_TYPE.GROUP) {
            this.setGroupMail(<GroupMailType>mail, roleId);
        } else if (mailType == GM_MAIL_TYPE.SERVER) {
            this.setServerMail(<ServerMailType>mail, roleId);
        }
    }

    setMail(mailType: GM_MAIL_TYPE, mail: MailType|GroupMailType|ServerMailType) {
        this.id = mail._id;
        this.mailType = mailType;
        this.sendTime = mail.sendTime;
        this.endTime = mail.endTime;
    }

    setSingleMail(mail: MailType, roleId?: string) {
        this.setContent(mail);
        if(roleId != undefined) {
            if(mail.roleId == roleId) {
                this.status = mail.status;
            } else {
                this.status = MAIL_STATUS.DELETE;
            }
        }
    }

    setGroupMail(mail: GroupMailType, roleId?: string) {
        this.setContent(mail);
        if(roleId != undefined) {
            let { roleStatus } = mail;
            let myRoleStatus = roleStatus.find(cur => cur.roleId == roleId);
            if(myRoleStatus) {
                this.status = myRoleStatus.status;
            } else {
                this.status = MAIL_STATUS.DELETE;
            }
        }
    }

    setServerMail(mail: ServerMailType, roleId?: string) {
        this.setContent(mail);
        if(roleId != undefined) {
            let { delRoles, roleStatus } = mail;
            if(delRoles.indexOf(roleId) == -1) {
                let myRoleStatus = roleStatus.find(cur => cur.roleId == roleId);
                if(myRoleStatus) {
                    this.status = myRoleStatus.status;
                } else {
                    this.status = MAIL_STATUS.CREATE;
                }
            } else {
                this.status = MAIL_STATUS.DELETE;
            }
        }
    }

    setContent(mail: MailType|GroupMailType|ServerMailType) {
        let { contentId = 0,  goods, sendName, content, title } = mail;
        let dicMail = gameData.mail.get(contentId);
        if(!dicMail) dicMail = gameData.mail.get(0);
        this.title = title;
        this.content = content;
        this.sendName = sendName||SEND_NAME;
        this.goods = goods;
    }
}