import { Scheduler, schedulerType } from './scheduler';
import { PublicShared } from "../../shared/public/public";
import { Wjjl } from "../../module/collection_wjjl";
import { ZhanLingTasks } from "../zhanling";
import { setGud } from '../gud';
import { EmailFun } from '../email';


export class SchedulerNewDayLocalCtor extends Scheduler {
    id: schedulerType = 'newDay_local_ctor';
    time = 0;
    name = '定时推送跨天通知';
    type: 'day' | 'week' = 'day';

    // todo 测试
    // get nextTime() {
    //     return G.time + 60;
    // }

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

    async start() {
        let users = Object.values(G.server.uid_connections)
        if (users.length) {
            for (let item of users) {
                let gud = item.gud
                let add: k_v<any> = {};
                //每天首次登陆
                if (!gud.loginTime || PublicShared.getToDayZeroTime(gud.loginTime) != PublicShared.getToDayZeroTime(G.time)) {
                    gud.loginDays = gud.loginDays ? gud.loginDays + 1 : 1;
                    add.loginDays = gud.loginDays;
                    Wjjl.setVal(gud.uid, `toDayLogin_${PublicShared.getOpenServerDay(gud.cTime, G.time)}`, 1);

                    if (gud.logoutTime && PublicShared.getToWeekMondayZeroTime(gud.logoutTime) < PublicShared.getToWeekMondayZeroTime(G.time)) {
                        ZhanLingTasks.clearLog(gud.uid, 'week');
                    }
                    G.emit('FIRST_LOGIN_EVERY_DAY', gud, gud.loginTime, G.time);
                }
                gud.loginTime = G.time;
                add.loginTime = G.time;

                if (Object.keys(add).length > 0) {
                    G.mongodb.collection('user').updateOne({ bindUid: gud.bindUid, sid: gud.sid }, {
                        $set: {
                            ...add
                        }
                    });
                    setGud(gud.uid, add);
                }
                G.server.sendMsgByUid(gud.uid, 'msg_s2c/PlayerChange', gud)

            }
            G.server.broadcastClusterMsg('msg_s2c/NewDay', { time: this.zeroTime });
        }

        (async () => {
            // 每周一，发放终身卡
            if (PublicShared.getWeek(G.time) != 1) {
                console.log("不是周一，不发放终身卡", PublicShared.getWeek(G.time));
                return
            };

            let logs = await G.mongodb.collection("payLogNew").find(
                { key: "zhongshenka", del_time: { $exists: false } }, { projection: { _id: 0, } }
            ).toArray();
            
            if (logs.length == 0) {
                console.log("没有终身卡数据，不发放终身卡");
            }
            let con = G.gc.payEmail.zhongshenka.filter(e => e.day == 7)[0];
            for (let i = 0; i < logs.length; i++) {
                console.log("发放终身卡", logs[i].uid);
                // 发送邮件
                EmailFun.addEmail({
                    uid: logs[i].uid,
                    type: "system",
                    title: con.title,
                    content: con.content,
                    contentInsertArr: [],
                    createTime: G.time,
                    prize:con.prize,
                })
            }
        })()
    }
}
