using System.IO;
using SGModule.Common.Helper;
using UnityEditor;
using UnityEngine;

namespace SGModule.Editor {
    public class NetworkConfigCreator : EditorWindow {
        private const string DefaultHost = "sandbox-api.swhitegames.tech";
        private const string ConfigFileName = "NetworkConfig";

        public bool showNetworkLog = true;
        public string debugHost = DefaultHost;
        public string releaseHost = "";

        public ConnectionMode model = ConnectionMode.Http;

        private void OnGUI() {
            showNetworkLog = EditorGUILayout.Toggle("显示日志", showNetworkLog);

            debugHost = EditorGUILayout.TextField("Debug Host:", debugHost);
            releaseHost = EditorGUILayout.TextField("Release Host:", releaseHost);

            model = (ConnectionMode) EditorGUILayout.EnumPopup("连接模式：", model);


            if (GUILayout.Button("Create NetworkConfig")) {
                if (string.IsNullOrEmpty(debugHost) || string.IsNullOrEmpty(releaseHost)) {
                    EditorUtility.DisplayDialog("请填写正确配置",
                        $"Debug Host {(string.IsNullOrEmpty(debugHost) ? "不存在" : "存在")},  Release Host {(string.IsNullOrEmpty(releaseHost) ? "不存在" : "存在")}",
                        "确定");
                    return;
                }

                CreateNetworkConfig();
                Close();
            }
        }

        [MenuItem("SwhiteGames/Create NetworkConfig")]
        public static void ShowWindow() {
            GetWindow<NetworkConfigCreator>("Create NetworkConfig");
        }

        private void CreateNetworkConfig() {
            // 确保 Resources 文件夹存在
            var resourcesPath = Path.Combine("Assets", "Resources");
            if (!Directory.Exists(resourcesPath)) {
                Directory.CreateDirectory(resourcesPath);
                AssetDatabase.Refresh(); // 刷新资源数据库
                Log.Common.Info("Resources 文件夹已创建。");
            }

            // 检查同名文件
            var assetPath = Path.Combine(resourcesPath, ConfigFileName + ".asset");
            if (File.Exists(assetPath)) {
                // 如果同名文件已存在，弹出提示并结束逻辑
                EditorUtility.DisplayDialog("文件已存在", $"配置文件 '{ConfigFileName}' 已存在于 Resources 文件夹中。", "确定");
                return;
            }
            // 检查同名文件
            // string assetPath = Path.Combine(resourcesPath, _configFileName + ".asset");
            // string uniqueAssetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath); // 生成唯一路径

            // 创建并设置 config 实例
            var networkConfig = CreateInstance<NetworkConfig>();
            networkConfig.showNetworkLog = showNetworkLog;
            networkConfig.debugHost = debugHost;
            networkConfig.releaseHost = releaseHost;
            networkConfig.connectionMode = model;

            // 将 ScriptableObject 实例保存到 Resources 文件夹中
            AssetDatabase.CreateAsset(networkConfig, assetPath);
            AssetDatabase.SaveAssets();

            Log.Common.Info("NetworkConfig 配置文件已成功创建在 " + assetPath);
        }
    }
}
