import { Widget, _decorator, Node, Sprite, Label, sys } from 'cc';
import { SkillType } from '../enum/SkillType';
import { SkillMgr } from '../manager/SkillMgr';
import { GameMgr } from '../manager/GameMgr';
import { UIBase } from '../scriptBase/UIBase';
import { CfgSkillInfo } from '../config/CfgSkillInfo';
import { ResMgr } from '../manager/ResMgr';
import { Bundle } from '../enum/Bundle';
import { AdMgr } from '../manager/AdMgr';
import { Debug } from "../util/Debug";


import ATRewardedVideoSDK from "../AnyThinkAds/ATRewardedVideoTSSDK"
import AAJS2 from "../utils/ATAndroidJS2";
import ATJSSDK from "../AnyThinkAds/ATJSSDK";
const Tag: string = 'zh:UIGetSkill';


const { ccclass, property, requireComponent } = _decorator;

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

    private icon: Sprite = null
    private lbTitle: Label = null
    private lbDesc: Label = null

    private skill: SkillType = null


    /**
     * 初始化广告
     */
    initAdForPage() {
        Debug.Log(Tag, 'initAdForPage foir UIGetSkill.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.icon = this.findComp('Icon', Sprite)
        this.lbTitle = this.findComp('LbTitle', Label)
        this.lbDesc = this.findComp('LbDesc', Label)
        this.initAdForPage()
    }

    public onOpen(data?: unknown): void {
        GameMgr.Pause = true
        this.skill = data as SkillType
        const skillInfo = CfgSkillInfo[this.skill]
        this.lbTitle.string = skillInfo.name
        this.lbDesc.string = skillInfo.desc
        this.icon.spriteFrame = ResMgr.getSpriteFrame(Bundle.Icon, skillInfo.icon)
    }

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

    protected onBtnGetClick(): void {
        Debug.Log(Tag, '点击了获取xxxx道具', this.skill)
        switch (this.skill) {
            case SkillType.DoubleCoin:
                Debug.Log(Tag, '星星')
                Debug.Log(Tag, SkillType.DoubleCoin)
                break;
            case SkillType.FreezeTime:
                Debug.Log(Tag, '冰冻时间')
                Debug.Log(Tag, SkillType.FreezeTime)
                break;
            case SkillType.RefreshPosition:
                Debug.Log(Tag, '转换')
                Debug.Log(Tag, SkillType.RefreshPosition)
                break;
            case SkillType.EraseGroup:
                Debug.Log(Tag, '灯泡')
                Debug.Log(Tag, SkillType.EraseGroup)
                break;
            default:
                Debug.Log(Tag, 'onBtnGetClick  未知skill。。。。')
                break;
        }

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

                AdMgr.showRewardedVideo(() => {
                    SkillMgr.addSkill(this.skill)
                    this.close()
                })


            } else {
                Debug.Log(Tag, 'onBtnGetClick ad  no ok')

                AdMgr.showRewardedVideo(() => {
                    SkillMgr.addSkill(this.skill)
                    this.close()
                })
            }
        }else{
            Debug.Log(Tag, 'onBtnGetClick H5直接发奖励')
            AdMgr.showRewardedVideo(() => {
                SkillMgr.addSkill(this.skill)
                this.close()
            })
        }


    }

}


