
import GameDataCenter from "../../data/GameDataCenter";
import UEBase from "../../frameWork/compment/UEBase";
import EventMng from "../../manager/EventMng";
import { GridEvent } from "./GridEvent";
import UECube from "./UECube";
import UEMergeTipItem from "./UEMergeTipItem";

const { ccclass, property } = cc._decorator;

export interface I_MergeTipData {
    idx: number;
    ueCube1: UECube;
    ueCube2: UECube;
    dir: number;//1左，2右
}
@ccclass
export default class UEMergeTip extends UEBase {
    static readonly BundleKey: string = "gridMap";
    static readonly PrefabUrl: string = "UEMergeTip";
    static readonly CLS: string = "UEMergeTip";
    @property(cc.Prefab)
    prefab_item: cc.Prefab = null!;
    @property(cc.Node)
    node_content: cc.Node = null!;
    @property(cc.Node)
    bg: cc.Node = null!;

    data: I_MergeTipData = null!;
    Init(data: I_MergeTipData) {
        //检测是否重复位置和重复合成链
        // this.node.active = true;
        // EventMng.emit(GridEvent.HC_MERGE_TIP, true);
        // if (this.data && data.idx == this.data.idx) {
        //     return;
        // }
        this.data = data;
        let mergeArr = GameDataCenter.gridMap.GetMergeItems(data.ueCube1, data.ueCube2);
        if (mergeArr.length > 0) {
            this.node.active = true;
            for (let i = 1; i <= 4; i++) {
                let itemNode = this.node_content.children[i - 1];
                if (!itemNode) {
                    itemNode = cc.instantiate(this.prefab_item);
                    this.node_content.addChild(itemNode);
                }
                let pos = this.GetCirclePosition(i - 1, mergeArr.length);
                itemNode.setPosition(pos);
                itemNode.getComponent(UEMergeTipItem).Init({ data: mergeArr[i - 1] });
            }
            EventMng.emit(GridEvent.HC_MERGE_TIP, { isShow: true, mergeArr: mergeArr });
            if (this.data.dir == 2) {
                //补充旋转角度
                this.bg.angle = 25;
            } else {
                this.bg.angle = -25;
            }
        } else {
            this.node.active = false;
            EventMng.emit(GridEvent.HC_MERGE_TIP, { isShow: false });
        }
    }


    Hide() {
        this.node.active = false;
        EventMng.emit(GridEvent.HC_MERGE_TIP, { isShow: false });
    }

    /** 计算圆上的位置 */
    private GetCirclePosition(index: number, total: number): cc.Vec2 {
        const radius = 120; // 圆的半径

        if (total === 1) {
            // 只有一个元素时，放在正上方
            return cc.v2(0, radius);
        }


        // 计算每个元素之间的角度
        const angleStep = 60; // 相邻两个元素之间的角度
        const startAngle = (this.data.dir == 1 ? 60 : 120) + -total * angleStep / 2 + angleStep / 2; // 从90度（正上方）开始，向右分布

        // 计算当前元素的角度（角度转弧度）
        const angle = (startAngle + index * angleStep) * Math.PI / 180; // 减去角度使其向右分布

        // 计算圆上的位置
        const x = radius * Math.cos(angle);
        const y = radius * Math.sin(angle);

        return cc.v2(x, y);
    }


}
