import { apply } from '../../api_s2c/kbzz/ApiOpen';
import { CollectionKbzzGroupTroop, CollectionKbzzGroupUser } from '../../module/collection_kbzz';
import { PublicShared } from '../../shared/public/public';
import { RankKbzz } from '../rank/rank_kbzz';
import { Scheduler } from './scheduler';


class SchedulerKbzz extends Scheduler {
    id = '';
    type = 'week';
    name: string;
    async read() {
        await this.ctorStartTime();
        this.isReady = false;
    }
    async start() {

    }
}

export class SchedulerKbzzAutoApply extends SchedulerKbzz {
    id = 'kbzzAutoApply';
    name = '恐怖战争自动报名';
    time = G.gc.kbzz.time.apply[0];

    async start() {
        let db = G.mongodb.cPlayerInfo('kbzzApply');
        let zeroTime = PublicShared.getToWeekMondayZeroTime();
        let roles = await db.find({ type: 'kbzzApply' }).toArray();

        let lastApplyRoles = roles.filter(r => r.applyTime && r.applyTime < zeroTime);
        lastApplyRoles.length > 0 && await db.updateMany({ uid: { $in: lastApplyRoles.map(r => r.uid) }, type: 'kbzzApply' }, { $set: { apply: false } });

        let autoApplyRoles = roles.filter(r => r.autoApply == true);
        autoApplyRoles.length > 0 && autoApplyRoles.forEach(r => apply(r.uid));

        await this.record();
    }
}

export class SchedulerKbzzGroup extends SchedulerKbzz {
    id = 'kbzzGroup';
    name = '恐怖战争分组';
    time = G.gc.kbzz.time.applyEnd[0];

    async start() {
        let groupNum = G.gc.kbzz.groupNum;
        let roles = await G.mongodb.collection('kbzzApplyUser').find({}).toArray();

        if (roles.length >= groupNum) {
            await G.mongodb.collection('kbzzGroupUser').deleteMany({});
            await G.mongodb.collection('kbzzGroupTroop').deleteMany({});
            await G.mongodb.collection('kbzzGroupLog').deleteMany({});

            let groupArr: Array<typeof roles[0]>[] = [];
            let insertRoles: CollectionKbzzGroupUser[] = [];
            let insertGroupTroop: CollectionKbzzGroupTroop[] = [];
            roles.sort((a, b) => a.info.player.cTime - b.info.player.cTime);

            while (roles.length > 0) {
                let arr = roles.slice(0, groupNum);
                roles.splice(0, groupNum);
                if (roles.length < groupNum) {
                    arr.push(...roles);
                    roles = [];
                }
                groupArr.push(arr);
            }


            groupArr.forEach((roles, groupIndex) => {
                roles.sort((a, b) => b.info.player.power - a.info.player.power);
                roles.forEach((role, index) => {
                    insertRoles.push({
                        uid: role.uid,
                        info: role.info,
                        group: groupIndex,
                        troop: index % G.gc.kbzz.troop.length,
                        score: G.gc.kbzz.playerScore
                    });
                });
                insertGroupTroop.push(...G.gc.kbzz.troop.map((v, i) => {
                    return {
                        group: groupIndex,
                        troop: i,
                        score: G.gc.kbzz.troopScore
                    };
                }));
            });

            await G.mongodb.collection('kbzzGroupUser').insertMany(insertRoles);
            await G.mongodb.collection('kbzzGroupTroop').insertMany(insertGroupTroop);
            await G.mongodb.collection('kbzzApplyUser').deleteMany({});
            RankKbzz.init();
        }

        await this.record();
    }
}