using System;
using System.Collections.Generic;
using System.Diagnostics;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using UnityEditor;
using UnityEngine;
using VOID.Generic.Objects.Item;

namespace VOID.Editor.EditorWindow.Objects
{
    public class ItemUniquenessGenerator : OdinEditorWindow
    {
        [ShowInInspector, ReadOnly] private List<GameObject> _wereDuplicated = new(); 
        [ShowInInspector, ReadOnly] private List<GameObject> _wereMissing = new(); 
        [ShowInInspector, ReadOnly] private List<GameObject> _untouched = new(); 
        
        [MenuItem("VOID RPG/Items/UniqueID generator")]
        private static void OpenWindow() => GetWindow<ItemUniquenessGenerator>().Show();
        
        [Button("GENERATE")]
        public void Generate()
        {
            EditorUtility.DisplayProgressBar("Generating unique ids...", "PREPARING", 0);
            
            _wereDuplicated.Clear();
            _wereMissing.Clear();
            _untouched.Clear();
            
            var stopwatch = Stopwatch.StartNew();
            var usedUniqueIds = new List<string>();
            var guids = AssetDatabase.FindAssets("t:prefab", new[] { "Assets/2 - Prefabs" });

            for (var i = 0; i < guids.Length; i++)
            {
                EditorUtility.DisplayProgressBar("Generating unique ids...", $"{i} / {guids.Length}", (float)i / guids.Length);
                var path = AssetDatabase.GUIDToAssetPath(guids[i]);
                var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                if (PrefabUtility.GetPrefabAssetType(prefab) is not PrefabAssetType.Regular) continue;
                var item = prefab.GetComponent<ItemObject>();
                if (!item) continue;
                
                if (item.itemData.uniqueId.IsNullOrWhitespace())
                {
                    // Missing UniqueId
                    _wereMissing.Add(prefab);
                    item.itemData.uniqueId = Guid.NewGuid().ToString();
                }
                else if (usedUniqueIds.Contains(item.itemData.uniqueId))
                {
                    // Duplicated UniqueId
                    _wereDuplicated.Add(prefab);
                    item.itemData.uniqueId = Guid.NewGuid().ToString();
                }
                else
                {
                    // Looks good
                    _untouched.Add(prefab);
                }

                usedUniqueIds.Add(item.itemData.uniqueId);
                PrefabUtility.SaveAsPrefabAsset(prefab, AssetDatabase.GetAssetPath(prefab));
            }

            EditorUtility.ClearProgressBar();
            EditorUtility.DisplayDialog(
                "Generating UniqueIDs finished",
                $"Generating finished in {stopwatch.ElapsedMilliseconds} ms.\n" +
                $"{_wereMissing.Count} - ItemObjects had missing UniqueID.\n" +
                $"{_wereDuplicated.Count} - ItemObjects had duplicated UniqueID.\n" +
                $"{_untouched.Count} - ItemObjects had good UniqueID.",
                "OK"
            );
        }
    }
}