using System.Collections;
using System.Collections.Generic;
using Animancer;
using Fort23.Core;
using Fort23.UTool;
using GameLogic.Combat.Hero;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Rendering;
using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;

public class CombatHeroEntity : ITimeLineSpecialDotPos, ILifeCycle, ITimeLineAnimtion
{
    public bool IsEnemy;
    public CombatAIBasic CombatAIBasic;
    public CombatHeroGameObject combatHeroGameObject;
    public CombatHeroInfo CurrCombatHeroInfo;
    public CombatHeroInfo MaxCombatHeroInfo;
    public CombatHeroTimeLineControl combatHeroTimeLineControl;
    public CombatHeroAnimtion combatHeroAnimtion;

    public Vector3 dotPos
    {
        get { return combatHeroGameObject.position; }
    }

    public Vector3 faceDir { get; }

    public async CTask<CombatHeroEntity> Init(CombatAIBasic combatAIBasic, CombatHeroInfo combatHeroInfo,
        System.Action<CombatHeroEntity> callBack = null)
    {
        //后面记到检查战斗里面不要出现异步加载，也不要出现同步IO加载

        GameObjectPool poolInterface =
            await GObjectPool.Instance.FetchAsync<GameObjectPool>(combatHeroInfo.modelName + ".prefab", null);
#if !COMBAT_SERVER
        if (poolInterface == null || poolInterface.own == null)
        {
            return null;
        }

        combatHeroTimeLineControl = new CombatHeroTimeLineControl();

        combatHeroGameObject = new CombatHeroGameObject();
        combatHeroGameObject.Init(this, poolInterface);
        NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
        if (combatAIBasic == null)
        {
            combatAIBasic = new CombatAIBasic();
        }

        combatAIBasic.Init(this, navMeshAgent);
        CombatAIBasic = combatAIBasic;
        CurrCombatHeroInfo = combatHeroInfo.Copy();
        MaxCombatHeroInfo = combatHeroInfo.Copy();
        AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
        combatHeroAnimtion = new CombatHeroAnimtion();
        combatHeroAnimtion.Init(animancerComponent);
        CombatAIBasic.ChangeState(CombatHeroStateType.idle);
        
        callBack?.Invoke(this);
#endif
        return this;
    }

    public void Update(float t)
    {
        CombatAIBasic.Update(t);
        combatHeroTimeLineControl.Update(t);
    }

    public T This<T>()
    {
        return (T)(object)this;
    }

    public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
        where T : ILifetCycleHitPoint
    {
        return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
    }

    public T GetMainHotPoin<T>(bool isIgnoreHind) where T : ILifetCycleHitPoint
    {
        return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
    }

    public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
    {
        combatHeroAnimtion.Play(animName, speed);
    }
}