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

public class DeleteMapWindow : SearchableEditorWindow
{

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

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

    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;
    }

    public static bool FindPrefab(string prefabName)
    {
        GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Do Not Edit OR Delete/Terrains/Prefabs/In Use/Scene Setup/Default Level Prefab.prefab", typeof(GameObject));

	    return prefab;
    }


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

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

        GUILayout.BeginVertical("Select Map to Delete", "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])
                    {
                        if (EditorUtility.DisplayDialog("Delete Current Scene", "The map you have selected is currently open. Are you sure you want to delete this scene?", "Yes", "No"))
                        {
                            AssetDatabase.DeleteAsset("Assets/Scenes/" + mapNames[i] + ".unity");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + ".unity", 
		                        Color.red);

                            EditorSceneManager.OpenScene("Assets/Do Not Edit OR Delete/Core Scenes/Tutorial Island.unity", OpenSceneMode.Single);

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Opening Tutorial Island", 
		                        Color.red);
		                        
                            AssetDatabase.DeleteAsset("Assets/Do Not Edit OR Delete/Terrains/" + mapNames[i] + " Terrain.asset");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + " Terrain.asset", 
		                        Color.red);

                            AssetDatabase.DeleteAsset("Assets/Levels/" + mapNames[i] + ".prefab");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + ".prefab", 
		                        Color.red);

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "All files for " + mapNames[i] + " successfully deleted!", 
		                        Color.red);

                            this.Close();
                        }
                    }
                    else
                    {
                        if (EditorUtility.DisplayDialog("Delete Scene", "Are you sure you want to delete this scene?", "Yes", "No"))
                        {
	                        AssetDatabase.DeleteAsset("Assets/Scenes/" + mapNames[i] + ".unity");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + ".unity", 
		                        Color.red);

	                        AssetDatabase.DeleteAsset("Assets/Do Not Edit OR Delete/Terrains/" + mapNames[i] + " Terrain.asset");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + " Terrain.asset", 
		                        Color.red);

                            AssetDatabase.DeleteAsset("Assets/Levels/" + mapNames[i] + ".prefab");

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "Deleting " + mapNames[i] + ".prefab", 
		                        Color.red);

	                        DebugChannelSO.OverrideRaise(
		                        "DeleteMapWindow", 
		                        "All files for " + mapNames[i] + " successfully deleted!", 
		                        Color.red);

                            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);
    }
}
