import { EmailFun } from '../email';
import { Rank } from '../rank/rank';
import { Scheduler, schedulerType } from './scheduler';


export class SchedulerClslCrossCtor extends Scheduler {
    id: schedulerType = 'clsl_cross_ctor';
    time = G.gc.clsl_com.divideTime;
    name = '丛林猎手赛季初始化';
    type: 'day' | 'week' = 'week';

    async read() {
        await this.ctorStartTime();
        this.isReady = false;
    }

    async start() {

        G.mongodb.collection('clslCrossUser').updateMany({}, { $set: { allStar: 0 } });
        Rank.list.clslCross.clear();

        await this.record();
    }
}

export class SchedulerClslLocalCtor extends SchedulerClslCrossCtor {
    id: schedulerType = 'clsl_local_ctor';

    async start() {

        G.mongodb.cPlayerInfo('clsl').updateMany({ type: 'clsl' }, { $set: { allStar: 0 } });

        await this.record();
    }
}

export class SchedulerClslPrize extends Scheduler {
    id: schedulerType = 'clsl_prize';
    time = G.gc.clsl_com.prizeTime;
    name = '丛林猎手赛季发奖';
    type: 'day' | 'week' = 'week';

    async read() {
        await this.ctorStartTime();
        this.isReady = false;
    }

    async start() {

        // 段位奖励
        let locals = await G.mongodb.cPlayerInfo('clsl').find({ type: 'clsl' }).toArray();
        G.gc.clsl_com.danPrize.forEach((conf) => {
            let sends = locals.filter(l => l.allStar >= conf.star[0]).map(s => s.uid);
            // locals.remove(l => sends.includes(l.uid));

            sends.forEach(uid => {
                EmailFun.addEmail({
                    uid: uid,
                    type: 'system',
                    title: G.gc.clsl_com.email_dan.title,
                    content: G.gc.clsl_com.email_dan.content,
                    prize: conf.prize,
                    contentInsertArr: [conf.star]
                });
            });
        })

        // 排名奖励
        let crossUids = (await G.clientCross.callApi('clsl/RankUids', {})).res.uids;
        G.gc.clsl_com.rankPrize.forEach((conf) => {
            let players = crossUids.slice(conf.rank[0] - 1, conf.rank[1]);
            players.forEach((uid, index) => {
                EmailFun.addEmail({
                    uid: uid,
                    type: 'system',
                    title: G.gc.clsl_com.email_rank.title,
                    content: G.gc.clsl_com.email_rank.content,
                    prize: conf.prize,
                    contentInsertArr: [conf.rank[0] + index]
                });
            });
        });

        await this.record();
    }
}