import { _decorator, find, Label, math, Node, Rect, UITransform } from 'cc';
import { TouchMgr } from './TouchMgr';
import { CharacterSlotMgr } from './CharacterSlotMgr';
import { UIMgr } from '../../Frames/UIManager';
import { PopupUI } from '../UI/PopupUI';
import { UIType } from '../GameFrameWork/UIBase';
import { PopupUIDataConfig } from './GameScene/Data/PopupUIDataConfig';
const { ccclass, property } = _decorator;

@ccclass('TouchCharacterSlot')
export class TouchCharacterSlot extends TouchMgr {
    private _characterSlot: Node = null;
    private unLock: Node = null;
    protected onLoad(): void {
        this._characterSlot = this.node.parent.getChildByName("CharacterSlot");
    }
    func(pos: math.Vec2): void {
        this.node.parent.getChildByName("CharacterSlot").getComponent(CharacterSlotMgr).removeCardImg(pos);
        for (const node of this._characterSlot.children) {
            if (node.getChildByName("Label").active) {
                const box: Rect = node.getComponent(UITransform).getBoundingBoxToWorld();
                if (box.contains(pos)) {
                    this.unLock = node;
                    PopupUIDataConfig.Instance.setUIName("解锁");
                    PopupUIDataConfig.Instance.setTypeImg("Lock");
                    PopupUIDataConfig.Instance.setTypeMoney("Gold");
                    PopupUIDataConfig.Instance.setLabel(
                        `可用卡槽：${PopupUIDataConfig.Instance.getAvailableCardSlot()}`,
                        `下一级：${PopupUIDataConfig.Instance.getAvailableCardSlot() + 1}`,
                        `耗费：`, `2000`);
                    PopupUIDataConfig.Instance.setFunction(() => {
                        this.closeNode();
                    })
                    UIMgr.openUI("PopupUI", UIType.WIDGET);
                }
            }
        }
    }

    closeNode() {
        if (this.unLock) {
            this.unLock.getChildByName("Lock").active = false;
            this.unLock.getChildByName("Label").active = false;
            PopupUIDataConfig.Instance.setAvailableCardSlot(PopupUIDataConfig.Instance.getAvailableCardSlot() + 1)

            const indexNext: number = this.unLock.getSiblingIndex();
            if (indexNext === this.unLock.parent.children.length - 1) return;
            const nodeNext = this.unLock.parent.children[indexNext + 1];
            nodeNext.getChildByName("Lock").active = true;
            nodeNext.getChildByName("Label").active = true;
        }
    }
}