using UnityEditor;
using UnityEngine;
 
public class FoldoutWindow : EditorWindow
{
    private static FoldoutWindow window;
    private static bool isOpen = true;
 
    [MenuItem("Test/FoldoutWidnow", false)]
    private static void OpenWindow()
    {
        window = GetWindow<FoldoutWindow>(true, "Foldout");
        window.position = new Rect(
            Screen.currentResolution.width / 2 - 150,
            Screen.currentResolution.height / 2 - 150,
            300,
            300
        );
        window.Show();
    }
 
    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        {
            isOpen = Foldout(isOpen, "Foldout");
        }
        EditorGUILayout.EndHorizontal();
 
        if (isOpen)
        {
            EditorGUILayout.BeginVertical();
            {
                // gui
                GUILayout.Label("Label1");
                GUILayout.Label("Label2");
                GUILayout.Label("Label3");
            }
            EditorGUILayout.EndVertical();
        }
 
    }
 
    public static bool Foldout(bool isOpen, string title)
    {
        // style
        var style = new GUIStyle("ShurikenModuleTitle");
        style.font = new GUIStyle(EditorStyles.label).font;
        style.border = new RectOffset(15, 7, 2, 2);
        style.fontStyle = FontStyle.Bold;
        style.fontSize = 12;
        style.fixedHeight = 26;
        style.fixedWidth = 300;
        style.contentOffset = new Vector2(20f, -2f);
 
        var rect = GUILayoutUtility.GetRect(16f, 26f, style);
        GUI.Box(rect, title, style);
 
        var e = Event.current;
 
        // ▼の位置
        var toggleRect = new Rect(rect.x + 4f, rect.y + 3f, 16f, 16f);
        if (e.type == EventType.Repaint)
        {
            EditorStyles.foldout.Draw(toggleRect, false, false, isOpen, false);
        }
 
        // action
        if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
        {
            isOpen = !isOpen;
            e.Use();
        }
 
        // menu
        GUILayout.FlexibleSpace();
        if (GUILayout.Button(GetIconTextureGUIContent("Assets/Icon/menuIcon.png"), GUIStyle.none))
        {
            // ここにメニューの内容を入れていく
            GenericMenu toolsMenu = new GenericMenu();
            toolsMenu.AddItem(new GUIContent("Copy"), false, Copy);
            toolsMenu.AddItem(new GUIContent("Paste"), false, Paste);
            toolsMenu.DropDown(new Rect(window.position.width - 20, 0, 0, 0));
        }
        GUILayout.Space(5);
 
        return isOpen;
    }
 
    // TextureをGUIContent形式に変換
    public static GUIContent GetIconTextureGUIContent(string imagePath)
    {
        GUIContent contents = new GUIContent();
        Texture iconTexture = (Texture)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));
        contents.image = iconTexture;
 
        return contents;
    }
 
    // GenericMenuで[Copy]を選択したときに実行される関数
    private static void Copy()
    {
        Debug.Log("Copy");
    }
 
    // GenericMenuで[Paste]を選択したときに実行される関数
    private static void Paste()
    {
        Debug.Log("Paste");
    }
 
}