using System;
using System.Collections;
using System.Linq;
using Sirenix.OdinInspector;
using VOID.Generic.Dict;
using VOID.ScriptableObjects.World;

namespace VOID.Generic.World
{
    [Serializable]
    public class AreaConnection
    {
        [HorizontalGroup("Row1")]
        [ReadOnly]
        public Direction2D direction;
        
        [HorizontalGroup("Row1")]
        [DisableIf(nameof(type), AreaConnectionType.Inner)]
        [OnValueChanged(nameof(OnConnectionTypeChange))]
        [ValueDropdown(nameof(GetConnectionTypes))]
        public AreaConnectionType type;

        [HorizontalGroup("Row2")]
        [DisableIf(nameof(IsNotEditable))]
        public AreaConnectionTag tag;
        
        [HorizontalGroup("Row2")]
        [DisableIf(nameof(IsNotEditable))]
        [MinValue(0)]
        public int floor;
        
        private void OnConnectionTypeChange()
        {
            if (IsNotEditable())
            {
                tag = null;
                floor = 0;
            }
        }

        private bool IsNotEditable() => type is AreaConnectionType.Inner or AreaConnectionType.None;
        
        private IEnumerable GetConnectionTypes() => Enum.GetValues(typeof(AreaConnectionType)).Cast<AreaConnectionType>()
            .Where(ct => ct != AreaConnectionType.Inner)
            .Select(ct => new ValueDropdownItem(Enum.GetName(typeof(AreaConnectionType), ct), (int)ct));
    }
}