// 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) => {
        smc.game.GameModel.isShowAd = true;
        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 } {
        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.onLoadFailed = 'ADHandler.inst.adInterstitialLoadFailed';
                param.callback.onClose = 'ADHandler.inst.adInterstitialClose';
                break;
            default:
                method = 'ad.reward';
                param.callback.onShow = 'ADHandler.inst.adRewardShow';
                param.callback.onClose = 'ADHandler.inst.adRewardClose';
                param.callback.onLoadFailed = 'ADHandler.inst.adRewardLoadFailed';
        }
        return { method, param };
    }

    //启屏广告关闭
    adSplashClose = async () => {
        smc.game.GameModel.isShowAd = false;
        if (oops.gui.has(UIID.KindTips)) oops.gui.remove(UIID.KindTips);
        if (oops.gui.has(UIID.Retention)) oops.gui.remove(UIID.Retention);

        const result = await LoginHandler.inst.savePrivacyStatus(true);
        if (result?.code === 0) {
            ServerHandler.inst.getAccountInfo();
        }
    };

    adSplashLoadFailed = () => {
        smc.game.GameModel.isShowAd = false;
        console.log('[广告] 启屏广告加载失败');
        ServerHandler.inst.getAccountInfo();
    };

    adInterstitialLoadFailed = () => {
        console.log('[广告] 插屏广告加载失败');
    };

    adInterstitialClose = (type: boolean) => {
        smc.game.GameModel.isShowAd = false;
        console.log('[广告] 插屏广告关闭', type);
    };

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

    adRewardLoadFailed = (str: string) => {
        smc.game.GameModel.isShowAd = false;
        console.log('[广告] 激励视频展示失败', str);
    };

    adRewardClose = (state: boolean) => {
        smc.game.GameModel.isShowAd = false;
        console.log("[广告] 激励视频关闭", state)
        smc.game.GameModel.isDone = state;
        if (state) {
            //发送金币通知--通关sign向服务器请求
            oops.message.dispatchEvent(GameEvent.showCoinAnimation);
        }
    };
}
window["ADHandler"] = ADHandler;