import { FightType } from "../../data/const/TypeConst";
import ResSpine from "../../frameWork/compment/ResSpine";
import { eg } from "../../frameWork/eg";
import { FightTeam } from "../../shared/fight/PtlFightTest";
import AssetMgr from "../../utils/AssetMgr";
import { uiCommon } from "../../utils/UICommon";
import UERole from "../ui/UERole";
import UEBattleView from "./UEBattleView";
import UESubHp from "./UESubHp";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UEBattleRole extends UERole {
    public m_ResSpine: ResSpine;
    m_IsLoadComplete: boolean;
    team: FightTeam;
    type: FightType;
    posX: number;
    posY: number;

    onLoad() {
        this.m_ResSpine = this.node.getChildByName("spine").getComponent(ResSpine);
    }

    start() {

    }

    setRole(pos: cc.Vec3, team: FightTeam, type: FightType) {
        this.team = team;
        this.type = type;
        this.node.x = pos.x;
        this.node.y = pos.y;
        this.posX = pos.x;
        this.posY = pos.y;
        this.LoadRoleRes();
    }

    GetSpinePath() {
        let spinePath = "role/nv";
        // if (this.team?.fid == "10086") {
        //     spinePath = "mon/10001";
        // }
        return spinePath;

    }
    public LoadRoleRes(cb?: Function): void {
        let spinePath = this.GetSpinePath();
        let that = this;
        this.m_IsLoadComplete = false;
        this.m_ResSpine.spSkeleton.skeletonData = null;
        this.m_ResSpine.setSpineData("spine", spinePath, (asset: sp.SkeletonData) => {
            if (asset) {

            }


            if (that.m_ResSpine && that.m_ResSpine.isValid) {
                that.m_ResSpine.addEventListener(that.OnEventListener.bind(that));
                that.m_ResSpine.AddSpineCompleteListener(that.OnCompleteListener.bind(that));
            }
            this.m_IsLoadComplete = true;
            cb && cb();
        }, false, "stand", true, 1);
    }

    ShowSubHp(element: [string, number], hpType?: string) {
        let hpLayer: cc.Node = UEBattleView.getInstance().GetHpLayer();
        let subHpNode = eg.poolManager.GetPool("subHPPool").get() || AssetMgr.instantiateUE(UESubHp).node;
        let pos = uiCommon.transPos(this.node, hpLayer);
        pos.y += this.GetHeightCenter() * Math.abs(this.node.scale);
        subHpNode.setPosition(pos);
        hpLayer.addChild(subHpNode);
        let subHpComp: UESubHp = subHpNode.getComponent(UESubHp);
        subHpComp.show(element, hpType);
    }

    /** 获取角色高度中心 */
    public GetHeightCenter() {
        let offset = this.node.getChildByName("spine").height * this.node.getChildByName("spine").scaleY * 0.7;//偏移到玩家身上的受击位置
        return offset;
    }

    playAni(aniName: string, loop: boolean, showLast?: boolean) {
        this.m_ResSpine.playAnimation(aniName, loop, showLast);
    }

    /** 监听事件帧 */
    OnEventListener(name: string) {
        // console.log("角色事件帧", name);
        if (name === "skill" || name === "atk") {

        }
    }

    OnCompleteListener(name: string) {

    }
}
