

import { _decorator, Material, Sprite} from 'cc';
import { BuyItemLayer } from './BuyItemLayer';
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 { Main } from '../game/Main';
import { ITEM_MAGENT_LV, ITEM_TIME_LV, GameConst, ITEM_TYPE } from '../core/common/GameConst';
import i18n from '../core/i18n/runtime-scripts/LanguageData';
const { ccclass, property } = _decorator;

@ccclass('LevelLayer')
export class LevelLayer extends BasePanel {
    @property({type: Material,tooltip: "灰色材质"})
    public garyMaterial: Material = null!;

    start() {
        this.GetGameObject("lock1").active = Data.user.lv < ITEM_MAGENT_LV;
        this.GetGameObject("btn_item_1").getComponent(Sprite).customMaterial = Data.user.lv < ITEM_MAGENT_LV ? this.garyMaterial : null;
        this.GetGameObject("icon1").active = Data.user.lv >= ITEM_MAGENT_LV;
        this.GetGameObject("lock2").active = Data.user.lv < ITEM_TIME_LV;
        this.GetGameObject("btn_item_2").getComponent(Sprite).customMaterial = Data.user.lv < ITEM_TIME_LV ? this.garyMaterial : null;
        this.GetGameObject("icon2").active = Data.user.lv >= ITEM_TIME_LV;
        
        this.SetText("lbl_item_magent_lv", i18n("main.%{value}级解锁", { value: ITEM_MAGENT_LV}));
        this.SetText("lbl_item_time_lv", i18n("main.%{value}级解锁", { value: ITEM_TIME_LV}));
        if (Data.user.magnet > 0) {
            this.SetText("lbl_item1_num", Data.user.magnet + "");
        }else {
            this.SetText("lbl_item1_num", "+");
        }
        if (Data.user.time > 0) {
            this.SetText("lbl_item2_num", Data.user.time + "");
        }else {
            this.SetText("lbl_item2_num", "+");
        }
        this.SetText("lbl_lvv", i18n("main.LV:%{value}", { value: Data.user.lv}));
    }

    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_item_1":
                if (Data.user.lv < ITEM_MAGENT_LV) return;
                if (Data.user.magnet == 0) {
                    WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
                        com.setItemType(ITEM_TYPE.Magnet);
                    })
                    this.close();
                }
                else {
                    this.GetGameObject("choose_item1").active = !this.GetGameObject("choose_item1").active;
                }
                break
            case "btn_item_2":
                if (Data.user.lv < ITEM_TIME_LV) return;
                if (Data.user.time == 0) {
                    WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
                        com.setItemType(ITEM_TYPE.Time);
                    })
                    this.close();
                }
                else {
                    this.GetGameObject("choose_item2").active = !this.GetGameObject("choose_item2").active;
                }
                break;
            case "btn_play":
                Data.user.useMagnet = this.GetGameObject("choose_item1").active;
                Data.user.useTime = this.GetGameObject("choose_item2").active;
                this.close();
                Main.I.play();
                break;
            case "btn_back":
                this.close();
                break;
        }
    }

}
