using Sirenix.OdinInspector;
using UnityEngine;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Abstract.Events;
using VOID.Generic.Util;

namespace VOID.Generic.Status.Effect.Damage
{
    [TypeRegistryItem("Apply Damage")]
    public class ApplyDamageEffect : BasicEffect
    {
        [InfoBox(
            "<b>" + nameof(OnApply) + "</b> - only once when effect is applied\n" +
            "<b>" + nameof(OnEnd) + "</b> - only once when effect is ended normally\n" +
            "<b>" + nameof(OnTurnSelfEnd) + "</b> - everytime when unit ends self turn\n" +
            "<b>" + nameof(OnTurnSelfStart) + "</b> - everytime when unit starts self turn\n" +
            "<b>" + nameof(OnCancel) + "</b> - only once when effect is canceled or ended forcefully"
            )]
        private enum DamageMoment
        {
            OnApply,
            OnEnd,
            OnTurnSelfEnd,
            OnTurnSelfStart,
            OnCancel
        }

        [SerializeField] private DamageMoment _damageMoment;
        [SerializeField, Required, MinValue(0)] private int _amountMin;
        [SerializeField, Required, MinValue(nameof(_amountMin))] private int _amountMax;
        [SerializeField, Required] private DamageType _type;
        
        public override void OnApply() => DoDamage(DamageMoment.OnApply);
        public override void OnEnd() => DoDamage(DamageMoment.OnEnd);
        public override void OnTurnSelfEnd() => DoDamage(DamageMoment.OnTurnSelfEnd);
        public override void OnTurnSelfStart() => DoDamage(DamageMoment.OnTurnSelfStart);
        public override void OnCancel() => DoDamage(DamageMoment.OnCancel);

        private void DoDamage(DamageMoment thisDamageMoment)
        {
            if (thisDamageMoment != _damageMoment) return;
            
            forObject.TakeDamage(new DamageEvent
            {
                target = forObject,
                attacker = source,
                amount = RandomUtil.RandomInteger(_amountMin, _amountMax),
                damageType = _type
            });
        }

        public override string GetSimpleDescription() => $"{_amountMin} - {_amountMax} ({_type})";
    }
}