using CommonAI.Zone;
using CommonAI.Zone.ZoneEditor;
using CommonLang;
using CommonLang.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static CommonAI.Zone.LaunchSpell;
using static CommonAI.Zone.SpellTemplate;
using static CommonAI.Zone.UnitActionData;

namespace xmds_battle_server.JSGModule
{
	/**
	 * 检测战斗服配置模块
	 */
	class CheckConfigModule
	{
		public static Logger log = LoggerFactory.GetDefLogger();

		//检测技能攻击异常
		public static void CheckActiveSkillAttackRange(EditorTemplates Templates)
		{
			HashMap <int, SkillTemplate> skills = Templates.Templates.getAllSkillData();

			foreach(SkillTemplate skillItem in skills.Values)
			{
				foreach (UnitActionData skillAction in skillItem.ActionQueue)
				{
					if(skillAction.IsJumpToTarget) { continue; }

					foreach(UnitActionData.KeyFrame actionFrame in skillAction.KeyFrames)
					{
						if(actionFrame.Spell == null || actionFrame.Spell.SpellID <= 0 || actionFrame.Move != null || 
							(actionFrame.Spell.PType == PosType.POS_TYPE_RANDOM_FOR_SPELL || actionFrame.Spell.PType == PosType.POS_TYPE_RANDOM_POS))
						{
							continue;
						}

						SpellTemplate spellData = Templates.Templates.getSpell(actionFrame.Spell.SpellID);
						if (spellData == null)
						{
							log.Error("技能:" + skillItem.ID + "-" + skillItem.Name + ", 配置了不存在的法术:" + actionFrame.Spell.SpellID);
							continue;
						}

						if (!skillItem.IsLaunchBody)
						{
							continue;
						}

						CheckSpellRange(Templates, skillItem, spellData);
					}
				}
			}
		}

		private static bool CheckSpellRange(EditorTemplates Templates, SkillTemplate skillItem, SpellTemplate spellData, int baseID = 0)
		{
			//无伤害的不判断
			if (spellData.HitIntervalKeyFrame == null && spellData.HitOnExplosionKeyFrame == null)
			{
				bool isAllNull = true;
				foreach (SpellTemplate.KeyFrame spellFrame in spellData.KeyFrames)
				{
					if (spellFrame.Attack != null)
					{
						isAllNull = false;
					}

					if (spellFrame.Spell != null && spellFrame.Spell.SpellID > 0 && baseID != spellFrame.Spell.SpellID)
					{
						if (!CheckSpellRange(Templates, skillItem, Templates.Templates.getSpell(spellFrame.Spell.SpellID), baseID))
						{
							baseID = spellFrame.Spell.SpellID;
						}
					}
				}

				if (isAllNull)
				{
					return true;
				}
			}

			if (spellData.MType == MotionType.Missile || spellData.MType == MotionType.Straight || spellData.MType == MotionType.StraightAndStop ||
						spellData.MType == MotionType.SeekerMissile)
			{
				if ((spellData.MSpeedAdd + spellData.MSpeedSEC) * spellData.LifeTimeMS * 0.001f + spellData.BodySize < skillItem.AttackRange)
				{				
					log.Error("A技能:" + skillItem.ID + "-" + skillItem.Name + ", 的法术:" + spellData.ID + ", 范围异常!");
					return false;
				}
			}
			else if (spellData.MType == MotionType.Immovability || spellData.MType == MotionType.Binding || spellData.MType == MotionType.Straight)
			{
				if (spellData.BodySize < skillItem.AttackRange)
				{
					log.Error("B技能:" + skillItem.ID + "-" + skillItem.Name + ", 的法术:" + spellData.ID + ", 范围异常!");
					return false;
				}
			}

			return true;
		}
	}
}