using CommonAI.Zone.Helper;
using CommonAI.Zone.Instance;
using XmdsCommonServer.Plugin;
using XmdsCommonServer.XLS.Data;
using XmdsCommonSkill.Plugin.Buffs;
using System;
using static CommonAI.Zone.Instance.InstanceUnit;
using XmdsCommon.Plugin;
using CommonAI.Zone;
using XmdsCommonServer.Plugin.Units;
using CommonAI.Zone.Formula;
using XmdsCommonSkill.Plugin.Skills;
using XmdsCommonServer.Plugin.XmdsSkillTemplate.DamageCalculator;
using CommonLang;
using CommonAI.Data;
using static CommonAI.Zone.Helper.HateSystem;
using System.Collections.Generic;
using XmdsCommonServer.Message;
using CommonAI.ZoneServer.JSGModule;

namespace XmdsCommonSkill.Plugin.PassiveSkills.Remedy
{
	/// <summary>
	/// boss-山大王管辖boss被动
	/// </summary>
	class Boss_91001 : XmdsPassiveSkillBase
	{
		public static int ID = 91001;
		public override int SkillID { get { return ID; } }

		//绑定buffID
		private static int mBuffID = (int)XmdsBuffBase.XmdsBuffList.BOSS_WEAK;

		private static XmdsSkillValue mValueSet1;               // 血量百分比N时,触发护卫事件
		private static XmdsSkillValue mValueSet2;               // 山大王死亡时触发虚弱逻辑:每隔N1秒N2概率虚弱N3秒
		private static XmdsSkillValue mValueSet3;               // 山大王死亡期间,受伤增加

		//内部变量
		private int mUUID_1 = 0;
		private int mTriggerHP;
		private bool mTriggerHelp;
		private float mDamageDeeper;
		private XmdsVirtual mOwner;
		//虚弱逻辑
		private long mNextWeakTime;
		public short mWeakRate;
		public int mWeakInterval;

		protected override void OnInit(GameSkill skillInfo, XmdsVirtual owner)
		{
			XmdsBuffPack pack = XmdsBuffFactory.GetInstance().GetXmdsBuffPack(mBuffID);
			BuffTemplate bt = pack.mBuffTemplate;
			bt.IsHarmful = true;
			bt.LifeTimeMS = mValueSet2.GetValue(skillInfo.SkillLevel, 3);
			pack.BindTemplateAndDispose();
			owner.RegistSendBuff(bt);


			this.mUUID_1 = owner.RegistOnHitDamage(OnHandleHitDmage, skillInfo, true);
			this.mTriggerHP = CUtils.CastInt(owner.MirrorProp.MaxHP * XmdsUnitProp.PER * mValueSet1.GetValue(skillInfo.SkillLevel));
			this.mOwner = owner;
			this.mDamageDeeper = mValueSet3.GetValue(skillInfo.SkillLevel) * XmdsUnitProp.PER;
			this.mWeakRate = (short)mValueSet2.GetValue(skillInfo.SkillLevel, 2);
			this.mWeakInterval = mValueSet2.GetValue(skillInfo.SkillLevel, 1);

		}

		protected override void OnDispose(XmdsVirtual owner)
		{
			owner.UnRegistOnHitDamage(this.mUUID_1);
		}

		protected override void OnUpdate(int intervalMS, bool slowRefresh)
		{
			if(!slowRefresh)
			{
				return;
			}

			if (this.mOwner.GetBattleStatus() <= BattleStatus.ReadyBattle)
			{
				mNextWeakTime = 0;
				mTriggerHelp = false;
				return;
			}

			int kingID = this.mOwner.mUnit.Parent.GetZoneKingID();
			if (kingID > 0 && mNextWeakTime < CommonLang.CUtils.localTimeMS && !JSGMountainKingModule.IsKingAlive(this.mOwner.mUnit.Parent, kingID) 
				&& this.mOwner.RandomPercent(this.mWeakRate))
			{
				this.mNextWeakTime = this.mWeakInterval + CommonLang.CUtils.localTimeMS;
				this.mOwner.mUnit.AddBuff(mBuffID);
			}
		}

		private float OnHandleHitDmage(float damage, XmdsVirtual hitted, XmdsVirtual attacker, AttackSource source,
		   ref XmdsVirtual.AtkResult result, DamageType damageType, GameSkill skill, ref bool isEndDispatch)
		{
			int kingID = hitted.mUnit.Parent.GetZoneKingID();
			if (kingID <= 0)
			{
				return damage;
			}

			if (!JSGMountainKingModule.IsKingAlive(hitted.mUnit.Parent, kingID))
			{
				damage += damage * this.mDamageDeeper;
			}
			else if (!mTriggerHelp && hitted.mUnit.IsActive && hitted.mUnit.Virtual.GetBattleStatus() >= BattleStatus.PVE)
			{
				//一刀就干到了触发值的,可以过滤,毕竟第二刀就死了
				int leftHP = (int)(hitted.mUnit.CurrentHP - damage);
				if (leftHP > 0 && leftHP < mTriggerHP)
				{
					mTriggerHelp = true;
					List<HateInfo> ret = new List<HateInfo>();
					hitted.GetHateSystem().GetHateList(ret);
					//

					MountainKingHelpEventB2R notifyEvent = new MountainKingHelpEventB2R();
					notifyEvent.mapId = hitted.mUnit.Parent.GetSceneID();
					notifyEvent.monsterId = hitted.mUnit.Info.ID;
					notifyEvent.monsterObjectId = hitted.mUnit.ID;
					int playerCount = Math.Min(3, ret.Count);
					for (int i = 0; i < ret.Count; i++)
					{
						InstanceUnit player = ret[i].Unit;
						if (player == null || !player.IsPlayer)
						{
							continue;
						}

						notifyEvent.players.Add(player.PlayerUUID);
					}

					if (notifyEvent.players.Count > 0)
					{
						hitted.mUnit.queueEvent(notifyEvent);
					}
					else
					{
						log.Warn("触发大王护卫事件,找不到仇恨目标:" + hitted.mUnit.Parent.GetSceneID() + ", " + hitted.mUnit.Info.ID + ", " + ret.Count 
							+ ", 本次伤害:" + damage + ", 剩余血量: " + leftHP + ", PV状态: " + hitted.mUnit.Virtual.GetBattleStatus()
							+ ", 本次攻击者信息:" + attacker.mUnit.PlayerUUID + ", " + attacker.mUnit.Info.ID + ", " + JSGModule.GetAttackSourceDes(source));
					}
				}
			}

			return damage;
		}

		protected override void OnInitSkillParam()
		{
			XmdsSkillData data = XmdsDataMgr.GetInstance().GetXmdsSkillData(ID);
			InitData(data, out mValueSet1, XmdsSkillData.XmdsSkillDataKey.ValueSet);
			InitData(data, out mValueSet2, XmdsSkillData.XmdsSkillDataKey.ValueSet2);
			InitData(data, out mValueSet3, XmdsSkillData.XmdsSkillDataKey.ValueSet3);
		}
	}
}