using System;
using Sirenix.OdinInspector;
using UnityEngine;
using VOID.Generic.Dict;
using VOID.ScriptableObjects.Affiliation;

namespace VOID.Generic.Objects.Unit
{
    [Serializable]
    public class UnitObjectAttitude : ObjectComponent<UnitObject>
    {
        [Required]
        [SerializeField]
        public Faction faction;

        public AttitudeType GetAttitudeWith(UnitObject targetUnit)
        {
            var targetFaction = targetUnit.unitAttitude.faction;

            if (!faction || !targetFaction)
            {
                Debug.LogWarning($"This unit ({parent.name}) and/or another unit ({targetUnit.name}) has no faction selected! Using neutral!", parent);
                return AttitudeType.Neutral;
            }
            
            var targetAttitude = targetFaction.GetAttitudeFor(faction);
            var thisAttitude = faction.GetAttitudeFor(targetFaction);

            // Both have attitudes toward themselves - get worse one
            if (thisAttitude.HasValue && targetAttitude.HasValue)
                return thisAttitude.Value > targetAttitude.Value ? targetAttitude.Value : thisAttitude.Value;

            // Only this unit have attitude towards target unit
            if (thisAttitude.HasValue)
                return thisAttitude.Value;

            // Only target unit have attitude towards this unit
            if (targetAttitude.HasValue)
                return targetAttitude.Value;

            return AttitudeType.Neutral;
        }
    }
}
