
import { _decorator, Component, Node, Sprite, spriteAssembler, SpriteFrame } from 'cc';

import BasePanel from '../core/component/BasePanel';
import { audioMgr } from '../core/manager/AudioManager';
import Data from '../core/manager/Data';
import WindowManager from '../core/manager/WindowManager';
import MsgHints from '../core/utils/MsgHints';
import { Main } from '../game/Main';
import { ITEM_TYPE, GameConst } from '../core/common/GameConst';
import i18n from '../core/i18n/runtime-scripts/LanguageData';
const { ccclass, property } = _decorator;

@ccclass('BuyItemLayer')
export class BuyItemLayer extends BasePanel {
    private type: ITEM_TYPE;

    setItemType(type: ITEM_TYPE) {
        this.type = type;
        this.GetGameObject("Magnet").active = type == ITEM_TYPE.Magnet;
        this.GetGameObject("Hint").active = type == ITEM_TYPE.Hint;
        this.GetGameObject("Frozen").active = type == ITEM_TYPE.Frozen;
        this.GetGameObject("Time").active = type == ITEM_TYPE.Time;
        this.SetText("lbl_name", GameConst.getItemName(type))
        Main.I._GameUI.pasue = true;
    }
    
    onDestroy() {
        super.onDestroy();
        Main.I._GameUI.pasue = false;
    }

    onBtnClicked(event: any, customEventData: any) {
       audioMgr.playOneShot(GameConst.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.close();
                break;
            case "btn_buy":
                if (Data.user.coin < 90) {
                    WindowManager.ins.open("BuyCoinLayer");
                }else {
                    MsgHints.show(i18n("main.购买成功"))
                    Data.user.coin -= 90;
                    Data.user.addItem(this.type, 3);
                    Data.save();
                    this.close();
                }
                break;
        }
    }
}

