using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI.PackgTool;

namespace UnityEditor.UI
{
    public class UGUIFunctiontemplateWindow : EditorWindow
    {
        private static System.Action<PackInfo> callBack;

        public static void OpenWidow(System.Action<PackInfo> callBack)
        {
            UGUIFunctiontemplateWindow.callBack = callBack;
            EditorWindow.GetWindow<UGUIFunctiontemplateWindow>().Show();
        }

        public string functionPathJson = "/Art/UIAssets/TextrueJson";
        Vector2 pos = Vector2.zero;

        private string _currSelectName;
        private void OnGUI()
        {
            string path = Application.dataPath + functionPathJson;
            string[] allPath = System.IO.Directory.GetFiles(path, "*.txt");
            List<TextrueJson> gameAllModel = new List<TextrueJson>();
            for (int i = 0; i < allPath.Length; i++)
            {
                string jsonPath = File.ReadAllText(allPath[i]);
                TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(jsonPath);
                gameAllModel.Add(textrueJson);
            }

            _currSelectName=  GUILayout.TextArea(_currSelectName,"筛选" );
            
            pos = GUILayout.BeginScrollView(pos);
            for (int i = 0; i < gameAllModel.Count; i++)
            {
                if (!string.IsNullOrEmpty(_currSelectName)&&_currSelectName!=""&&!gameAllModel[i].packName.Contains(_currSelectName))
                {
                    continue;
                }

                if (GUILayout.Button(gameAllModel[i].packName))
                {
                    Close();
                    PackInfo packInfo=  UGUICacheInfo.GetPackDB(gameAllModel[i].packName);
                    if (packInfo == null)
                    {
                        packInfo= UGUICacheInfo.AddNewPackInfo(allPath[i].Replace(Application.dataPath, ""), gameAllModel[i]);
           
                    }

                    callBack?.Invoke(packInfo);
                }
            }

            GUILayout.EndScrollView();
        }
    }
}