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

[CustomEditor(typeof(MyImage), true)]
[CanEditMultipleObjects]
public class MyImageInspector : ImageEditor
{
    public override void OnInspectorGUI()
    {
        MyImage image = target as MyImage;
        RectTransform rect = image.GetComponent<RectTransform>();


        if (image.packInfo != null && !string.IsNullOrEmpty(image.packInfo.packgJsonPath))
        {
            GUILayout.BeginHorizontal();
            if (GUILayout.Button(image.packInfo.packName))
            {
            }

            if (GUILayout.Button("x"))
            {
                image.packInfo = null;
                image.CurrSpriteAtlas = null;
                image.sprite = null;
                image.icon_name = null;
                EditorUtility.SetDirty(image);
            }

            GUILayout.EndHorizontal();
        }
        else
        {
            if (GUILayout.Button("选择图集"))
            {
                UGUIFunctiontemplateWindow.OpenWidow(delegate(PackInfo s)
                {
                    image.packInfo = s;
                    image.CurrSpriteAtlas = UGUICacheInfo.GetSpriteAtlas(image.packInfo.packName);
                    image.ReashUI();
                });
                EditorUtility.SetDirty(image);
            }
        }


        if (image.packInfo != null && !string.IsNullOrEmpty(image.packInfo.packName))
        {
            string buttonName = string.IsNullOrEmpty(image.icon_name) ? "选择图片" : image.icon_name;

            GUILayout.BeginHorizontal();


            if (!string.IsNullOrEmpty(image.icon_name))
            {
                List<SpriteAtlas> allSpriteAtlas = UGUICacheInfo.GetAllSpriteAtlas(image.icon_name);
                if (allSpriteAtlas.Count > 0)
                {
                    if (GUILayout.Button("切换图集"))
                    {
                        UGUISelectSpriteAtlasWindow.OpenWidow(image.packInfo, image.sprite, allSpriteAtlas,
                            delegate(SpriteAtlas atlas, Sprite sprite)
                            {
                                if (atlas != null)
                                {
                                    image.CurrSpriteAtlas = atlas;
                                    image.ReashUI();
                                }
                            });
                    }
                }
            }

            if (GUILayout.Button(buttonName))
            {
                UGUISpriteAtlasSelectWindow.OpenWidow(image.packInfo, delegate(SpriteAtlas atlas, Sprite sprite)
                {
                    image.CurrSpriteAtlas = atlas;
                    if (atlas == null)
                    {
                        image.sprite = sprite;
                    }

                    image.icon_name = sprite.name;
                    EditorUtility.SetDirty(image);
                });
            }

            if (GUILayout.Button("跳转"))
            {
                string[] p = AssetDatabase.FindAssets(image.icon_name);
                Selection.objects = null;
                List<Object> allObject = new List<Object>();
                for (int i = 0; i < p.Length; i++)
                {
                    Object obj = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(new GUID(p[i])));
                    if (obj.name.Equals(image.icon_name))
                    {
                        allObject.Add(obj);
                    }
                }

                Selection.objects = allObject.ToArray();
                // UGUISpriteAtlasSelectWindow.OpenWidow(image.packInfo, delegate(SpriteAtlas atlas, Sprite sprite)
                // {
                //     image.CurrSpriteAtlas = atlas;
                //     if (atlas == null)
                //     {
                //         image.sprite = sprite;
                //     }
                //
                //     image.icon_name = sprite.name;
                //     EditorUtility.SetDirty(image);
                // });
            }

            GUILayout.EndHorizontal();
        }


        EditorGUI.BeginChangeCheck();
        image.isNotLoadDeftIcon = GUILayout.Toggle(image.isNotLoadDeftIcon, "isNotLoad");

        if (EditorGUI.EndChangeCheck())
        {
            EditorUtility.SetDirty(image);
            AssetDatabase.SaveAssets();
        }

        base.OnInspectorGUI();
        serializedObject.ApplyModifiedProperties();
        // EditorUtility.SetDirty(image);
    }
}