﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace Rayman
{
	#region class
	public class CAbilityMeta : BaseMeta
	{
		//技能的类型
		public enum AbilityType
		{
			Attack, //攻击
			EffectInstance, //立刻执行的effect
			EffectTarget, //需要选定目标的effect--或者相当于A地面
			EffectPassive //被动技能
		}

		public AbilityType type = AbilityType.Attack;


		//对友方使用. 还是敌方使用
		//TODO 会改成验证器--星际like
		public bool friendly = false;

		//技能配合的动作clip
		public string clip = RayConst.CLIP_ATK;

		public CAbilityMeta(){}
	}
	#endregion

	#region manager
	public class CAbilityMetaManager
	{
		private static Dictionary<string, CAbilityMeta> _dict = new Dictionary<string, CAbilityMeta>();

		public static void AddMeta(CAbilityMeta meta)
		{
			if(_dict.ContainsKey(meta.idKey)) {
				RLog.LogError("CAbilityMetaManager ALREADY CONTAIN the ability with id -- {0} ", meta.idKey);
			}

			_dict[meta.idKey] = meta;
		}

		public static CAbilityMeta GetMeta(string id)
		{
			CAbilityMeta meta = null;
			bool v = _dict.TryGetValue(id, out meta);
			if(!v)RLog.LogError("CAbilityMetaManager DO NOT CONTAIN the ability with id -- {0} ", id);

			return meta;
		}
	}
	#endregion

	#region parser
	public class CAbilityMetaParser : MetaParser
	{
		public const string CAbilType_Attack = "CAbilAttack";

		public CAbilityMetaParser() : base(true){}

		public override void Execute (string content)
		{
			base.Execute(content);

			_xreader.ReadRootNode();
			foreach (XmlElement node in _xreader.rootChildNodes){
				switch (node.LocalName){
					case CAbilType_Attack:
						ParseAbility_Attack(node);
						break;
				}
			}
		}

		private void ParseAbility_Attack(XmlElement root)
		{
			CAbilAttackMeta meta = new CAbilAttackMeta();
			ParseAbility_Base(root, meta);


			_xreader.TryReadChildNodeAttr(root, "MinAttackSpeedMultiplier", "value", ref meta.MinAttackSpeedMultiplier);
			_xreader.TryReadChildNodeAttr(root, "MinAttackSpeedMultiplier", "value", ref meta.MinAttackSpeedMultiplier);
		}

		private void ParseAbility_Base(XmlElement root, CAbilityMeta meta)
		{
			meta.idKey = root.GetAttribute("id");
			_xreader.TryReadChildNodeAttr(root, "Clip", "value", ref meta.clip);

			CAbilityMetaManager.AddMeta(meta);
		}
	}
	#endregion
}