using XGame.Database;
using XGame.Framework;

namespace FL.Battle.Buffs
{
    /// <summary>
    /// 元素易伤debuff
    /// </summary>
    public class ElementAddDebuff : Buff
    {
        private int _autoTickLayer = 3;
        private int _elementTickLayer = 5;
        //private EElementType _elementType;
        protected override void OnInited()
        {
            //_elementType = (EElementType)(Table.BuffType / 10 % 10); // BuffType的十位数为元素类型
            Assert.IsTrue((Table.BuffType / 10 % 10) == (int)ElementType, $"元素易伤配置错误. BuffType:{Table.BuffType} ElementType:{ElementType}");
            _autoTickLayer = Table.BuffTypeNum[2];
            _elementTickLayer = Table.BuffTypeNum[4];
        }
        protected override void OnDispose()
        {
        }
        public bool IsReachLayer3()
        {
            return Layers >= _autoTickLayer;
        }
        /// <summary>
        /// 是否达到3层元素易伤条件
        /// AddBuff时判定
        /// </summary>
        /// <param name="attacker"></param>
        /// <returns></returns>
        public bool TryTickLayer3(ICombatCalculation attacker)
        {
            if (Layers < _autoTickLayer) return false;
            if (Layers == _autoTickLayer)
            {
                var buffOrSkillId = Table.BuffTypeNum[3];
                if (Table.BuffType == (int)EBuffType.ElementAddPoison)
                {
                    var calculation = Context.Calculation.GetSkillTrigger(buffOrSkillId, attacker);
                    calculation?.TickSkill(buffOrSkillId, Context.Entity.EntityId);
                }
                else
                {
                    var calculation = Context.Calculation.GetBuffTrigger(buffOrSkillId, attacker);
                    calculation?.TickBuff(buffOrSkillId);
                }
            }
            return true;
        }
        /// <summary>
        /// 是否达到5层元素易伤条件
        /// 攻击生效时判定
        /// </summary>
        /// <param name="elementType"></param>
        /// <returns></returns>
        public bool IsReachLayer5(EElementType elementType)
        {
            return elementType == ElementType && _elementTickLayer == Layers;
        }
        public bool TryTickBuff(int damage, EElementType elementType, ICombatCalculation attacker)
        {
            if (elementType == EElementType.None)
                return false;
            var isReach = IsReachLayer5(elementType);
            var isSkill = false;
            var buffOrSkillId = isReach ? Table.BuffTypeNum[5] : Table.BuffTypeNum[0];
            var probability = isReach ? TableUtils.Multiple : Table.BuffTypeNum[1];
            var addLayer = 1;
            switch (BuffType)
            {
                case EBuffType.ElementAddFire:
                case EBuffType.ElementAddWind:
                    {
                        isSkill = isReach;
                    }
                    break;
                case EBuffType.ElementAddThunder:
                    {
                        isSkill = true;
                    }
                    break;
                case EBuffType.ElementAddIce:
                    {
                        if (!isReach)
                            return false;
                        // TODO 给主角加冰盾buff，
                        //probability = 0;
                    }
                    break;
                case EBuffType.ElementAddPoison:
                    {
                        addLayer = isReach ? Table.BuffTypeNum[5] : Layers;
                        buffOrSkillId = Table.BuffTypeNum[0];
                    }
                    break;
            }
            if (isSkill)
            {
                var calculation = Context.Calculation.GetSkillTrigger(buffOrSkillId, attacker);
                calculation?.TickSkill(buffOrSkillId, Context.Entity.EntityId, probability);
            }
            else
            {
                var calculation = Context.Calculation.GetBuffTrigger(buffOrSkillId, attacker);
                calculation?.TickBuff(buffOrSkillId, probability, addLayer, damage);
            }
            if (isReach)
            {
                Listener?.OnCompleted(TableId);
            }
            return true;
        }
    }
}
