import LocalStorage from "../../../Data/LocalStorage";
import GameManager from "../../../GamePlay/GameManager";
import UIManager from "../../UIManager";
import InGameScreen from "../InGameScreen";
import { ScreenName } from "../ScreenUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemLevel extends cc.Component {

    @property(cc.Animation)
    animItemLevel: cc.Animation = null;
    @property(cc.Label)
    txtLevel: cc.Label = null;
    @property(cc.Node)
    iconChecked: cc.Node = null;
    @property(cc.Node)
    iconLocked: cc.Node = null;

    idBackground: number = 0;
    level: number = 0;
    isLock = true;

    initialize(level, idBackground) {
        this.isLock = false;
        this.level = level - 1;
        this.txtLevel.string = level + "";
        this.setup(LocalStorage.getLevel());
        this.idBackground = idBackground;
    }

    setup(curentLevel){
        if (this.level < curentLevel) {
            this.iconChecked.active = true;
            this.iconLocked.active = false;
            this.txtLevel.node.color.fromHEX("#FFFFFF");
        } else if (this.level == curentLevel) {
            this.iconChecked.active = false;
            this.iconLocked.active = false;
            this.txtLevel.node.color.fromHEX("#FFFFFF");
            this.animItemLevel.play("ItemLevel");
        } else {
            this.iconChecked.active = false;
            this.iconLocked.active = true;
            this.txtLevel.node.color.fromHEX("#A86244");
            this.isLock = true;
        }
    }

    retload(){
        this.setup(LocalStorage.getLevel());
    }

    onClickLevel() {
        if (this.isLock) {
            return;
        }
        UIManager.Instance.activeScreen(ScreenName.INGAME);
        GameManager.Instance.playGame(this.level);
    }

}
