import { ApiCall, ApiReturn } from 'tsrpc';
import { reissuePrize } from './api_s2c/event/kaifukuanghuan/ApiOpen';
import { qiRiDengLuAddDay } from './api_s2c/event/qiridenglu/ApiOpen';
import { addDay } from './api_s2c/event/yuedujijin/ApiOpen';
import { payZhanLing } from './api_s2c/event/zhanling/ApiOpen';
import { md_redPoint_check } from './api_s2c/gongyu/mingdao/ApiOpen';
import { ActionLog } from './public/actionLog/actionLog';
import { EmailFun } from './public/email';
import { GHManage } from './public/gonghui/manage';
import { PayFun } from './public/pay';
import { SignFun } from './public/sign';
import { TaskAllEmitFun, TaskFun } from './public/task';
import { XstaskFun } from './public/xstask';
import { ZhanLingTasks } from './public/zhanling';
import { player } from './shared/protocols/user/type';
import { PublicShared } from './shared/public/public';
import { setGud } from './public/gud';
import {checkResetBuyLog} from "./api_s2c/event/zhoumolibao/ApiOpen";
import {Christmasfun} from "./api_s2c/event/christmas/fun";
import {PushGiftFun} from "./public/pushgift";
import {LeiChongLiBaoBuyGift} from "./api_s2c/event/leichonglibao/ApiReceive";

