using XGame.Editor.Asset.Addressable;
using XGame.Framework.Asset;
using XGame.Framework.Asset.Addressable;
using XGame.Framework.Asset.Addressable.Data;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Asset
{
    public class SingleAddressableAssetWindow : EditorWindow
    {
        [MenuItem("Assets/AddressableName", priority = 99)]
        private static void CreateWindow()
        {
            var activeObject = Selection.activeObject;
            var assetPath = AssetDatabase.GetAssetPath(activeObject);
            if (!AddressableHelper.TryAssetPathToClassify(assetPath, out var classify))
            {
                return;
            }
            //检查选中的资源是否有配置记录
            var manifest = AddressableHelper.LoadAssetManifest(classify);
            if (manifest == null)
            {
                Debug.LogError($"Can't find manifest file. Path: {PathDefine.ProductAssetManifestPath}");
                return;
            }
            var assetInfos = manifest.GetAssetInfos();
            var guid = AssetDatabase.AssetPathToGUID(assetPath);
            var index = assetInfos.FindIndex((info => info.assetGUID.Equals(guid)));
            if (index < 0)
            {
                Debug.LogError($"Can't find AddressableAssetInfo. Please select a valid object. Path:{assetPath} File:{Path.GetFileNameWithoutExtension(assetPath)}");
                return;
            }
            var window = EditorWindow.GetWindow<SingleAddressableAssetWindow>(true, "AddressableNameEditor");
            window.Show();
            window.Init(manifest, classify, assetInfos[index]);
        }
        [MenuItem("Assets/AddressableName", true, priority = 99)]
        private static bool VerifyCreateWindow()
        {
            var path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (string.IsNullOrEmpty(path) || Directory.Exists(path))
            {
                return false;
            }
            return (!FileUtil.IsFileIgnore(path) && AddressableHelper.IsValidPath(path));
        }

        private AddressableAssetFile assetFile;
        private AddressableInfosSo manifest;
        private AddressableClassify classifyOperation;

        private void Init(AddressableInfosSo manifest, AddressableClassify classify, AssetInfo assetInfo)
        {
            var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
            //if (!AddressableHelper.TryAssetPathToClassify(assetPath, out var classify))
            //{
            //    return;
            //}
            this.manifest = manifest;
            classifyOperation = classify;
            //var assetInfos = manifest.GetAssetInfos();
            //var guid = AssetDatabase.AssetPathToGUID(assetPath);
            //var index = assetInfos.FindIndex((info => info.assetGUID.Equals(guid)));

            assetFile?.Clear();
            assetFile = new AddressableAssetFile(assetPath, assetInfo)
            {
                IsFoldout = true
            };
            assetFile.OnApply += OnApplyFile;
        }

        //private void OnEnable()
        //{

        //}
        private void OnDisable()
        {
            assetFile?.Clear();
            manifest = null;
        }

        private void OnGUI()
        {
            assetFile?.GUI();
        }

        private bool OnApplyFile(ApplyEvnetContext context)
        {
            if (classifyOperation == AddressableClassify.BuiltIn)
            {
                Alert("内置资源禁止修改AddressableName.");
                return false;
            }
            if (classifyOperation == AddressableClassify.Backup)
            {
                Alert("禁止修改备份文件.");
                return false;
            }
            return AddressableHelper.TryApplyAddressableName(manifest, context.assetGUID, context.addressableName);
            //return manifest?.TryApplyAddressableName(guid, addressableName) ?? false;
        }
        private void Alert(string content)
        {
            EditorWindow.focusedWindow.ShowNotification(new GUIContent(content));
        }
    }

}
