/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-04-11 10:14:44
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-30 17:05:37
 * @Description: 
 */
// ADHandler.ts
import { CocosHandlerType, CocosHandler } from './CocosHandler';
import { AD_TYPE } from '../config/GameDefine';
import { oops } from 'db://oops-framework/core/Oops';
import { UIID } from '../config/GameUIConfig';
import { smc } from '../SingletonModuleComp';
import { ServerHandler } from './ServerHandler';
import { LoginHandler } from './LoginHandler';
import { GameEvent } from '../config/GameEvent';

export class ADHandler {
    private static _inst: ADHandler;

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

    showAd = async (id: string) => {
        const { method, param } = this.buildAdParam(id);
        const data: CocosHandlerType = {
            method,
            param: JSON.stringify(param)
        };
        return await CocosHandler.inst.sendMessageToAndroid(data, `广告 ${id}`);
    };

    private buildAdParam(id: string): { method: string, param: any } {
        console.log("播放的广告ID>>>>>>>>>>>>>", id)
        const param: any = {
            funcId: id,
            callback: {
                onLoaded: '',
                onShow: '',
                onLoadFailed: '',
                onShowFailed: '',
                onClose: ''
            }
        };
        let method = '';

        switch (id) {
            case AD_TYPE.Start:
                method = 'ad.splash';
                param.callback.onLoadFailed = 'ADHandler.inst.adSplashLoadFailed';
                param.callback.onClose = 'ADHandler.inst.adSplashClose';
                break;
            case AD_TYPE.Jion_Main:
            case AD_TYPE.Double_Close:
            case AD_TYPE.Rebates:
            case AD_TYPE.Double_Speed_Close:
                method = 'ad.interstitial';
                param.callback.onShow = 'ADHandler.inst.adInterstitialonShow'
                param.callback.onLoadFailed = 'ADHandler.inst.adInterstitialLoadFailed';
                param.callback.onClose = 'ADHandler.inst.adInterstitialClose';
                break;
            default: //激励视频
                oops.gui.waitOpen();
                // oops.message.dispatchEvent(GameEvent.updateGameState, "paused");
                method = 'ad.reward';
                param.callback.onShow = 'ADHandler.inst.adRewardShow';
                param.callback.onClose = 'ADHandler.inst.adRewardClose';
                param.callback.onLoadFailed = 'ADHandler.inst.adRewardLoadFailed';
                break;
        }
        return { method, param };
    }

    //启屏广告关闭P
    adSplashClose = async () => {
        console.log("启屏广告关闭")
        if (oops.gui.has(UIID.KindTips)) oops.gui.remove(UIID.KindTips);
        if (oops.gui.has(UIID.Retention)) oops.gui.remove(UIID.Retention);
        ServerHandler.inst.getAccountInfo();
    };

    adSplashLoadFailed = () => {
        oops.gui.waitClose();
        console.log('[广告] 启屏广告加载失败');
        ServerHandler.inst.getAccountInfo();
        //加载实名认证
    };

    //插屏广告展示的时候也要暂停
    adInterstitialonShow = () => {
        oops.message.dispatchEvent(GameEvent.pauseGame);
    }

    adInterstitialLoadFailed = () => {
        console.log('[广告] 插屏广告加载失败');
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        oops.message.dispatchEvent(GameEvent.resumeGame);

    };

    adInterstitialClose = (type: boolean) => {
        console.log('[广告] 插屏广告关闭', type);
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        oops.message.dispatchEvent(GameEvent.resumeGame);
    };

    adRewardShow = (str: string) => {
        const data = JSON.parse(str);
        ServerHandler.inst.getSign(data.price);
        oops.message.dispatchEvent(GameEvent.pauseGame);
    };

    adRewardLoadFailed = (str: string) => {
        oops.gui.waitClose();
        const viewType = smc.game.GameModel.viewType;
        // 重置视图类型
        const resetViewType = () => {
            smc.game.GameModel.viewType = "";
        };
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        oops.message.dispatchEvent(GameEvent.resumeGame);
        // 广告未完成观看
        switch (viewType) {
            case "pass_reward":
                resetViewType();
                ServerHandler.inst.getNextLevel();
                break;
            case "revive_reward":
                resetViewType();
                ServerHandler.inst.RestartGame();
                break;
            case "double_reward":
                ServerHandler.inst.getLittleRewards();
                resetViewType();
                break;
            case "speed_reward":   //加速奖励
                oops.gui.toast("跳过广告，翻倍失败~")
                break;
        }
    };

    adRewardClose = (state: boolean) => {
        oops.gui.waitClose();
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        oops.message.dispatchEvent(GameEvent.resumeGame);
        const viewType = smc.game.GameModel.viewType;
        console.log("viewType>>>>>>>>>>>", viewType);
        // 重置视图类型
        const resetViewType = () => {
            smc.game.GameModel.viewType = "";
        };
        if (state) {
            // 广告观看完成，发放奖励
            switch (viewType) {
                case "double_reward":  //翻倍奖励
                    ServerHandler.inst.getVideorReward();
                    resetViewType();
                    break;
                case "pass_reward":    //通关奖励
                    ServerHandler.inst.getVideorReward();
                    resetViewType();
                    ServerHandler.inst.getNextLevel();
                    break;
                case "speed_reward":   //加速奖励
                    oops.message.dispatchEvent(GameEvent.DoubleSpeedOpenSuccess);
                    ServerHandler.inst.getVideorReward();
                    resetViewType();
                    break;
                case "revive_reward":  //复活奖励
                    ServerHandler.inst.getVideorReward();
                    resetViewType();
                    oops.message.dispatchEvent(GameEvent.RestartGame);
                    break;
            }
        } else {
            // 广告未完成观看
            switch (viewType) {
                case "pass_reward":
                    resetViewType();
                    ServerHandler.inst.getNextLevel();
                    break;
                case "revive_reward":
                    resetViewType();
                    ServerHandler.inst.RestartGame();
                    break;
                case "double_reward":
                    ServerHandler.inst.getLittleRewards();
                    resetViewType();
                    break;
                case "speed_reward":   //加速奖励
                    oops.gui.toast("跳过广告，翻倍失败~")
                    break;
            }
        }
    };
}
// @ts-ignore
window["ADHandler"] = ADHandler;