export type gEventType = {
    /**玩家断开连接 */
    PLAYER_DISCONNECT: (uid: string) => void;
    /**玩家修改名字 */
    PLAYER_CHANGE_NAME: (gud: player, name: string) => void;
    /**玩家充值 */
    PLAYER_PAY: (gud: player, payId: string, type: 'user' | 'system', call) => void;
    /**玩家发生api调用 */
    API_CALL: (node: { call: ApiCall, return: ApiReturn<any>; }) => void;
    /**玩家每日首次登陆 */
    FIRST_LOGIN_EVERY_DAY: (gud: player, lastLoginTime: number, curLoginTime: number) => void;
    /**玩家消耗道具 */
    USE_ITEM: (gud: player, items: atn[]) => void;
    /**进行n次签到*/
    Class_task_100: (eventname, call, val, chkVal) => void;
    /**升级 n 次干部 */
    Class_task_101: (eventname, call, val, chkVal) => void;
    /**杂货店购买 n 次道具 */
    Class_task_102: (eventname, call, val, chkVal) => void;
    /**日常任务完成 n 次 */
    Class_task_999: (eventname, call, val, chkVal) => void;
    /**玩家等级升到 n 次 */
    Class_task_104: (eventname, call, val, chkVal) => void;
    /**成就任务完成 n 次 */
    Class_task_105: (eventname, call, val, chkVal) => void;
    /**玩家通关主线关卡 n 次 */
    Class_task_1: (eventname, call, val, chkVal) => void;
    /**干部等级达到 n 级 */
    Class_task_106: (eventname, call, val, chkVal) => void;
    /**同时出站 n 个干部 */
    Class_task_107: (eventname, call, val, chkVal) => void;
    /**n 个干部进阶配置k阶 */
    Class_task_108: (eventname, call, val, chkVal) => void;
    /**综合聊天频道聊天 n 次 */
    Class_task_109: (eventname, call, val, chkVal) => void;
    /**升级 n 次训练技能 */
    Class_task_110: (eventname, call, val, chkVal) => void;
    /**装备强化 n 次 */
    Class_task_111: (eventname, call, val, chkVal) => void;
    /**装备强化到 n 级 */
    Class_task_112: (eventname, call, val, chkVal) => void;
    /**有 n 件强化 配置 等级装备 */
    Class_task_113: (eventname, call, val, chkVal) => void;
    /**获得 n 个 配置颜色 干部 */
    Class_task_114: (eventname, call, val, chkVal) => void;
    /**获得 n 个 配置颜色 装备 */
    Class_task_115: (eventname, call, val, chkVal) => void;
    /**累计充值 n 钻石 */
    Class_task_116: (eventname, call, val, chkVal) => void;
    /**使用 n 次点金 */
    Class_task_117: (eventname, call, val, chkVal) => void;
    /**酒吧招募 n 次 */
    Class_task_118: (eventname, call, val, chkVal) => void;
    /**商店购买 n 次道具 */
    Class_task_119: (eventname, call, val, chkVal) => void;
    /**探险进行 n 次战斗 不管输赢 */
    Class_task_120: (eventname, call, val, chkVal) => void;
    /**势力捐献进行 n 次捐献 */
    Class_task_121: (eventname, call, val, chkVal) => void;
    /**进行 n 次快速探险 */
    Class_task_122: (eventname, call, val, chkVal) => void;
    /**竞技场中挑战 n 次对手 */
    Class_task_123: (eventname, call, val, chkVal) => void;
    /**斩草除根进行 n 次通关或扫荡 */
    Class_task_124: (eventname, call, val, chkVal) => void;
    /**挑战 n 次物资筹集 */
    Class_task_125: (eventname, call, val, chkVal) => void;
    /**地盘争夺抢夺或战领 n 次 */
    Class_task_126: (eventname, call, val, chkVal) => void;
    /**挑战 n 次清缴真主党 */
    Class_task_127: (eventname, call, val, chkVal) => void;
    /**竞技场首次达到x名 */
    Class_task_129: (eventname, call, val, chkVal) => void;
    /**在仓库使用 x 次交易10次(交易一次不算) */
    Class_task_130: (eventname, call, val, chkVal) => void;
    /**在仓库使用 x 次指定高级仓库 */
    Class_task_131: (eventname, call, val, chkVal) => void;
    /**进行 x 次公路狂飙 */
    Class_task_132: (eventname, call, val, chkVal) => void;
    /**进行 x 次教会招募 */
    Class_task_133: (eventname, call, val, chkVal) => void;
    /**消耗 x 个 配置道具 */
    Class_task_134: (eventname, call, val, chkVal, any) => void;
    /**累计登录天数 */
    Class_task_135: (eventname, call, val, chkVal) => void;
    /**修复 x 次名刀 */
    Class_task_136: (eventname, call, val, chkVal) => void;
    /**获得 x 个 配置颜色 配件 */
    Class_task_137: (eventname, call, val, chkVal, any) => void;
    /**配件升级 xx 次 */
    Class_task_138: (eventname, call, val, chkVal) => void;
    /**配件升级到 xx 级 */
    Class_task_139: (eventname, call, val, chkVal) => void;
    /**配置 配件升级到 xx 级（颜色限制） */
    Class_task_140: (eventname, call, val, chkVal) => void;
    // 在仓库交易x次
    Class_task_141: (eventname, call, val, chkVal) => void;
    /**势力 配置xx 捐献进行 n 次捐献 */
    Class_task_142: (eventname, call, val, chkVal) => void;
    /**穿戴X件以上装备 */
    Class_task_143: (eventname, call, val, chkVal) => void;
    /**进行x次好友申请或送礼 */
    Class_task_144: (eventname, call, val, chkVal) => void;
    /**领取x次挂机收益 */
    Class_task_145: (eventname, call, val, chkVal) => void;
    /**合成x次英雄 */
    Class_task_146: (eventname, call, val, chkVal) => void;
    /**合成x次装备 */
    Class_task_147: (eventname, call, val, chkVal) => void;
    /**合成x次英雄 */
    Class_task_148: (eventname, call, val, chkVal) => void;
    /**合成x次装备 */
    Class_task_149: (eventname, call, val, chkVal) => void;
    /**合成x次英雄 */
    Class_task_150: (eventname, call, val, chkVal) => void;
    /**合成x次装备 */
    Class_task_151: (eventname, call, val, chkVal) => void;
    /**合成x次装备 */
    Class_task_152: (eventname, call, val, chkVal) => void;
    /**合成x次装备 */
    Class_task_153: (eventname, call, val, chkVal) => void;
    /**参与圣诞活动小游戏 */
    Class_task_154: (eventname, call, val, chkVal) => void;
    /**每日任务宝箱领取 */
    Class_task_155: (eventname, call, val, chkVal) => void;
    /**每日钻石消耗 */
    Class_task_156: (eventname, call, val, chkVal) => void;
    /**每日获取vip经验 */
    Class_task_157: (eventname, call, val, chkVal) => void;
    /**今日参与{1}次抓娃娃小游戏 */
    Class_task_158: (eventname, call, val, chkVal) => void;
};

