using System;
using System.Collections.Generic;
using CommonAI.Zone.Instance;
using XmdsCommon.Plugin;
using CommonAI.Zone;
using CommonLang.Log;
using CommonLang;
using CommonLang.Property;
using XmdsCommon.Message;
using XmdsCommonZone.Zones;

namespace XmdsServerNode.Node.R2bNotify
{
    public class R2bNotifyManager
    {
        private static Logger log = LoggerFactory.GetLogger("R2bNotifyManager");
        private static HashMap<string, Type> mMessageTypeMap = new HashMap<string, Type>();

        private static void LoadR2BNotifyMessage()
        {
            try
            {
                Type base_type = typeof(R2BNotifyMessage);
                foreach (Type sub_type in ReflectionUtil.GetNoneVirtualSubTypes(base_type))
                {
                    if (sub_type.IsSubclassOf(base_type))
                    {
                        mMessageTypeMap.Add(sub_type.Name, sub_type);
                    }
                }
            }
            catch (Exception err)
            {
                log.Error("LoadS2bNotifyMessage : " + err.ToString());
                throw;
            }
        }

        static R2bNotifyManager()
        {
            
        }

        public static void Init()
        {
            LoadR2BNotifyMessage();
        }

        public static R2BNotifyMessage GetR2bNotifyMessage(string eventName)
        {
            Type stype;
            if (mMessageTypeMap.TryGetValue(eventName, out stype))
            {
                R2BNotifyMessage ret = ReflectionUtil.CreateInstance(stype) as R2BNotifyMessage;
                return ret;
            }
            else
            {
                return null;
            }

        }
    }


    public abstract class R2BNotifyMessage
    {
        public string MessageName
        {
            get
            {
                return GetType().Name;
            }
        }
        public virtual void OnHandle(InstanceZone zone) { }
    }

    public delegate void OnGameServerMessageHandler(InstanceZone zone, R2BNotifyMessage act);

    public class QuestAcceptedR2B : R2BNotifyMessage
    {
        public string playerUUID;
        public string questID;
        public override void OnHandle(InstanceZone zone)
        {
            zone.QuestAdapter.OnQuestAcceptedHandler(playerUUID, questID);
        }
    }

    public class QuestCommittedR2B : R2BNotifyMessage
    {
        public string playerUUID;
        public string questID;
        public override void OnHandle(InstanceZone zone)
        {
            zone.QuestAdapter.OnQuestCommittedHandler(playerUUID, questID);
        }
    }

    public class QuestDroppedR2B : R2BNotifyMessage
    {
        public string playerUUID;
        public string questID;
        public override void OnHandle(InstanceZone zone)
        {
            zone.QuestAdapter.OnQuestDroppedHandler(playerUUID, questID);
        }
    }

    public class QuestStatusChangedR2B : R2BNotifyMessage
    {
        public string playerUUID;
        public string questID;
        public bool initStatus;
        public struct KeyValue
        {
            public string key;
            public string value;
        }
        public List<KeyValue> status;
        public override void OnHandle(InstanceZone zone)
        {
            //QuestAcceptedR2B合并到initStatus为true的QuestStatusChangedR2B消息中
            if (initStatus)
            {
                zone.QuestAdapter.OnQuestAcceptedHandler(playerUUID, questID);
            }

            if (status != null)
            {
                foreach (var item in status)
                {
                    zone.QuestAdapter.OnQuestStatusChangedHandler(playerUUID, questID, item.key, item.value);
                }
            }

            if (initStatus)
            {
                // INIT_OK 表示已全部加载完毕
                zone.QuestAdapter.OnQuestStatusChangedHandler(playerUUID, questID, XmdsQuestData.Attribute_InitOK, "1");
            }
        }
    }

    public class LaunchPlayerEffectR2B : R2BNotifyMessage
    {
        public string playerUUID;

        public string Name;
        public bool BindBody;
        public string BindPartName;
        public float ScaleToBodySize;
        public string SoundName;
        public int EarthQuakeMS;
        public float EarthQuakeXYZ;
        public string CameraAnimation;
        public int EffectTimeMS = 0;

        public LaunchEffect launchEffect;
        public override void OnHandle(InstanceZone zone)
        {
            zone.queueTask((InstanceZone z) =>
            {
                DynamicUnitEffectB2C e = new DynamicUnitEffectB2C();

                e.Name = Name;
                e.BindBody = BindBody;
                e.BindPartName = BindPartName;
                e.ScaleToBodySize = ScaleToBodySize;
                e.SoundName = SoundName;
                e.EarthQuakeMS = EarthQuakeMS;
                e.EarthQuakeXYZ = EarthQuakeXYZ;
                e.CameraAnimation = CameraAnimation;
                e.EffectTimeMS = EffectTimeMS;
                InstancePlayer p = z.getPlayerByUUID(playerUUID);
                if (p != null)
                {
                    p.queueEvent(e);
                }
            });
        }
    }

    public class TreasureActivityInfoR2B : R2BNotifyMessage
    {
        public int[] timeOpenCloseList;
        public int[] monsterIdProbabilityList;
        public int createBossIntervalMS;
        public int maxMonsterCount;

        public override void OnHandle(InstanceZone zone)
        {
            zone.queueTask((InstanceZone z) =>
            {
                try
                {
                    //TreasureActivityInfoR2B e = new TreasureActivityInfoR2B();

                    //e.timeOpenCloseList = timeOpenCloseList;
                    //e.monsterIdProbabilityList = monsterIdProbabilityList;
                    //e.createBossIntervalMS = createBossIntervalMS;
                    //e.maxMonsterCount = maxMonsterCount;

                    ZoneIntervalRebirthMonster zz = z as ZoneIntervalRebirthMonster;
                    zz.InitSceneDataFromGameServer(timeOpenCloseList, monsterIdProbabilityList, createBossIntervalMS, maxMonsterCount);

                }
                catch (Exception error)
                {
                    throw new Exception("TreasureActivityInfoR2B Error" + error.ToString());
                }
            });
        }
    }

	//场景通知事件
	public class ZoneFlagNotifyR2B : R2BNotifyMessage
	{
		public int value1;
		public override void OnHandle(InstanceZone zone)
		{
			zone.GSZoneFlagNotifyMsg(value1);
		}
	}
}