import { _decorator, Component,  Node,  Sprite, UITransform, Vec2 } from 'cc';
import { RoleCard } from './RoleCard';
import { PopupUIDataConfig } from './GameScene/Data/PopupUIDataConfig';
const { ccclass, property } = _decorator;

@ccclass('CharacterSlotMgr')
export class CharacterSlotMgr extends Component {
    private _roleList: Node = null;
    protected onLoad(): void {
        this._roleList = this.node.parent.parent.getChildByName("RoleList");
    }
    start() {
        this.setCardSlots();
    }

    private setCardSlots(){
        const ownCardSlot: number = PopupUIDataConfig.Instance.getAvailableCardSlot();
        for (const element of this.node.children) {
            if(element.getSiblingIndex() === (ownCardSlot - 1)){
                if(element.getChildByName("Lock").active){
                    element.getChildByName("Label").active = false;
                    element.getChildByName("Lock").active = false;
                    this.node.children[ownCardSlot].getChildByName("Label").active = true;
                }
            };
            if(element.getChildByName("Lock").active){
                if(element.getSiblingIndex() < ownCardSlot){
                    element.getChildByName("Label").active = false;
                    element.getChildByName("Lock").active = false;
                }
            }
        }
    }

    removeCardImg(pos: Vec2) {
        for (const element1 of this.node.children) {
            const box = element1.getComponent(UITransform).getBoundingBoxToWorld();
            if (box.contains(pos)) {
                let a: Node = element1.getChildByName('Sprite');
                if(!a.getComponent(Sprite).spriteFrame){
                    return;
                }
                for (const element2 of this._roleList.getChildByPath("view/content").children) {
                    if (element2.getChildByName("Role_Img").getComponent(Sprite).spriteFrame.name 
                    === a.getComponent(Sprite).spriteFrame.name) {
                        element2.getComponent(RoleCard).isOpenShadow(false);
                        a.getComponent(Sprite).spriteFrame = null;
                        return;
                    }
                }
            }
        }
    }
}