export function addListener() {
    // 注册任务相关监听
    TaskFun.addevent();

    G.on('PLAYER_DISCONNECT', uid => {

        //离线时删除该玩家的悬赏任务缓存
        if (XstaskFun.uidTask[uid]) {
            XstaskFun.uidTask[uid] = null;
            delete XstaskFun.uidTask[uid];
        }
        setGud(uid,{ logoutTime: G.time });
        G.mongodb.collection('user').updateOne({ uid: uid }, { $set: { logoutTime: G.time } });
    });

    G.on('PLAYER_CHANGE_NAME', async (player, name) => {

        //说明是公会会长 修改数据库里公会的会长名字
        if (player.ghId && player.ghLevel == 1) {
            (await GHManage.getGH(player.ghId)).updateCreateName(name);
        }
    });

    G.on('PLAYER_PAY', async (player, payId, payArgs,call) => {
        let conf: any = await PayFun.getConf(payId, payArgs);
        ActionLog.addDayLog(player.uid, { key: 'pay', val: conf.payExp[0].n });
        ActionLog.addRetainLog(player.uid, { key: 'pay', val: conf.payExp[0].n });

        if (payId == G.gc.zhanling.payId) payZhanLing(player);
        let payEmailConf = G.gc.payEmail[payId] as _gcType['payEmail']['caifutequan'];
        if (payEmailConf?.length > 0) {
            let sendEmailConfs = payEmailConf;
            sendEmailConfs.forEach(conf => {
                EmailFun.addEmail({
                    uid: player.uid,
                    type: 'system',
                    title: conf.title,
                    content: conf.content,
                    prize: conf.prize,
                });
            });
        }

        G.mongodb.collection('dayPay').updateOne(
            { uid: player.uid, time: PublicShared.getToDayZeroTime(G.time) },
            { $inc: { payNum: conf.money } },
            { upsert: true }
        );
        await PushGiftFun.buy(player.uid, payId)  // 推送礼包
        await LeiChongLiBaoBuyGift(call, payId)   // 累充礼包
    });

    G.on("FIRST_LOGIN_EVERY_DAY", (gud, lastTime, curTime) => {

        // 签到增加天数
        SignFun.updateLogin(gud.uid);
        ActionLog.initDayLog(gud.uid);
        ActionLog.addRetainLog(gud.uid, { key: 'login_day', val: 1 });
        ZhanLingTasks.clearLog(gud.uid, 'day');
        PayFun.checkGiftDayEmail(gud, lastTime, curTime);
        reissuePrize(gud, lastTime, curTime);
        addDay(gud);

        // 任务累计登录天数
        let call = PayFun.getCall(gud);
        G.emit("Class_task_135", 'Class_task_135', call, 1, 0);
        // 七日登录活动计数
        qiRiDengLuAddDay(call);
        // 周末礼包检测刷新
        checkResetBuyLog(call);

    });

    G.on('API_CALL', node => {

        if (node.return.isSucc && node.call.uid && node.call.service.name != 'user/Ping') {
            ActionLog.addDayLog(node.call.uid, { key: node.call.service.name, val: 1 });
            if (node.call.service.name == 'shop/Buy') {
                ActionLog.addDayLog(node.call.uid, { key: node.call.service.name + `/${node.call.req.shopId}`, val: 1 });
            }
        }
        //战令任务
        node.return.isSucc && ZhanLingTasks.addListener(node);
        // 所有任务监听
        TaskAllEmitFun.allEmit(node);
    });

    G.on('USE_ITEM', (gud, items) => {

        ActionLog.addDayLog(gud.uid, ...items.map(item => {
            if (item.t == 'rmbmoney') {
                ActionLog.addRetainLog(gud.uid, { key: `use_${item.a}_${item.t}`, val: item.n });
                ZhanLingTasks.addLog(gud.uid, 'day', `use_${item.a}_${item.t}`, item.n);
                ZhanLingTasks.addLog(gud.uid, 'round', `use_${item.a}_${item.t}`, item.n);
                md_redPoint_check(G.server.uid_connections[gud.uid], 'use_attr_rmbmoney');
            }

            return {
                key: `use_${item.a}_${item.t}`,
                val: item.n
            };
        }));
        let call = PayFun.getCall(gud);
        G.emit("Class_task_134", 'Class_task_134', call, 1, 0, items);
    });
}