import { Widget, _decorator, Node, Sprite, Label, SpriteFrame } 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 skill0Bg: Node = null
    private skill1Bg: Node = null
    private skill2Bg: Node = null
    private icon: Sprite = null
    private lbTitle: Label = null
    private lbDesc: Label = null
    private btnGet: Sprite = null

    private skill: SkillType = null

    protected onLoad(): void {
        this.skill0Bg = this.findNode('skill0_bg')
        this.skill1Bg = this.findNode('skill1_bg')
        this.skill2Bg = this.findNode('skill2_bg')
        this.icon = this.findComp('Icon', Sprite)
        this.btnGet = this.findComp('BtnGet', 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)

        let currentBg = null;
        if (this.skill == SkillType.FreezeTime) {
            this.skill0Bg.active = true
            this.skill1Bg.active = false
            this.skill2Bg.active = false
            currentBg = this.skill0Bg
        } else if (this.skill == SkillType.EraseGroup) {
            this.skill0Bg.active = false
            this.skill1Bg.active = true
            this.skill2Bg.active = false
            currentBg = this.skill1Bg
        } else {
            this.skill0Bg.active = false
            this.skill1Bg.active = false
            this.skill2Bg.active = true
            currentBg = this.skill2Bg
        }

        currentBg.getChildByName('LbTitle').getComponent(Label).string = CfgSkillInfo[this.skill].name
        currentBg.getChildByName('LbDesc').getComponent(Label).string = CfgSkillInfo[this.skill].desc
        this.btnGet.spriteFrame = ResMgr.getSpriteFrame(Bundle.UI, skillInfo.btnIcon)
    }

    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();
        }
    }

}


