﻿using JLGames.RocketDriver.Editor.Windows.GettingStarted;
using JLGames.RocketDriver.Editor.Windows.Tools;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace JLGames.RocketDriver.Editor.Windows
{
    public static class WindowsUtil
    {
        public static void ShowWindow<T>() where T : EditorWindow
        {
            var panel = EditorWindow.GetWindow<T>();

            if (panel is IInitWindow) ((IInitWindow)panel).InitWindow();


            if (panel is IShowWindow) ((IShowWindow)panel).ShowWindow();
            else panel.ShowUtility();
        }

        public static void ShowGettingStarted(bool firstTime)
        {
            if (firstTime)
            {
                var notShow = EditorPrefs.GetBool(WinGettingStartedConst.EditorKeyNotShow);
                if (notShow) return;
            }

            if (WinGettingStarted.OnShow) return;
            ShowGettingStartedPrivate();
        }

        public static void CloseGettingStarted()
        {
            if (!WinGettingStarted.OnShow) return;
            CloseGettingStartedPrivate();
        }

        public static void ShowMainEditor(int index = 0)
        {
            var wnd = EditorWindow.GetWindow<Main.MainEditor>();
            wnd.titleContent = new GUIContent("MainEditor");
            wnd.ShowUtility();
            wnd.ShowTab(index);
        }

        public static void CloseMainEditor()
        {
            var wnd = EditorWindow.GetWindow<Main.MainEditor>();
            wnd.Close();
            Object.DestroyImmediate(wnd);
        }

        public static void ShowWinToolFindLayer()
        {
            var wnd = EditorWindow.GetWindow<WinFindLayer>();
            wnd.titleContent = new GUIContent("FindLayer");
            wnd.ShowUtility();
        }

        public static void CloseWinToolFindLayer()
        {
            var win = EditorWindow.GetWindow<WinFindLayer>();
            win.Close();
            Object.DestroyImmediate(win);
        }

        private static void ShowGettingStartedPrivate()
        {
            var wnd = EditorWindow.GetWindow<WinGettingStarted>();
            wnd.minSize = wnd.maxSize = WinGettingStartedConst.Size;
            wnd.ShowUtility();
        }

        private static void CloseGettingStartedPrivate()
        {
            var wnd = EditorWindow.GetWindow<WinGettingStarted>();
            wnd.Close();
            Object.DestroyImmediate(wnd);
        }
    }
}