import { find, Node, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { msgac } from '../data/msgac';
import { ItemLayer } from '../item/ItemLayer';
import { eventManager } from '../manager/eventManager';
import { levelManager } from '../manager/levelManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
const { ccclass, property } = _decorator;

@ccclass('LevelEspSelectLayer')
export class LevelEspSelectLayer extends BaseLayer {

    levelLayer: Node;

    onLoad() {
        super.onLoad();

        this.levelLayer = find("levelLayer", this.node);
        this.levelLayer.addComponent(ItemLayer);

        this.initUI();
    }

    initUI() {
        this.levelLayer.getComponent(ItemLayer).initUI(this, levelManager.instance.levelEspIdArr, this.refreshLevelItem.bind(this));
    }

    refreshLevelItem(itemUI: Node, id: any) {
        let unlockNode = find("unlock", itemUI);
        if (playerModel.instance.isLevelEspUnlock(id)) {
            unlockNode.active = false;
        } else {
            unlockNode.active = true;
        }
    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "levelItem":
                this.onClickLevelItem(node);
                break;
            case "btnClose":
                this.onClickBtnClose(node);
                break;
            default:
                break;
        }
    }

    onClickLevelItem(node: Node) {
        let id = node["item"];
        if (playerModel.instance.isLevelEspUnlock(id)) {
            let obj: any = {};
            obj.level = id;
            eventManager.instance.send(msgac.openLevelLayer, obj);
            this.closeLayer();
            return;
        }

        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            playerModel.instance.unlockLevelEsp(id);
        });
    }

    onClickBtnClose(node: Node) {
        this.closeLayer();
    }

}

