using UnityEditor;
using UnityEngine;
using System.IO;

public class CustomConfigWindow : EditorWindow
{
    private string ipAddress = "127.0.0.1";
    private GameObject backpackPrefab;
    private string backpackButtonPath = "Assets";
    private string popupWindowPath = "Assets";

    [MenuItem("工具/配置编辑窗口")]
    public static void ShowWindow()
    {
        GetWindow<CustomConfigWindow>("配置编辑窗口");
    }

    private void OnGUI()
    {
        GUILayout.Label("配置设置", EditorStyles.boldLabel);

        // IP 地址输入
        GUILayout.Label("IP 地址");
        ipAddress = EditorGUILayout.TextField("IP 地址:", ipAddress);

        // 背包预制体选择
        GUILayout.Space(10);
        GUILayout.Label("选择背包预制体");
        backpackPrefab = (GameObject)EditorGUILayout.ObjectField("背包预制体:", backpackPrefab, typeof(GameObject), false);

        // 显示背包预制体信息并提供定位功能
        if (backpackPrefab != null)
        {
            if (GUILayout.Button($"定位预制体: {backpackPrefab.name}"))
            {
                EditorGUIUtility.PingObject(backpackPrefab);
            }
        }

        // 背包按钮路径
        GUILayout.Space(10);
        GUILayout.Label("背包按钮路径");
        if (GUILayout.Button("选择背包按钮路径"))
        {
            string path = EditorUtility.OpenFolderPanel("选择背包按钮路径", "Assets", "");
            if (!string.IsNullOrEmpty(path))
            {
                backpackButtonPath = GetRelativePath(path);
            }
        }
        EditorGUILayout.LabelField("路径:", backpackButtonPath);

        // 弹出窗口路径
        GUILayout.Space(10);
        GUILayout.Label("弹出窗口路径");
        if (GUILayout.Button("选择弹出窗口路径"))
        {
            string path = EditorUtility.OpenFolderPanel("选择弹出窗口路径", "Assets", "");
            if (!string.IsNullOrEmpty(path))
            {
                popupWindowPath = GetRelativePath(path);
            }
        }
        EditorGUILayout.LabelField("路径:", popupWindowPath);

        // 保存按钮
        GUILayout.Space(20);
        if (GUILayout.Button("保存配置"))
        {
            SaveConfiguration();
            EditorUtility.DisplayDialog("配置编辑窗口", "配置已保存", "确定");
        }

        // 更新按钮
        GUILayout.Space(20);
        GUI.backgroundColor = Color.green;
        if (GUILayout.Button("更新配置到 StreamingAssets", GUILayout.Height(40)))
        {
            UpdateConfiguration();
        }
        GUI.backgroundColor = Color.white;  // 还原默认颜色
    }

    private void SaveConfiguration()
    {
        Debug.Log("IP 地址: " + ipAddress);
        Debug.Log("背包预制体: " + (backpackPrefab != null ? backpackPrefab.name : "未选择"));
        Debug.Log("背包按钮路径: " + backpackButtonPath);
        Debug.Log("弹出窗口路径: " + popupWindowPath);
    }

    private void UpdateConfiguration()
    {
        string dataFolderPath = Path.Combine(Application.streamingAssetsPath, "data");
        string configFilePath = Path.Combine(dataFolderPath, "config.json");

        // 确保 data 文件夹存在
        if (!Directory.Exists(dataFolderPath))
        {
            Directory.CreateDirectory(dataFolderPath);
        }

        // 创建配置数据对象
        ConfigData configData = new ConfigData
        {
            ipAddress = ipAddress,
            backpackPrefabPath = backpackPrefab != null ? AssetDatabase.GetAssetPath(backpackPrefab) : "",
            backpackButtonPath = backpackButtonPath,
            popupWindowPath = popupWindowPath
        };

        // 将配置数据序列化为 JSON 并写入文件
        string json = JsonUtility.ToJson(configData, true);
        File.WriteAllText(configFilePath, json);

        EditorUtility.DisplayDialog("配置编辑窗口", "配置已更新到 StreamingAssets/data/config.json", "确定");
    }

    private string GetRelativePath(string fullPath)
    {
        if (fullPath.StartsWith(Application.dataPath))
        {
            return "Assets" + fullPath.Substring(Application.dataPath.Length);
        }
        return fullPath;
    }

    // 配置数据类，用于序列化为 JSON
    [System.Serializable]
    public class ConfigData
    {
        public string ipAddress;
        public string backpackPrefabPath;
        public string backpackButtonPath;
        public string popupWindowPath;
    }
}
