import { EmailFun } from '../email';
import { GHManage } from '../gonghui/manage';
import { Rank } from '../rank/rank';
import { RankSlzd1 } from '../rank/rank_slzd';
import { Scheduler } from './scheduler';
import { SlzdShared } from '../../shared/public/slzd';

export class SchedulerSlzdClean extends Scheduler {
    id = 'slzdClean';
    type = 'week';
    time = G.gc.slzd.schedulerGroup;
    name = '势力争夺清空数据';

    async read() {
        await this.ctorStartTime();
        this.isReady = false;
    }
    async start() {
        await G.mongodb.collection('slzdGh').deleteMany({});
        await G.mongodb.collection('slzdUser').deleteMany({});

        for (let i = 1; i <= 6; i++) {
            let rank = Rank.list['slzd' + i] as Rank;
            rank.clear();
        }

        await this.record();
    }
}

export class SchedulerSlzdPrize extends Scheduler {
    id = 'slzdPrize';
    type = 'week';
    time = G.gc.slzd.schedulerPrize;
    name = '势力争夺结算';

    async read() {
        await this.ctorStartTime();
        this.isReady = false;
    }
    async start() {
        // 获取数据库的特产库
        let stashObj:k_v<number> = await SlzdShared.getStash()
        for (let [fort, conf] of Object.entries(G.gc.slzd.forts)) {
            await this.sendPrizeByFort(fort, conf, stashObj[fort]);
        }
        // 结算完成，更新特产库,可异步
        await SlzdShared.updateStash()
        await this.record();
    }
    async sendPrizeByFort(fort: string, conf: typeof G.gc.slzd.forts[''], stashIndex: number) {
        // 特产库
        let stash = G.gc.slzd.stash[stashIndex]
        let rankList = await (Rank.list['slzd' + fort] as RankSlzd1).getRankListAll();
        // 据点奖励组合特产库奖励
        Object.keys(conf.prize).forEach( (key) => {
            conf.prize[key].prize.push(...stash.prize[key].prize)
        })
        
        // 对应排名分配奖励
        conf.prize.forEach(_conf => {
            // 计算获取奖励的排名
            let ghs = rankList.slice(_conf.ph[0] - 1, _conf.ph[1]);
            ghs.forEach((g, index) => {
                this.sendPrizeByGhId(g.player.ghId, _conf.prize, G.gc.slzd.email_prize.title, G.gc.slzd.email_prize.content, [G.gc.slzd.forts[fort].name, _conf.ph[0] + index]);
            });
        });

        let others = rankList.slice(conf.prize.last().ph[1]);
        if (others.length > 0) {
            let luck = others.filter(r => r.valArr[0] >= stash.luckPrize.need).random();
            if (luck) {
                this.sendPrizeByGhId(luck.player.ghId, stash.luckPrize.prize, G.gc.slzd.email_luck.title, G.gc.slzd.email_luck.content, [G.gc.slzd.forts[fort].name]);
                others.removeOne(o => o.player.ghId == luck.player.ghId);
            }

            let join = others.filter(o => o.valArr[0] >= conf.joinPrize.need);
            join.forEach(v => {
                this.sendPrizeByGhId(v.player.ghId, conf.joinPrize.prize, G.gc.slzd.email_join.title, G.gc.slzd.email_join.content, [G.gc.slzd.forts[fort].name]);
            });
        }
    }
    async sendPrizeByGhId(ghid: string, prize: atn[], title: string, content: string, args: any[]) {
        let gh = await GHManage.getGH(ghid);
        if (!gh) return;

        for (let role of gh.data.players) {

            let multiple: number;
            if (role.level == 1) multiple = 1.5;
            else if (role.level == 2) multiple = 1.4;
            else if (role.level == 3) multiple = 1.2;
            else multiple = 1;

            let p = prize.map(_p => {
                return {
                    ..._p,
                    n: Math.floor(_p.n * multiple)
                };
            });
            EmailFun.addEmail({
                uid: role.uid,
                type: 'system',
                title: title,
                content: content,
                prize: p,
                contentInsertArr: args
            });
        }
    }
}