import { _decorator, Component, Node, Sprite, UITransform, Vec2, Vec3, view } from 'cc';
import { ModulerBase } from './ModulerBase';
import { GameInfo } from '../../GameInfo';
import { LevelData, TowerData } from '../../DataItem/ItemData';
import { dataMgr } from '../../Frames/DataManager';
import { resMgr } from '../../Frames/ResourcesMgr';
const { ccclass, property } = _decorator;

@ccclass('CardMgr')
export class CardMgr extends ModulerBase {
    private _cards: {node: Node, data: TowerData}[] = [];

    init(): void {

        //显示有哪些卡片可用
        const id: number = GameInfo.Instance.CurTheme * 1000 + GameInfo.Instance.CurLv;
        const data: LevelData = dataMgr.getData(id, "LevelDt");
        for(const id of data.towerid){
            const node = new Node();
            const towerDt: TowerData = dataMgr.getData(id, "TowerDt");
            node.addComponent(Sprite).spriteFrame = 
            resMgr.getSpriteFrame(towerDt.cardImg[1]);
            node.parent = this.node.getChildByName("Content");
            this._cards.push({node:node, data: towerDt});
        }
        
    }
    start() {

    }

    update(deltaTime: number) {
        
    }

    touchCard(pos: Vec2):TowerData{
        for(const card of this._cards){
            const box = card.node.getComponent(UITransform).getBoundingBoxToWorld();
            if(box.contains(pos)){
                this.hide();
                return card.data;
            }
        }
        this.hide();
        return null;;

    }
    //当前卡片是否显示
    isShow(){
        return this.node.active;
    }

    show(pos: Vec3){
        //view.getVisibleSize().width;
        //view.getVisibleSize().height;
        // if(this.node.active){
        //     this.hide();
        //     return true;

        // }
        this.node.setPosition(pos);
        this.node.active = true;
    }

    hide(){
        this.node.active = false;

    }
}


