import {Scheduler, schedulerType} from "./scheduler";
import {EmailFun} from "../email";
import {e} from "mathjs";

export class CrossEmailPull extends Scheduler {
    id: schedulerType = "cross_email_pull";

    time = 60;  // 60s同步一次

    name = "跨服邮件同步"

    type = ""

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

    async start() {
        /**同步跨服邮件*/
        G.clientCross.callApi("email/GetCrossEmail", {server_id: G.config.serverId + ""}).then((res) => {
            if (!res.isSucc) return;
            res.res.emails.forEach(async (email) => {
                let _prize = R.clone(email?.prizeData?.prize) || [];
                if (email?.prizeData) delete email.prizeData;

                email["prize"] = _prize;
                if (email.uid != "system") {
                    await EmailFun.addEmail(email)
                } else {
                    await EmailFun.addQuanFuEmail(email)
                }
            })
            if (res.res.emails.length > 0) {
                G.clientCross.callApi("email/DelCrossEmail", {eids: res.res.emails.map(e => e._id)})
            }
        })
        await this.ctorStartTime()
    }

    get nextTime(): number {
        return G.time + this.time;
    }

    async ctorStartTime() {
        this.isStart = false;
        this.startTime = this.nextTime;
    }
}