import { _decorator, instantiate, Node } from 'cc';
import { UIBase } from '../GameFrameWork/UIBase';
import { resMgr } from '../../Frames/ResourcesMgr';
import { PopupUIDataConfig } from '../MyApp/GameScene/Data/PopupUIDataConfig';
import { UIMgr } from '../../Frames/UIManager';
import { GameInfo } from '../../GameInfo';
import { Tip } from './Tip';
import { messageMgr } from '../../Frames/MessageMgr';
const { ccclass, property } = _decorator;

@ccclass('PopupUI')
export class PopupUI extends UIBase {
    private _UIName: string = null;
    private _typeImg: string = null;
    private _typeMoney: string = null;
    private _label_1: string = null;
    private _label_2: string = null;
    private _label_3: string = null;
    private _consume: string = null;

    protected onEnable(): void {
        this._UIName = PopupUIDataConfig.Instance.getUIName()
        this._typeImg = PopupUIDataConfig.Instance.getTypeImg()
        this._typeMoney = PopupUIDataConfig.Instance.getTypeMoney();
        this._label_1 = PopupUIDataConfig.Instance.getLabel()[0];
        this._label_2 = PopupUIDataConfig.Instance.getLabel()[1];
        this._label_3 = PopupUIDataConfig.Instance.getLabel()[2];
        this._consume = PopupUIDataConfig.Instance.getLabel()[3];
    }
    onStart() {
        this.onBtnClick("_btnBack", this.onBtnBack, this);
        this.onBtnClick("_btnYes", this.onBtnYes, this);
        this.onBtnClick("_btnCancel", this.onBtnCancel, this);
        this.uiName();
        this.typeImg();
        this.label()
    }

    uiName() {
        this.getLabel("_uiName").string = this._UIName;
    }

    typeImg() {
        this.getSprite("_typeImg").spriteFrame = resMgr.getSpriteFrame(this._typeImg);
        this.getSprite("_typeMoney").spriteFrame = resMgr.getSpriteFrame(this._typeMoney);
    }

    label() {
        this.getLabel("_label-1").string = this._label_1;
        this.getLabel("_label-2").string = this._label_2;
        this.getLabel("_label-3").string = this._label_3;
        this.getLabel("_consume").string = `${this._consume}`;
    }

    //返回
    private onBtnBack() {
        this.hide(false);
    }
    //确定
    private onBtnYes() {
        // if (this._typeMoney === "Gold") {
        //     if (Number(this._consume) > GameInfo.Instance.getGold()) {
        //         const tip: Node = instantiate(resMgr.getPrefab("Tip"));
        //         tip.getComponent(Tip).setContent("金币不足！");
        //         tip.parent = this.node;
        //         return;
        //     }
        // } else if(this._typeMoney === "Diamond"){
        //     if (Number(this._consume) > GameInfo.Instance.getDiamond()) {
        //         const tip: Node = instantiate(resMgr.getPrefab("Tip"));
        //         tip.getComponent(Tip).setContent("钻石不足！")
        //         tip.parent = this.node;
        //         return;
        //     }
        // }
        const resourceMap = {
            "Gold": {
                getAmount: GameInfo.Instance.getGold(),
                message: "金币不足！",
            },
            "Diamond": {
                getAmount: GameInfo.Instance.getDiamond(),
                message: "钻石不足！",
            }
        };

        const resource = resourceMap[this._typeMoney];
        //消耗
        const consume: number = Number(this._consume);
        //总共
        const totalGold: number = GameInfo.Instance.getGold();
        
        if (resource && Number(this._consume) > resource.getAmount) {
            const tip: Node = instantiate(resMgr.getPrefab("Tip"));
            tip.getComponent(Tip).setContent(resource.message);
            tip.parent = this.node;
            return;
        }
        messageMgr.dispatch("reduceGold", (totalGold - consume));
        GameInfo.Instance.setGold((totalGold - consume));
        (PopupUIDataConfig.Instance.getFunction())();
        UIMgr.closeUI("PopupUI", true);
        PopupUIDataConfig.Instance.clearDt();
    }

    //取消
    private onBtnCancel() {
        this.hide(false);
    }
}


