using UnityEngine;
using XGame.Database;
using XGame.Framework.Components;

namespace FL.Battle.Components
{
    public interface IEpigraphicsContext
    {
        IEntity Entity { get; }
        int EpigraphId { get; }
        ITargetSelector Selector { get; }
    }
    /// <summary>
    /// 铭文组件
    /// 只有随从可以装配，且只能装配一个
    /// </summary>
    public class EpigraphicsComponent : Component<IEpigraphicsContext>
    {
        protected override void OnDispose()
        {
        }
        /// <summary>
        /// 是否铭文的目标debuff
        /// </summary>
        /// <param name="buffId"></param>
        /// <returns></returns>
        public bool IsTargetBuff(int buffId, out int epigrapId)
        {
            if (Context.EpigraphId == 20300)
            {
                var table = EpigraphTableRepo.Get(Context.EpigraphId);
                if (buffId == table.EffectArgs[0])
                {
                    epigrapId = Context.EpigraphId;
                    return true;
                }
            }
            epigrapId = 0;
            return false;
        }
        /// <summary>
        /// 尝试触发远古毒素
        /// 冰->毒
        /// </summary>
        /// <param name="targetId"></param>
        /// <param name="skillElementType"></param>
        /// <param name="damage"></param>
        /// <returns></returns>
        public bool TryTickIceToPoison(long targetId, EElementType skillElementType, int damage)
        {
            //EffectArgs: [目标buffId,层数,伤害转吸血万分比,不满足层数时buff触发概率,buffid1,buffid2,buffid3]
            if (Context.EpigraphId != 20300 || skillElementType != EElementType.Ice)
                return false;
            var target = Context.Selector.GetTarget(targetId);
            if (target == null) return false;
            var table = EpigraphTableRepo.Get(Context.EpigraphId);
            var tarBuffId = table.EffectArgs[0];
            var tarLayers = target.Buffs.GetLayers(tarBuffId);
            if (tarLayers == 0)
                return false;
            if (tarLayers == table.EffectArgs[1])
            { // 触发5层特效
                // 吸血比例，吸血给玩家
                var suckBloodPer = table.EffectArgs[2];
                var player = Context.Selector.GetPlayer();
                player.Calculation.Damage(Mathf.Abs(damage * suckBloodPer), EElementType.None);
                return true;
            }
            if (MathUtils.Random(table.EffectArgs[3]) == false)
            {
                return false;
            }
            var buffId1 = table.EffectArgs[4];
            var buffId2 = table.EffectArgs[5];
            var buffId3 = table.EffectArgs[6];
            target.Calculation.TickBuff(buffId1);
            target.Calculation.TickBuff(buffId2);
            target.Calculation.TickBuff(buffId3);
            return true;
        }
    }
}
