using UnityEngine;
using CommonAI.ZoneClient;
using CommonAI.Zone;
using CommonAIClient.Unity.Utils;

namespace CommonAIClient.Unity.Battle
{
    public partial class ComAISpell : ComAICell
    {
        private ComAICell mLauncher;
        private ComAICell mTarget;


        protected ZoneSpell ZSpell { get { return ZObject as ZoneSpell; } }
        protected ComAICell Launcher { get { return mLauncher; } }
        protected ComAICell Target { get { return mTarget; } }


        public ComAISpell(BattleScene battleScene, ZoneSpell obj)
            : base(battleScene, obj)
        {
            if (ZSpell.Sender != null)
            {
                mLauncher = BattleScene.GetBattleObject(ZSpell.Sender.ObjectID);
            }
            if (ZSpell.Target != null)
            {
                mTarget = BattleScene.GetBattleObject(ZSpell.Target.ObjectID);
            }

            SyncState();

            if (!string.IsNullOrEmpty(ZSpell.Info.FileName))
            {
                BattleFactroy.Instance.GameObjectAdapter.Load(
                    ZSpell.Info.FileName,
                    System.IO.Path.GetFileNameWithoutExtension(ZSpell.Info.FileName),
                    (succ, aoe) =>
                    {
                        if (succ)
                        {
                            if (IsDisposed)
                            {
                                BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
                                return;
                            }

                            OnLoadModelSuccess(aoe);
                        }
                    });
            }
        }

        protected virtual GameObject GetBindingPart(ComAICell unit)
        {
            DummyNode node = unit.GetDummyNode("chest_buff");
            if (node == null)
                return unit.EffectRoot;
            return node.gameObject;
        }
        
        protected virtual void OnLoadModelSuccess(AssetObjectExt aoe)
        {
            DisplayCell.Model = aoe;
            if (this.ZSpell.Info.FileBodyScale != 0)
            {
                aoe.transform.localScale = new Vector3(
                    this.ZSpell.Info.FileBodyScale,
                    this.ZSpell.Info.FileBodyScale,
                    this.ZSpell.Info.FileBodyScale);
            }

            SyncState();

            if (!string.IsNullOrEmpty(ZSpell.Info.FileNameSpawn))
            {
                LaunchEffect effect = new LaunchEffect();
                effect.Name = ZSpell.Info.FileNameSpawn;
                PlayEffect(effect, EffectRoot.Position(), EffectRoot.Rotation());
            }

            if (ZSpell.Info.BodyShape == SpellTemplate.Shape.LineToTarget
                && Launcher != null && !Launcher.IsDisposed
                && Target != null && !Target.IsDisposed)
            {
                //必须自己实现连接
                GameObject from = GetBindingPart(Launcher);
                GameObject to = GetBindingPart(Target);
                BattleFactroy.Instance.MakeDamplingJoint(aoe.gameObject, from, to);
            }
        }

        protected override void SyncState()
        {
            Vector3 pos = Extensions.ZonePos2NavPos(ZObject.Parent.Terrain.TotalHeight
                , ZObject.X, ZObject.Y, ZObject.Z);
            ObjectRoot.Position(new Vector3(pos.x, pos.y + ZSpell.LaunchHeight, pos.z));
            ObjectRoot.ZoneRot2UnityRot(ZObject.Direction);
        }

        protected override void OnDispose()
        {
            if (!string.IsNullOrEmpty(ZSpell.Info.FileNameDestory))
            {
                LaunchEffect effect = new LaunchEffect();
                effect.Name = ZSpell.Info.FileNameDestory;
                PlayEffect(effect, EffectRoot.Position(), EffectRoot.Rotation());
            }
            base.OnDispose();
        }
    }
}