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

public class AddToBuildWindow : SearchableEditorWindow {

	[System.Serializable]
	public enum OptionType { Build, Export, Import };

	public static List<string> trackScenes;
	public static List<bool> toggleTrack;

	public OptionType optionType;
	public bool allFiles = false;

	List<SceneAsset> m_SceneAssets = new List<SceneAsset>();

	[MenuItem("Rocket Kart Racers/Add Tracks to Build", false)]
	public static void SelectTracksToExport () {
		AddToBuildWindow window = GetWindow<AddToBuildWindow> ("Select Tracks to Add to Build");
		window.optionType = OptionType.Export;
		window.FindTracks ();
	}

	public void FindTracks () {
		string[] tempNames = Directory.GetFiles (Application.dataPath + "/Racetrack Scenes");
		trackScenes = new List<string> ();
		toggleTrack = new List<bool> ();

		if (tempNames.Length < 1) {
			Debug.Log ("None");
		}
		foreach (string track in tempNames) {
			if (!track.Contains (".meta") && !track.Contains(".txt")) {
				#if UNITY_EDITOR_WIN
				string trackName = track.Substring(track.LastIndexOf('\\') + 1);
				#else
				string trackName = track.Substring(track.LastIndexOf('/') + 1);
				#endif
				trackScenes.Add (trackName.Replace (".unity", ""));
				toggleTrack.Add (false);
			}
		}
	}

	void OnGUI () {

		GUILayout.BeginHorizontal ();
		GUILayout.Label ("Add All Tracks to Build", EditorStyles.boldLabel);
		allFiles = GUILayout.Toggle (allFiles, "");
		GUILayout.EndHorizontal ();

		for (int i = 0; i < trackScenes.Count; i++) {
			if (allFiles) {
				toggleTrack [i] = GUILayout.Toggle (true, trackScenes [i]);
			} else {
				toggleTrack [i] = GUILayout.Toggle (toggleTrack [i], trackScenes [i]);
			}
		}

		if (GUILayout.Button ("Add Tracks to Build")) {
			List<string> selectedTrackNames = new List<string> ();
			for (int i = 0; i < toggleTrack.Count; i++) {
				if (toggleTrack [i]) {
					selectedTrackNames.Add ("Assets/Racetrack Scenes/" + trackScenes [i] + ".unity");
				}
			}
			SetEditorBuildSettingsScenes(selectedTrackNames);
			this.Close ();
		}
	}

	public void SetEditorBuildSettingsScenes (List<string> scenes) {
		// Find valid Scene paths and make a list of EditorBuildSettingsScene
		List<EditorBuildSettingsScene> editorBuildSettingsScenes = new List<EditorBuildSettingsScene>();

		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/Esports-Lobby.unity", true));
		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/RKR-Lobby.unity", true));
		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/Custom Racer Creator.unity", true));
		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/Astroid Desert.unity", true));
		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/GummyBear Lane.unity", true));
		editorBuildSettingsScenes.Add (new EditorBuildSettingsScene ("Assets/Scenes/Rocket Roadhouse.unity", true));

		if (scenes [0] != "") {

			foreach (string scene in scenes) {
				editorBuildSettingsScenes.Add (new EditorBuildSettingsScene (scene, true));
			}

			// Set the Build Settings window Scene list
		}

		EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray ();
	}
}
