import { _decorator} from 'cc';
import i18n from '../core/i18n/runtime-scripts/LanguageData';
import { GameConst } from '../core/common/GameConst';
import { audioMgr } from '../core/manager/AudioManager';
import { settingData } from '../user/SettingData';
import { Singleton } from '../core/manager/Singleton';

const { ccclass, property } = _decorator;

class PlatformBrowser extends Singleton{
    adInstance: any = null;
    gtag: any = null;
    openAd: boolean = true;

    public initialize(): void {
        this.initilizeWindow();
        this.adInstance = (window as any)['adInstance'];
        try {
            window["__woso"]?.["TopCallback"]?.();
        } catch (error) { 
        }
        console.log('web ads sdk初始化');
    }
    
    private initilizeWindow() {
        window["youxi"] = {
            SoundManager: {
                pauseAll: () => {
                    settingData.data.soundFx = false;
                    settingData.data.bgMusic = false;
                    settingData.saveToCache();
                    audioMgr.stop(GameConst.audios.bg_music);

                },
                resumeAll: () => {
                    settingData.data.soundFx = true;
                    settingData.data.bgMusic = true;
                    audioMgr.play(GameConst.audios.bg_music,true);
                    settingData.saveToCache();
                }
            },
            GameManager: {
                pauseAll: () => {
                    // console.log(`暂停游戏`);
                },
                resumeAll: () => {
                    // console.log(`开始游戏`);
                }
            },
            TargetedAds: {
                open: () => {
                    console.log("开启广告");
                },
                clos: () => {
                    console.log("关闭广告");
                }
            },
            Fps: {
                setfps: (value) => {
                    // GlobalMgr.instance.setfps(value);
                }
            }
        };
    }

    /**显示插屏广告*/
    public showInterstitialAd(cb?:Function): void {
        if(GameConst.isDebug){
            console.log('模拟显示插屏广告回调');
            cb?.();
            return
        }
        try {
            window['showAd']('interstitial').then(() => {
                //MsgHints.show(i18n('main.插屏广告加载成功'))
            }).catch(() => {
                //MsgHints.show(i18n('main.插屏广告加载失败'))
                // GtagMgr.inst.doGameDot(GtagType.ad_error);
            });
        } catch (error) {
            // tgxUITips.show('The ad failed to load')
        }
    }

    /** 显示激励广告*/
    public showRewardVideo(cb:Function): void {
        //1 成功 0 失败分享
        if(GameConst.isDebug){
            console.log('模拟激励广告回调');
            cb?.(1);
            return
        }
        try {
            window["showAd"]('rewarded')
                .then(() => {
                    //MsgHints.show(i18n('main.激励广告加载成功'))
                    if (cb) cb(1);
                })
                .catch(() => {
                    //MsgHints.show(i18n('main.激励广告加载失败'))
                    //上报信息
                    // GtagMgr.inst.doGameDot(GtagType.ad_error);
                    // cb && cb();
                })
                return
        } catch (error) {
        }
        //tgxUITips.show('The ads failed to load');
    }

    /** 显示激励广告*/
    public hideBannerAd(): void {
    }
    
    public showBannerAd(): void {
    }

    public vibrateShort(): void {
    }
}
export const platformBrowser = PlatformBrowser.ins();
