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

namespace UnityEditor.UI
{
    public class UGUISelectSpriteAtlasWindow : EditorWindow
    {
        private static System.Action<SpriteAtlas, Sprite> callBack;
        private static string jsonPackPath;

        private static string spriteAtlasPath = "/Res/UIAtlas";
        float mClickTime = 0f;

        private static List<SpriteAtlas> currSelectSpriteAtlas = new List<SpriteAtlas>();
        private static Sprite currSelectSprite;

        private static PackInfo packInfo;

        public static void OpenWidow(PackInfo packInfo, Sprite sprite, List<SpriteAtlas> currSelectSpriteAtlas, System.Action<SpriteAtlas, Sprite> callBack)
        {
            UGUISelectSpriteAtlasWindow.packInfo = packInfo;
            UGUISelectSpriteAtlasWindow.currSelectSpriteAtlas = currSelectSpriteAtlas;
            currSelectSprite = sprite;
            jsonPackPath = packInfo.packgJsonPath;
            UGUISelectSpriteAtlasWindow.callBack = callBack;
            UGUISelectSpriteAtlasWindow window = EditorWindow.GetWindow<UGUISelectSpriteAtlasWindow>();

            window.name = "选择一个一个当前图片";
            window.Show();
        }


        Vector2 pos = Vector2.zero;


        private void GUISpriteAtlas()
        {
            if (currSelectSprite != null)
            {
                GUI.DrawTexture(new Rect(EditorGUIUtility.currentViewWidth / 2 - 64, 0, 128, 128), currSelectSprite.texture);
            }

            GUILayout.Space(150);
            GUILayout.Label($"请选择{currSelectSprite.name}需要使用的图集");
            pos = GUILayout.BeginScrollView(pos);
            if (currSelectSpriteAtlas != null)
            {
                for (int i = 0; i < currSelectSpriteAtlas.Count; i++)
                {
                    if (GUILayout.Button(currSelectSpriteAtlas[i].name))
                    {
                        GUILayout.EndScrollView();
                        callBack?.Invoke(currSelectSpriteAtlas[i], currSelectSprite);
                        Close();
                        return;
                    }
                }
            }


            GUILayout.EndScrollView();
        }


        void OnGUI()
        {
            GUISpriteAtlas();
        }
    }
}