using System;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor.Validation;
using Sirenix.Utilities;
using UnityEngine;
using VOID.Editor.AttributeValidator.Objects;
using VOID.Generic.Objects.Item;
using VOID.Generic.Objects.Usable;

[assembly: RegisterValidator(typeof(ItemObjectDataValidator))]

namespace VOID.Editor.AttributeValidator.Objects
{
    public class ItemObjectDataValidator : ValueValidator<ItemObjectData>
    {
        protected override void Validate(ValidationResult result)
        {
            if (Value == null) return;
            if (Property.Parent.ValueEntry.WeakSmartValue is not ItemObject itemObject) return;
            if (OdinPrefabUtility.GetPrefabKind(itemObject.gameObject) is not PrefabKind.Regular) return;
            
            IsStackableValid(result);
        }

        private void IsStackableValid(ValidationResult result)
        {
            var allowedTypes = new List<Type> { typeof(ItemObject), typeof(UsableObject) };
            if (!Value.stackable || allowedTypes.Contains(Property.ParentType)) return;

            var allowedTypeNames = string.Join(", ", allowedTypes.Select(t => t.GetNiceName()).ToList());
            result.AddError($"Stackable can be TRUE only for objects of type: {allowedTypeNames}")
                .WithFix(() => Value.stackable = false);
        }
    }
}