/** 2dUI 特效 血条 粒子等管理器*/

import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view } from "cc";
import { resLoader } from "db://assets/core_tgx/base/ResLoader";
import { StormSunderGlobalInstance } from "../StormSunderGlobalInstance";
import { BloodComponent } from "../Component/BloodComponent";
import { PlayerInfo, PlayerInfoComponent } from "../Component/PlayerInfoComponent";
import { TornadoComponent } from "../Component/TornadoComponent";
import { GameUtil } from "../GameUtil";

export class Effect2DUIMgr {
    private static _instance: Effect2DUIMgr;
    public static get Instance(): Effect2DUIMgr {
        if (this._instance == null) {
            this._instance = new Effect2DUIMgr();
        }
        return this._instance;
    }

    public static get inst(): Effect2DUIMgr {
        return this.Instance;
    }

    private bloodMap: Map<Node, Node> = new Map(); // 存储节点与对应血条的映射
    private playerInfoMap: Map<Node, Node> = new Map(); // 存储节点与玩家信息的映射

    async showBlood(target: Node, hpPercent: number) {
        let bloodNode = this.bloodMap.get(target) as any;

        if (!bloodNode) {
            const bloodPrefab = await resLoader.loadAsync(resLoader.gameBundleName, "Prefabs/Blood");
            bloodNode = instantiate(bloodPrefab);
            bloodNode.parent = StormSunderGlobalInstance.instance.bloodUI;
            this.bloodMap.set(target, bloodNode);
        }

        const mainCamera = StormSunderGlobalInstance.instance.camera;
        if (!mainCamera) {
            console.error("Main camera not found!");
            return;
        }

        const uiPos = GameUtil.worldToScreenLocal(target).clone();
        // 设置位置偏移
        const offsetY = 200;
        const offsetX = 100;
        bloodNode.setPosition(uiPos.x + offsetX, uiPos.y + offsetY);

        // 更新血量显示
        const bloodComp = bloodNode.getComponent(BloodComponent);
        if (bloodComp) {
            bloodComp.updateHP(hpPercent);
        }
    }

    //添加玩家预设
    async addPlayerInfo(target: Node, playerInfo: PlayerInfo) {
        let infoNode = this.playerInfoMap.get(target) as any;

        if (!infoNode) {
            const infoPrefab = await resLoader.loadAsync(resLoader.gameBundleName, "Prefabs/PlayerInfo");
            infoNode = instantiate(infoPrefab);
            infoNode.parent = StormSunderGlobalInstance.instance.effectUI;
            this.playerInfoMap.set(target, infoNode);

            const playerInfoComp = infoNode.getComponent(PlayerInfoComponent);
            if (playerInfoComp) {
                playerInfoComp.updateInfo(playerInfo);
            }
        }

        const uiPos = GameUtil.worldToScreenLocal(target).clone();
        const offsetY = 250;
        const offsetX = 70;
        infoNode.setPosition(uiPos.x + offsetX, uiPos.y + offsetY);
    }

    //更新对应玩家信息
    updatePlayerInfo(target: Node, playerInfo: PlayerInfo) {
        const infoNode = this.playerInfoMap.get(target);
        if (infoNode) {
            const playerInfoComp = infoNode.getComponent(PlayerInfoComponent);
            if (playerInfoComp) {
                playerInfoComp.updateInfo(playerInfo);
            }
        }
    }

    // 清理血条
    removeBlood(target: Node) {
        const bloodNode = this.bloodMap.get(target);
        if (bloodNode) {
            bloodNode.destroy();
            this.bloodMap.delete(target);
        }
    }

    // 清理玩家信息节点
    removePlayerInfo(target: Node) {
        const infoNode = this.playerInfoMap.get(target);
        if (infoNode) {
            infoNode.destroy();
            this.playerInfoMap.delete(target);
        }
    }
}

