import {ApiCall} from 'tsrpc';
import {HuoDongFun} from '../../../public/huodongfun';
import {christmas} from '../../../shared/protocols/event/christmas/PtlOpen';
import {PublicShared} from '../../../shared/public/public';
import {PayFun} from "../../../public/pay";

export class Yuandanfun {
    /**配置 */
    static async getCon(call: ApiCall) {
        return (await HuoDongFun.gethdList(call, 14))[0]
    }

    /**获取我的数据 */
    static async getData(call: ApiCall, hdid: number) {

        let data = await G.mongodb.cEvent(`yuandan${hdid}`).findOne({uid: call.uid, type: `yuandan${hdid}`})

        if (!data || !data.refreshTime || !PublicShared.chkSameDate(data.refreshTime, G.time)) {
            // 刷新每日任务
            data = (await G.mongodb.cEvent(`yuandan${hdid}`).findOneAndUpdate({uid: call.uid, type: `yuandan${hdid}`}, {
                $set: {
                    gameNum: 0,
                    gift: {},
                    exchange: {},
                    taskfinish: [],
                    taskval: await this.getTaskVal(call),
                    qiandaoTime: data?.qiandaoTime || 1,
                    refreshTime: G.time
                },
            }, {upsert: true, returnDocument: 'after'})).value
            this.refreshPayLog(call)
        }
        return data
    }

    static async refreshPayLog(call: ApiCall) {
        let _hd = await this.getCon(call)
        let payIds = _hd?.data?.gift?.filter(i => i.payId).map(i => i.payId)
        PayFun.delPayLog(call.uid, ...payIds.map(i => {
            return {payId: i, val: []}
        }))
    }

    /**获取所有taskid 及对应的值 */
    static async getTaskVal(call: ApiCall) {
        let _initCon = await this.getCon(call)
        let _tasks = _initCon.data.task
        let _res = {}
        for (let index = 0; index < Object.keys(_tasks).length; index++) {
            const element = Object.keys(_tasks)[index];
            let _tmp = _tasks[element]
            _tmp["id"] = element
            // 每日登录直接完成
            if (_tmp.stype == "128") {
                _res[element] = 1
            } else {
                _res[element] = 0
            }
        }
        return _res
    }

    /**设置数据 */
    static async setData(uid: string, hdid: number, set: {}) {
        await G.mongodb.cEvent(`yuandan${hdid}`).updateOne(
            {uid: uid, type: `yuandan${hdid}`},
            set
        )
    }

    /**设置任务 */
    static async setTaskVal(call: ApiCall, stype: number, val: number, chkCall: Function, chkval: number = 0, isinc: number = 0, alchangeVal: Function, arg) {
        // 活动过期，不计数
        let _hd = await this.getCon(call)
        if (!_hd) return

        await this.getData(call, _hd.hdid)

        let _tasks = _hd.data.task

        let _setData = {
            $inc: {},
            $set: {}
        }
        let isset = 0
        for (let indextask = 0; indextask < Object.keys(_tasks).length; indextask++) {
            const ele = Object.keys(_tasks)[indextask];
            // 具体任务配置
            let _taskCon = _tasks[ele]
            if (_taskCon.stype != stype) continue

            // 不符合任务要求
            if (!(await chkCall(_taskCon["cond"], chkval, arg))) continue

            // 根据需求改写
            val = await alchangeVal(call, _taskCon, val, arg)

            isset = 1
            if (isinc == 1) {    // 累加
                _setData["$inc"][`taskval.${ele}`] = val
            } else {
                _setData["$set"][`taskval.${ele}`] = val
            }
        }

        // 设置任务
        if (isset == 1) {
            await G.mongodb.collection('event').updateOne({
                uid: call.uid,
                type: `yuandan${_hd.hdid}`
            }, _setData)
        }
    }


}
