﻿using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEditor;

public class LoadMapWindow : SearchableEditorWindow {

    public static List<string> mapNames;
    public Vector2 scrollSpot;

    [MenuItem("Battle Royale/Custom Map Tools/Load Map", false, 4)]
    public static void LoadMap()
    {
        LoadMapWindow window = GetWindow<LoadMapWindow>("Map Loader");
        if (FindMaps())
        {
            window.minSize = new Vector2(320, 500);
	        window.maxSize = window.minSize;
	        
	        DebugChannelSO.OverrideRaise(
		        "LoadMapWindow", 
		        mapNames.Count + " custom scene" + (mapNames.Count > 1 ? "s" : "") + " found!", 
		        Color.magenta);
        }
        else
        {
            window.minSize = new Vector2(320, 236);
	        window.maxSize = window.minSize;
            
	        DebugChannelSO.OverrideRaise(
		        "LoadMapWindow", 
		        "No custom scenes found!", 
		        Color.magenta);
        }
    }

    public static bool FindMaps()
    {
        string[] tempNames = Directory.GetFiles(Application.dataPath + "/Scenes");
        mapNames = new List<string>();

        if (tempNames.Length < 1)
            return false;

        foreach(string map in tempNames)
        {
            if (!map.Contains(".meta") && map.Contains(".unity"))
            {
                #if UNITY_EDITOR_WIN
                string mapName = map.Substring(map.LastIndexOf('\\') + 1);
                #else
                string mapName = map.Substring(map.LastIndexOf('/') + 1);
                #endif
                mapNames.Add(mapName.Replace(".unity", ""));
            }
        }

	    return mapNames.Count > 0;
    }

    void OnGUI()
    {
        GUILayout.Space(6);
        GUILayout.BeginHorizontal();
        #region

        GUILayout.Space(6);
        GUILayout.BeginVertical();
        #region

        GUILayout.BeginVertical("Select Map to Load", "window", GUILayout.Width(position.width - 12), GUILayout.Height(position.height - 12));
        #region

        if (FindMaps())
        {
            scrollSpot = GUILayout.BeginScrollView(scrollSpot, GUILayout.Width(position.width - 24), GUILayout.Height(472));

            for(int i = 0; i < mapNames.Count; i++)
            {
                if (GUILayout.Button(mapNames[i]))
                {
                    if (SceneManager.GetActiveScene().name != mapNames[i])
                    {
                        EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();

                        EditorSceneManager.OpenScene("Assets/Scenes/" + mapNames[i] + ".unity", OpenSceneMode.Single);
                    
	                    DebugChannelSO.OverrideRaise(
		                    "LoadMapWindow", 
		                    "Opening " + mapNames[i], 
		                    Color.magenta);
                    }

                    this.Close();
                }
            }

            GUILayout.EndScrollView();
        }
        else
        {
            GUILayout.Label("Uh oh! We couldn't find any Custom Map scenes.", EditorStyles.wordWrappedLabel);

            GUILayout.Space(10);

            GUILayout.Label("Make sure your scenes are placed in the Scenes folder.", EditorStyles.wordWrappedLabel);

            GUILayout.Space(10);

            GUILayout.Label("You can access the folder by clicking 'Window' at the top of your screen, then 'General -> Project'. The Scenes folder can be found in the default 'Assets' directory.", EditorStyles.wordWrappedLabel);

            GUILayout.Space(10);

            GUILayout.Label("Ask your instructor for help moving your scenes, or you may contact one of our Tech Support agents by clicking the button below:", EditorStyles.wordWrappedLabel);

            GUILayout.Space(10);

            if (GUILayout.Button("Black Rocket Support"))
            {
                Application.OpenURL("https://www.blackrocket.com/help");
            }
        }

        #endregion
        GUILayout.EndVertical();

        #endregion
        GUILayout.EndVertical();
        GUILayout.Space(6);

        #endregion
        GUILayout.EndHorizontal();
        GUILayout.Space(6);
    }
}
