using System;
using CommonLang;
using CommonAIClient.Unity.Utils;

namespace CommonAIClient.Unity.Battle
{
    public partial class ComAICell
    {
        private HashMap<Type, Action<CommonAI.Zone.ObjectEvent>> mObjectEvens = new HashMap<Type, Action<CommonAI.Zone.ObjectEvent>>();


        public void DoObjectEvent(CommonAI.Zone.ObjectEvent ev)
        {
            Action<CommonAI.Zone.ObjectEvent> action = null;
            if (mObjectEvens.TryGetValue(ev.GetType(), out action))
            {
                action(ev);
            }
        }

        protected virtual void RegistAllObjectEvent()
        {
            RegistObjectEvent<CommonAI.Zone.UnitEffectEvent>(ObjectEvent_UnitEffectEvent);
        }

        protected void RegistObjectEvent<T>(Action<T> action) where T : CommonAI.Zone.ObjectEvent
        {
            Type type = typeof(T);
            Action<CommonAI.Zone.ObjectEvent> outVal = null;
            if (!mObjectEvens.TryGetValue(type, out outVal))
            {
                mObjectEvens.Add(type, (e) =>
                {
                    action((T)e);
                });
            }
        }

        protected virtual void ObjectEvent_UnitEffectEvent(CommonAI.Zone.UnitEffectEvent ev)
        {
            PlayEffect(ev.effect, EffectRoot.Position(), EffectRoot.Rotation());
        }
    }

}