using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using WS;

///<summary>对象池定时销毁编辑器</summary>
public class GamePoolTimerEditor : EditorWindow
{
    [MenuItem("Tools/Framework/对象池定时销毁编辑")]
    public static void ShowVoice()
    {
        GamePoolTimerEditor window = EditorWindow.GetWindow<GamePoolTimerEditor>("对象池定时销毁编辑");
        window.Show();
    }

    ///<summary>所有数据</summary>
    private List<PoolClear> Config = new List<PoolClear>();
    ///<summary>路径</summary>
    private string path;
    ///<summary>滑杆</summary>
    private Vector2 pos;
    ///<summary>临时数据</summary>
    private PoolClear tmpdata = new PoolClear();

    private void OnEnable()
    {
        path = $"{Application.dataPath}/Resources/Config/Framework/PoolClear.txt";
        if (ConfigHelper.ConfigExists(path))
        {
            Config = ConfigHelper.FileLoadConfig<List<PoolClear>>(path);
        }
    }

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        DrawPoolClearView(tmpdata);
        if (GUILayout.Button("添加", GUILayout.Width(100)))
        {
            if (Config.Exists(p => p.Name == tmpdata.Name))
            {
                ShowNotification(new GUIContent($"物体:{tmpdata.Name} 已存在"), 2);
            }
            else
            {
                Config.Add(tmpdata);
                tmpdata = new PoolClear();
            }
        }
        EditorGUILayout.EndHorizontal();

        pos = EditorGUILayout.BeginScrollView(pos);
        for (int i = 0; i < Config.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            DrawPoolClearView(Config[i]);
            if (GUILayout.Button("删除", GUILayout.Width(100)))
            {
                Config.RemoveAt(i);
            }
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("保存", GUILayout.Width(100)))
        {
            ConfigHelper.SaveConfig(Config, path);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndScrollView();
    }

    ///<summary>绘制PoolClear</summary>
    private void DrawPoolClearView(PoolClear data)
    {
        GUILayout.Label("物体名称:", GUILayout.Width(60));
        data.Name = EditorGUILayout.TextField(data.Name);
        GUILayout.Label("持续保存数量:", GUILayout.Width(80));
        data.CullSave = EditorGUILayout.IntField(data.CullSave);
        GUILayout.Label("清理间隔时间:", GUILayout.Width(80));
        data.CullTime = EditorGUILayout.IntField(data.CullTime);
        GUILayout.Label("每次清理数量:", GUILayout.Width(80));
        data.CullNextCount = EditorGUILayout.IntField(data.CullNextCount);
    }
}
