/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-04-11 10:16:41
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-11 15:49:20
 * @Description: 
 */
// ServerHandler.ts
import { smc } from '../SingletonModuleComp';
import { GameEvent } from '../config/GameEvent';
import { oops } from 'db://oops-framework/core/Oops';
import { CocosHandler, CocosHandlerType } from './CocosHandler';
import { ProtocolEvent } from './ProtocolEvent';

export class ServerHandler {
    private static _inst: ServerHandler;

    public static get inst(): ServerHandler {
        if (!this._inst) {
            this._inst = new ServerHandler();
        }
        return this._inst;
    }


    private buildCallback(success: string, fail?: string) {
        const callback: any = { onSuccess: success };
        if (fail) callback.onFail = fail;
        return callback;
    }

    async sendMsgToServer(param: any) {
        const data: CocosHandlerType = {
            method: 'request.post',
            param: JSON.stringify(param)
        };
        return await CocosHandler.inst.sendMessageToAndroid(data, '服务器请求');
    }

    //微信登录
    wxLogin(code: string) {
        const param = {
            url: ProtocolEvent.WechatLogin,
            param: {
                code: code
            },
            callback: this.buildCallback('ServerHandler.inst.onWxLoginInfo', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onWxLoginInfo(str: string) {
        console.log('[服务器] 微信登录返回', str);

    }


    getAccountInfo() {
        const param = {
            url: ProtocolEvent.AccountInfo,
            callback: this.buildCallback('ServerHandler.inst.onAccountInfo', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onAccountInfo(str: string) {
        const data = JSON.parse(str);
        const account = smc.account.AccountModel;
        const game = smc.game.GameModel;
        account.uid = data.uid;
        account.accountName = data.nickname;
        account.headUrl = data.headImgUrl;
        account.isLogined = data.isBand;
        account.curLevel = data.currentLevelInfo.level;
        game.curScore = data.currentLevelInfo.score;
        game.targetScore = data.currentLevelConf.score;
        game.curLevelConfig = data.currentLevelConf;
        game.costInfo = data.handlingChargeConf;
        oops.message.dispatchEvent(GameEvent.UserLogin);
    }

    getDailyReward(level: number) {
        const param = {
            url: ProtocolEvent.GetDailyReward,
            param: { level },
            callback: this.buildCallback('ServerHandler.inst.onDailyReward', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onDailyReward(str: string) {
        console.log('[服务器] 每日奖励返回', str);
    }

    //红包币-获取记录
    getDailyTaskReward() {
        this.getUserItemInfo();
        const param = {
            url: ProtocolEvent.GetDailyTask,
            callback: this.buildCallback('ServerHandler.inst.onDailyTaskReward', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onDailyTaskReward(str: string) {
        console.log('[服务器] 红包任务数据返回', str);
        oops.message.dispatchEvent(GameEvent.openView, 'openRedBagView');
    }

    getUserItemInfo() {
        const param = {
            url: ProtocolEvent.UserItemInfo,
            callback: this.buildCallback('ServerHandler.inst.onUserItemInfo', 'ServerHandler.instonRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onUserItemInfo(str: string) {
        const result = JSON.parse(str);
        const props = result.data.props;
        const account = smc.account.AccountModel;
        const game = smc.game.GameModel;
        account.gameCoin = props['1005'];
        account.cashCoin = props['1004'];
        account.goldCoin = props['1006'];
        game.handlingCharge = props['1009'];
    }

    //获取提现记录
    getRecordList() {
        const param = {
            url: ProtocolEvent.GetWithdrawRecord,
            param: {
                offset: 0,
                limit: 50
            },
            callback: this.buildCallback('ServerHandler.inst.onRecordList', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    //提现列表
    onRecordList(str: string) {
        const result = JSON.parse(str);
        if (result.count > 0) {
            smc.game.GameModel.recordList = result.data.list;
        }
        oops.message.dispatchEvent(GameEvent.openRecordView);
    }

    getGameAwardInfo() {
        const param = {
            url: ProtocolEvent.getGameAward,
            callback: this.buildCallback('ServerHandler.inst.onGameAwardInfo', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onGameAwardInfo(str: string) {
        const result = JSON.parse(str);
        smc.game.GameModel.passViewInfo = result;
        oops.message.dispatchEvent(GameEvent.openView, 'openPassView');
    }

    getTxbfInfo() {
        const param = {
            url: ProtocolEvent.GetWithdrawReward,
            callback: this.buildCallback('ServerHandler.inst.onRebates', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    //提现返利
    onRebates(str: string) {
        console.log('[服务器] 提现返利信息', str);
        let result = JSON.parse(str);
        smc.game.GameModel.cashNum = result.props["1004"] / 100;
        oops.message.dispatchEvent(GameEvent.openView, "openRebateView")
    }

    //获取双倍奖励返回
    getDoubleSurprise() {
        const param = {
            url: ProtocolEvent.getDoubleAwardInfo,
            callback: this.buildCallback('ServerHandler.inst.onDoubleSurprise', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    //双倍惊喜返回
    onDoubleSurprise(str: string) {
        console.log('[服务器] 恭喜翻倍信息', str);
        oops.message.dispatchEvent(GameEvent.openView, "openDoubleSurprise");
    }

    //更新消除奖励
    updateEliminationReward(data: { level: number, score: number, count: number }) {
        const param = {
            url: ProtocolEvent.GetEliminationReward,
            param: {
                eliminationCount: data.count,
                level: data.level,
                score: data.score
            },
            callback: this.buildCallback('ServerHandler.inst.onEliminationSuccess', 'ServerHandler.inst.onRequestFail')
        };
        this.sendMsgToServer(param);
    }

    onEliminationSuccess(str: string) {
        console.log('[服务器] 消除成功返回', str);
    }

    getSign(price: number) {
        const param = {
            url: ProtocolEvent.AdVideoStart,
            param: { ecpm: price },
            callback: this.buildCallback('ServerHandler.inst.onSign')
        };
        this.sendMsgToServer(param);
    }

    onSign(str: string) {
        console.log('[服务器] 签名返回', str);
        let result = JSON.parse(str);
        smc.game.GameModel.sign = result?.sign || "";
    }

    //直接领取通关奖励
    getPassRewards() {
        const level = smc.account.AccountModel.curLevel;
        const param = {
            url: ProtocolEvent.GetPassReward,
            param: {
                level: level,
            },
            callback: this.buildCallback('ServerHandler.inst.onGetPassRewards', 'ServerHandler.inst.onRequestFail')
        }
        this.sendMsgToServer(param);
    }
    //直接领取通关奖励返回
    onGetPassRewards(str: string) {
        console.log('[服务器] 直接领取通关奖励成功返回', str);
        oops.message.dispatchEvent(GameEvent.showCoinAnimation);
    }






    //少量领取翻倍奖励
    getLittleRewards() {
        const level = smc.account.AccountModel.curLevel;
        const param = {
            url: ProtocolEvent.GetLittlePassReward,
            param: {
                level: level,
            },
            callback: this.buildCallback('ServerHandler.inst.onGetDoubleRewards', 'ServerHandler.inst.onRequestFail')
        }
        this.sendMsgToServer(param);
    }

    //少量领取翻倍奖励返回
    onGetDoubleRewards(str: string) {
        console.log('[服务器] 直接领取通关奖励成功返回', str);
        oops.message.dispatchEvent(GameEvent.showCoinAnimation);
    }



    onRequestFail(code: number, str: string) {
        console.log('[服务器] 请求失败', code, str);
    }
}

window["ServerHandler"] = ServerHandler;
