using UnityEngine;
using Mono;
using System.Collections.Generic;
using AnimationType = Mono.AnimationData.AnimationType;
using FairyGUI;
using CommonLang;

namespace ET.Client
{
    [ChildOf(typeof(ModelViewComponent))]
	public class UnitRenderComponet : Entity, IAwake<GameObject>, IUpdate, IDestroy
	{
        public static readonly AnimatorCommand CMDIdle = new AnimatorCommand(AnimationType.Idle, true);
        public static readonly AnimatorCommand CMDRun = new AnimatorCommand(AnimationType.Run, true);
        public static readonly AnimatorCommand CMDDead = new AnimatorCommand(AnimationType.Dead);

        //buff产生的特效.
        public HashMap<string, uint> BuffEffects = new();

        //指令队列
        public List<AnimatorCommand> Commands = new ();

        public GameObject GameObject { get; set; }
        public AnimationData AniData;
        public IceFrozen FrozenComponent;
        public AnimatorCommand DoingCmd;
        public GComponent HeadBar;
        public GProgressBar HPBar;
        public GTextField NameBar;
        public Transform TransHeadInfo;
        public Transform TransChest;
        public Transform TransFoot;

        public int Vip = 0;

        //TODO: 获得模型绑定部件,如不存在返回Root
        public Transform GetBindPart(string partName)
        {
            if(partName == "chest")
            {
                if (TransChest == null)
                {
                    TransChest = GameObject.transform.Find("BindPart/" + partName);
                }

                if (TransChest != null)
                {
                    return TransChest;
                }
                else
                {
                    Log.Error($"NotFound part({partName}) @{GameObject.name}");
                }
            }
            else if (partName == "foot")
            {
                if (TransFoot == null)
                {
                    TransFoot = GameObject.transform.Find("BindPart/" + partName);
                }

                if (TransFoot != null)
                {
                    return TransFoot;
                }
                else
                {
                    Log.Error($"NotFound part({partName}) @{GameObject.name}");
                }
            }
            else
            {
                if(!partName.Contains('/'))
                {
                    partName = "BindPart/" + partName;
                }
                var trans = GameObject.transform.Find(partName);
                if(trans != null)
                {
                    return trans;
                }
                else
                {
                    Log.Error($"NotFound part({partName}) @{GameObject.name}");
                }
            }

            return GameObject.transform;
        }

        //重复回收利用的component,在下次使用前需要reset
        public void Reset()
        {
            FrozenComponent = null;
            GameObject = null;
            AniData = null;
            DoingCmd = null;
            HeadBar = null;
            TransHeadInfo = null;
            TransChest = null;
            TransFoot = null;
            Commands.Clear();
            BuffEffects.Clear();
            Vip = 0;
        }
    }

    //TODO: 支持ObjectPool
    public class AnimatorCommand
    {
        public AnimationType Type;
        public float Duration = -1f;
        public float Speed = 1.0f;
        public uint GroupId = 0;
        public bool Loop = false;
        public AnimatorCommand(AnimationType type, bool loop = false)
        {
            this.Type = type;
            this.Loop = loop;
        }
        public bool IsSkillCmd()
        {
            return Type >= AnimationType.Skill0 && Type < AnimationType.SkillMax;
        }
    }
}