
type KeysEnum<T> = { [P in keyof Required<T>]: true };
import { RewardInter } from '../interface';
import { GMMailModel } from '../../db/GMMail';
const _ = require('lodash');

export interface GMMail {
    readonly id: string;
    readonly sendRoles?: Array<{roleId: string, status: number}>;
    readonly goods: Array<RewardInter>;
    readonly sendTime: number;
    readonly endTime: number;
    readonly content: string;
    readonly gmMailType: number;
    readonly updatedAt: number;
    readonly serverId: number;
    readonly sendName: string;
}

const GMMailKeys: KeysEnum<GMMail> = {
    id: true,
    sendRoles: true,
    goods: true,
    sendTime: true,
    endTime: true,
    content: true,
    gmMailType: true,
    updatedAt: true,
    serverId: true,
    sendName: true
};

export async function mailInit() {
    let gmMail = new Map<number, any>();
    let mails = await GMMailModel.getMails();
    mails.map((o:any)=>{
        o.id = o._id.toString();
        o.updatedAt = Math.floor(o.updatedAt.getTime()/1000);
        let mail = gmMail.get(o.serverId);
        if (!mail)
            mail = new Map<string, GMMail>();
        o.sendName = o.sendName||'系统';
        mail.set(o.id, _.pick(o, Object.keys(GMMailKeys)));
        gmMail.set(o.serverId, mail);
    });
    return gmMail;
}


export function setMails(mails:GMMail[], gmMail) {
    mails.map((o:any)=>{
        o.id = o._id.toString();
        o.updatedAt = Math.floor(o.updatedAt.getTime()/1000);
        o.sendName = o.sendName||'系统';
        let mail = gmMail.get(o.serverId);
        if (!mail)
            mail = new Map<string, GMMail>();
        mail.set(o.id, _.pick(o, Object.keys(GMMailKeys)));
        gmMail.set(o.serverId, mail);
    });
}
