using CommonAI.Zone;
using CommonAI.Zone.Instance;
using System;
using XmdsCommonServer.Plugin;
using XmdsCommonServer.Plugin.XmdsSkillTemplate.DamageCalculator;
using XmdsCommonServer.Plugin.XmdsSkillTemplate.Skills;
using static CommonAI.Zone.Instance.InstanceUnit;

namespace XmdsCommonSkill.Plugin.Buffs
{
    //剑影强化模块类,因为buff只是挂一个标记而已,所以buff没有能力(普攻强化放在BuffState的扩展字段中)
    public class XmdsBuff_SwordShadow
    {
        public readonly static int BuffID = (int)XmdsBuffBase.XmdsBuffList.PASSIVE_FS_6_1;
        //剑影类型
        public enum SwordShadownType : ushort
        {
            NormalAtk       = 0,          // 默认普攻 
            Normal          = 1,          // 默认剑影
            TalentLv1       = 2,          // 剑气1层-剑影
			TalentLv2		= 3,          // 剑气2层-剑影
			TalentLv3		= 4,          // 剑气3层-剑影
		}

        private static void UpdateBuffExtData(ref BuffState buffState, SwordShadownType addType)
        {
            buffState.BuffExtData = buffState.BuffExtData * 10 + (int)addType;

            //只有五个剑影
            if (buffState.BuffExtData > 100000)
            {
                buffState.BuffExtData = buffState.BuffExtData % 100000;
            }
        }

        //释放技能增加剑影
        public static void LauncherSkillAddSwordShadow(XmdsVirtual launcher)
        {
			if (launcher == null)
			{
				return;
			}

			SwordShadownType addType = SwordShadownType.Normal;
			byte talentLv = launcher.GetPlayerCache().GetTalentLv();
			if (talentLv == 1)
			{
				addType =  SwordShadownType.TalentLv1;
			}
			else if (talentLv == 2)
			{
				addType = SwordShadownType.TalentLv2;
			}
			else if (talentLv == 3)
			{
				addType = SwordShadownType.TalentLv3;
			}

            BuffState state = launcher.mUnit.GetBuffByID(BuffID);
            if(state == null)
            {
                launcher.mUnit.AddBuff(BuffID, launcher.mUnit, 0, false, false, false, (int)addType);
            }
            else
            {
                UpdateBuffExtData(ref state, addType);
                launcher.mUnit.AddBuff(BuffID, launcher.mUnit);
            }

			
        }

        //消耗剑影
        public static void LanucherNormalSkill(XmdsVirtual launcher, ref BuffState state)
        {
            int step = 0;
            int lockStep = GetShadowIndex(launcher, ref state, ref step);
            if(step > 0)
            {
				//使用了一层buff
				state.SetLayerLevel((byte)(state.OverlayLevel - 1));
               // state.OverlayLevel -= 1;

                state.BuffExtData = state.BuffExtData - step * lockStep;
                if (state.BuffExtData < 0)
                {
                    state.BuffExtData = 0;
                }

				// 法师专属逻辑-释放剑影,增加剑气值
				launcher.Talent__Add(0);
			}
        }


        public static int GetShadowIndex(XmdsVirtual launcher, ref BuffState state, ref int step)
        {
            if (launcher == null || state == null)
            {
                return 0;
            }

            step = 1;
            int tempExt = state.BuffExtData;
            while (tempExt > 10)
            {
                step *= 10;
                tempExt = tempExt / 10;
            }

            if (tempExt <= 0 || tempExt > (int)SwordShadownType.TalentLv3)
            {
                return 0;
            }

            return tempExt;
        }
    }
}