import { Widget, _decorator, Node, sys } from 'cc';
import { UIBase } from '../scriptBase/UIBase';
import { EventMgr } from '../manager/EventMgr';
import { EventType } from '../enum/EventType';
import { TransMgr } from '../manager/TransMgr';
import { GameMgr } from '../manager/GameMgr';
import { UI } from '../enum/UI';
import { AudioMgr } from '../manager/AudioMgr';
import { ModeName } from '../enum/Mode';
import { AdMgr } from '../manager/AdMgr';

import ATRewardedVideoSDK from "../AnyThinkAds/ATRewardedVideoTSSDK"
import AAJS2 from "../utils/ATAndroidJS2";
import ATJSSDK from "../AnyThinkAds/ATJSSDK";

import { Debug } from "../util/Debug";
const Tag: string = 'zh:UITimeOut';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UITimeOut')
@requireComponent(Widget)
export class UITimeOut extends UIBase {

    /**
     * 初始化广告
     */
    initAdForPage() {
        Debug.Log(Tag, 'initAdForPage foir UITimeOut.ts')
        if (sys.os === sys.OS.ANDROID) {
            let deviceId = AAJS2.getDeviceUserId();
            Debug.Log(Tag, 'checkAdStatus =' + ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
            var setting = {};
            setting[ATRewardedVideoSDK.userIdKey] = deviceId;
            ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
        }
    }
    protected onLoad(): void {
        this.initAdForPage()
    }


    public onOpen(data?: any): void {
        GameMgr.Pause = true
        AudioMgr.playSfx('通关失败')
    }

    public onClose(data?: any): void {
        GameMgr.Pause = false
    }

    protected async onBtnCloseClick() {
        await TransMgr.Inst.fadeIn()
        this.close()
        GameMgr.quitGame()
        this.open(UI.Main)
        TransMgr.Inst.fadeOut()
    }

    protected onBtnYesClick(): void {
        if (sys.os == sys.OS.ANDROID) {
            if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
                Debug.Log(Tag, 'onBtnGetClick ad  ok')
                //  sys.localStorage.setItem('yxAdMark', 'xxx');//看广告用于 xx 标记
                ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());

                AdMgr.showRewardedVideo(() => {
                    this.close()
                    EventMgr.emit(EventType.Revive)
                })

            } else {
                Debug.Log(Tag, 'onBtnYesClick ad  no ok,直接发奖励')

                AdMgr.showRewardedVideo(() => {
                    this.close()
                    EventMgr.emit(EventType.Revive)
                })
            }
        } else {
            Debug.Log(Tag, 'onBtnYesClick H5直接发奖励')
            AdMgr.showRewardedVideo(() => {
                this.close()
                EventMgr.emit(EventType.Revive)
            })
        }
    }
    
    


}


