import {ApiCall} from "tsrpc";
import {PlayerFun} from '../../public/player';
import {XstaskFun} from '../../public/xstask';
import {ReqAllGet, ResAllGet} from "../../shared/protocols/xstask/PtlAllGet";
import {HongDianChange} from "../hongdian/fun";
import {PublicShared} from "../../shared/public/public";
import {TeQuanFun} from "../../public/tequan";

export default async function (call: ApiCall<ReqAllGet, ResAllGet>) {
    const taskList = JSON.parse(JSON.stringify(await XstaskFun.getAllTask(call.uid)));
    let taskInfo = await XstaskFun.getInfo(call.uid);
    let tequan = await TeQuanFun.getXsTaskNum(call);
    let prizeArr: atn[] = []
    for (let task of taskList) {
        const taskConf = G.gc.xstask[task?.taskId];
        if (!task) continue;
        if (task.receiveData == undefined) continue;
        if (task.receiveData.rec == true) continue;
        if (task.receiveData.time + taskConf.time > G.time) {
            // let second = task.receiveData.time + taskConf.time - G.time;
            // let needConf = G.gc.xstaskcom.jiasu.filter(v => second >= v.time[0] && second <= v.time[1])[0];
            // if (needConf) {
            //     await PlayerFun.checkNeedIsMeet(call, needConf.need);
            //     await PlayerFun.cutNeed(call, needConf.need);
            // }
            continue
        }
        let change: Parameters<typeof XstaskFun.changeInfo>[1] = {$inc: {}};
        change.$inc[`finishNum.${taskConf.colour}`] = 1;
        // 比对派遣时间，如果小于当日0点，则更新新任务，否则该变领取状态为true
        if (task.receiveData.time < PublicShared.getToDayZeroTime()) {
            // 更新任务
            let newTask = XstaskFun.randomTasks(taskInfo?.lv, 1)
            XstaskFun.updateTask(call.uid, task._id, newTask[0].taskId)
        } else {
            // 更新领取状态
            XstaskFun.finishTask(call.uid, task._id)
        }
        // XstaskFun.delTask(call.uid, task._id);
        XstaskFun.changeInfo(call.uid, change);
        HongDianChange.sendChangeKey(call.uid, ['xstaskhd', 'huodonghd']);
        if (!tequan) {
            prizeArr.push(...taskConf.prize)
        } else {
            for (let i = 0; i < 3; i++) {
                prizeArr.push(...taskConf.prize)
            }
        }
        // prizeArr.push(...taskConf.prize)
    }
    if (prizeArr.length > 0) await PlayerFun.sendPrize(call, prizeArr);
    call.succ({
        taskList: await XstaskFun.getAllTask(call.uid),
        prize: PublicShared.mergePrize(prizeArr)
    });
}