using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Sirenix.Utilities;
using Test.Util;
using UnityEditor;
using UnityEngine;
using VOID.ScriptableObjects;

namespace Test.EditMode.GameData.Generic
{
    public class AbilityQualityTest
    {
        private List<AbilityQuality> _qualities = new();

        [OneTimeSetUp]
        public void Setup()
        {
            AssetDatabase.FindAssets($"t:{nameof(AbilityQuality)}").ForEach(guid =>
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                var abilityQuality = AssetDatabase.LoadAssetAtPath<AbilityQuality>(path);

                _qualities.Add(abilityQuality);
            });
        }

        [OneTimeTearDown]
        public void Cleanup()
        {
            _qualities.Clear();
            _qualities = null;
        }

        [Test]
        public void Index_NonNegative()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                if (quality.index >= 0) return;
                Debug.LogError($"ScriptableObject: {assetHyperLink} index must be non negative.", quality);
            });
        }

        [Test]
        public void Index_Unique()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                var other = _qualities.FirstOrDefault(other => other != quality && other.index == quality.index);
                if (!other) return;
                var otherPath = AssetDatabase.GetAssetPath(other.GetInstanceID());
                var otherHyperLink = DebugLogUtil.Hyperlink(otherPath);
                Debug.LogError($"ScriptableObject: {assetHyperLink} and {otherHyperLink} have same indexes", quality);
            });
        }

        [Test]
        public void DisplayName_IsPresent()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                if (quality.displayName.IsNullOrWhitespace())
                    Debug.LogError($"ScriptableObject: {assetHyperLink} description is empty.", quality);
            });
        }

        [Test]
        public void Color_IsPresent()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                if (quality.color == default)
                    Debug.LogError($"ScriptableObject: {assetHyperLink} color is not selected.", quality);
            });
        }

        [Test]
        public void Background_IsPresent()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                if (!quality.background)
                    Debug.LogError($"ScriptableObject: {assetHyperLink} background is not selected.", quality);
            });
        }

        [Test]
        public void Border_IsPresent()
        {
            _qualities.ForEach(quality =>
            {
                var assetPath = AssetDatabase.GetAssetPath(quality.GetInstanceID());
                var assetHyperLink = DebugLogUtil.Hyperlink(assetPath);
                if (!quality.border)
                    Debug.LogError($"ScriptableObject: {assetHyperLink} background is not selected.", quality);
            });
        }
    }
}

