using CommonAI.ZoneClient;
using ET.EventType;
using FairyGUI;
using Sirenix.Utilities;
using UnityEngine;

namespace ET.Client
{
    [Event(SceneType.None)]
    [FriendOfAttribute(typeof(ET.Client.UnitRenderComponet))]
    public class OnNewZoneObjectHandler : BEvent<EventType.OnNewZoneObject>
    {
        public override void OnEvent(EventType.OnNewZoneObject args)
        {
            var obj = UnitMgr.Instance.GetUnit(args.ObjectId);
            if (obj == null)
            {
                //还没显示就已挂掉的单位,走好
                Log.Debug($"ignore dead unit: {args.ObjectId}");
                return;
            }

            if (obj is BattleUnit)
            {
                CreatUnitModel(obj as BattleUnit, args.ModelName).Coroutine();
            }
            else if (obj is BattleSpell)
            {
                //TODO: 性能有问题时,可以减少法术展示
                CreateSpellModel(obj as BattleSpell).Coroutine();
            }
            else
            {
                Log.Error("unknow new object");
            }
        }

        private static CommonLang.Geometry.Vector3 vecTemp = new();
        private async ETTask CreatUnitModel(BattleUnit unit, string modelName)
        {
            var zu = unit.ZUnit;
            if(modelName.IsNullOrWhitespace() )
            {
                modelName = $"Unit_{zu.Info.FileName}";
            }

            var go = await GameObjectPool.Instance.Acquire(modelName);
            go.SetActive(true);
            go.transform.parent = GlobalViewComponent.Instance.Unit;
            vecTemp.Set(zu.X, zu.Y, zu.Z);
            go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
            go.transform.rotation = RenderUtils.UnityRotationFromBattle(zu.Direction);

            var render = ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
            CreateHeadbar(render, zu).Coroutine();
            if (unit is BattleActor)
            {
                CameraMgr.FollowMe(go.transform.position);
            }
            //Log.Debug($"unitRender({zu.ObjectID}),pos({zu.X},{zu.Y},{zu.Z}) ok.");

            if(zu.GetBuffStatusCount() > 0)
            {
                EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(zu.ObjectID, null, BuffChangeType.Reload));
            }
            //TODO: 同步ZoneUnit status
        }

        private async ETTask CreateSpellModel(BattleSpell spell)
        {
            var zs = spell.ZoneObject as ZoneSpell;
            var res = zs.Info.FileName;
            if (res.IsNullOrWhitespace())
            {
                Log.Debug($"spell({zs.Info.ID}) not config fileName");
                return;
            }

            if (!UnitMgr.Instance.HasUnit(zs.ObjectID) || (zs.Sender != null && !UnitMgr.Instance.HasUnit(zs.Sender.ObjectID)))
            {
                //还没显示就已挂掉的单位,走好
                Log.Debug($"ignore dead unit's spell: {zs.ObjectID}@{zs.Sender.ObjectID}");
                return;
            }

            GameObject go = await GameObjectPool.Instance.Acquire($"Effect_{res}");
            go.transform.parent = GlobalViewComponent.Instance.Unit;
            go.SetActive(true);
            go.transform.localScale = Vector3.one * zs.Info.FileBodyScale;
            vecTemp.Set(zs.X, zs.Y, zs.Z + zs.LaunchHeight);
            go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
            go.transform.rotation = RenderUtils.UnityRotationFromBattle(zs.Direction);
            ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(zs.ObjectID, go, true);

            var et = go.GetComponent<EffectTime>();
            if (et != null)
            {
                et.speed = zs.Info.EffectAddSpeed;
                //et.playAnimName = zs.Info.AnimtionName;
                //TODO:特效不支持播放动画
            }
        }

        private async ETTask CreateHeadbar(UnitRenderComponet render, ZoneUnit zu)
        {
            var view = await GameObjectPool.Instance.AcquireHeadBar();
            view.visible = true;

            render.HeadBar = view;
            var name = view.GetChild("text_name");
            var progresshp = view.GetChild("bar_hp") as GProgressBar;
            progresshp.max = 100;
            progresshp.min = 0;

            name.text = render.GameObject.name;
            progresshp.value = zu.HP * 100 / zu.MaxHP;
            render.SyncHeadBarPos();
        }
    }
}