﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;

namespace AsmodeeDigital.PlayReal.Plugin.Editor
{
    [InitializeOnLoad]
    public class PlayRealMenu
    {
#if UNITY_EDITOR_WIN
        public static string BuildPath = "..\\Builds\\PlayReal\\Windows\\AsmodeeDigital_PlayReal_Sample.exe";
#elif UNITY_EDITOR_OSX
        public static string BuildPath = "Builds/OSX/PlayReal/AsmodeeDigital_PlayReal_Sample.app";
#endif

        static PlayRealMenu()
        {
            EditorApplication.playmodeStateChanged += OnPlayModeChanged;
        }

        [MenuItem("PlayReal/Launch 2 players")]
        private static void Launch2Players()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);

            path = Path.Combine(Directory.GetParent(path).Parent.Parent.FullName, BuildPath);

#if UNITY_EDITOR_WIN
            Process.Start(path, "user1");
            Process.Start(path, "user2");
#elif UNITY_EDITOR_OSX
            Process.Start(new ProcessStartInfo("open","-n " + path + " --args " + "user1"));
            Process.Start(new ProcessStartInfo("open","-n " + path + " --args " + "user2"));
#endif
        }

        [MenuItem("PlayReal/Launch 2 players and editor")]
        private static void Launch2PlayersAndEditor()
        {
            Launch2Players();

            EditorPrefs.SetBool("loadedFromDebugMenu", true);
            EditorPrefs.SetString("previousScene", EditorSceneManager.GetActiveScene().path);

            EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
            EditorSceneManager.OpenScene("Assets/Samples/PlayReal/Scenes/1_MainMenu.unity", OpenSceneMode.Single);

            EditorApplication.isPlaying = true;
        }

        [MenuItem("PlayReal/Build and launch 2 players and editor &b")]
        private static void BuildAndLaunch2Players()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);

            path = Path.Combine(Directory.GetParent(path).Parent.Parent.FullName, BuildPath);

            string[] levels = new string[] {
            "Assets/Samples/PlayReal/Scenes/1_MainMenu.unity",
            "Assets/Samples/PlayReal/Scenes/2_Lobby.unity",
            "Assets/Samples/PlayReal/Scenes/3_Game.unity"};

#if UNITY_EDITOR_WIN
            BuildPipeline.BuildPlayer(levels, path, BuildTarget.StandaloneWindows, BuildOptions.None);
#elif UNITY_EDITOR_OSX
            BuildPipeline.BuildPlayer(levels, path, BuildTarget.StandaloneOSXUniversal, BuildOptions.None);
#endif

            Launch2PlayersAndEditor();
        }

        private static void OnPlayModeChanged()
        {
            bool loadedFromDebugMenu = EditorPrefs.GetBool("loadedFromDebugMenu", false);
            string previousScene = EditorPrefs.GetString("previousScene", string.Empty);

            if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode && loadedFromDebugMenu)
            {
                EditorApplication.isPlaying = false;
                EditorPrefs.SetBool("loadedFromDebugMenu", false);

                EditorSceneManager.OpenScene(previousScene);
            }
        }
    }
}
