import { Widget, _decorator, Node, Sprite, Label } 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 { AdvertMgr } from '../manager/AdvertMgr';
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

    protected onLoad(): void {
        this.icon = this.findComp('Icon', Sprite)
        this.lbTitle = this.findComp('LbTitle', Label)
        this.lbDesc = this.findComp('LbDesc', Label)
    }

    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 {
        // AdMgr.showRewardedVideo(() => {
        //     SkillMgr.addSkill(this.skill)
        //     this.close()
        // })

        if (AdvertMgr.instance.openAd) {
            AdvertMgr.instance.showReawardVideo(() => {
                SkillMgr.addSkill(this.skill);
                this.close();
            })
        } else {
            SkillMgr.addSkill(this.skill);
            this.close();
        }
    }

}


