﻿using ARGOFoundation;
using UnityEditor;
using UnityEngine;

namespace ARGOEditor
{
    public static class MenuHandler
    {
        // only constants are allowed for menu items
        private const string SignInTitle = "ARGO/Sign in";
        private const string NewSceneTitle = "ARGO/New Scene";
        private const string ResetSceneTitle = "ARGO/Reset Scene";
        private const string BuildAndDeploySceneTitle = "ARGO/Build And Deploy";
        private const string SignOutTitle = "ARGO/Sign out";
        private const string ResetLabel = "Reset";
        private const string CancelLabel = "Cancel";
        private const string ResetSceneMessage = "Are you sure you want to reset the current ARGO scene";

        [MenuItem(SignInTitle)]
        public static void SignIn()
        {
#if !ARGO_DEBUG
            AGLogger.SetLogType(5);
#endif
            var loginWindow = EditorWindow.GetWindow(typeof(LoginWindow), true, "Sign in");
            loginWindow.minSize = new Vector2(215f, 300f);
            var screenSize = Screen.currentResolution;
            loginWindow.maxSize = new Vector2(screenSize.width, screenSize.height);
            loginWindow.Show();
        }

        [MenuItem(NewSceneTitle)]
        public static void CreateScene()
        {
#if !ARGO_DEBUG
            AGLogger.SetLogType(5);
#endif
            if (IsSceneExist())
                if (!EditorUtility.DisplayDialog("ARGO scene already exists",
                    $"{ResetSceneMessage} and create a new one?", ResetLabel, CancelLabel)) return;
            var createWindow = EditorWindow.GetWindow(typeof(CreateWindow), true, "Create New Scene");
            createWindow.maxSize = new Vector2(215f, 300f);
            createWindow.Show();
        }

        [MenuItem(ResetSceneTitle)]
        public static void CleanScene()
        {
            if (!EditorUtility.DisplayDialog("Reset ARGO scene", $"{ResetSceneMessage}?", ResetLabel, CancelLabel)) return;
            SceneHolder.DestroyScene();
        }

        [MenuItem(BuildAndDeploySceneTitle)]
        public static void BuildScene()
        {
            if (!IsSceneExist()) return;
            var buildWindow = EditorWindow.GetWindow(typeof(BuildWindow), true, "Build Scene");
            buildWindow.minSize = new Vector2(215f, 300f);
            buildWindow.Show();
        }

        [MenuItem(SignOutTitle)]
        public static void SignOut()
        {
#if !ARGO_DEBUG
            AGLogger.SetLogType(5);
#endif
            UserSettings.Reset();
        }

        [MenuItem(ResetSceneTitle, true), MenuItem(BuildAndDeploySceneTitle, true)]
        private static bool IsSceneExist()
        {
            return SceneHolder.GetScene() != null;
        }

        [MenuItem(SignInTitle, true)]
        private static bool IsSignedOut()
        {
            return !IsSignedIn();
        }

        [MenuItem(SignOutTitle, true), MenuItem(NewSceneTitle, true)]
        private static bool IsSignedIn()
        {
            return UserSettings.IsSettingsExist();
        }
    }
}
