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

public class AssetAndBuildHandling : 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;

	public string exportName = "My Race Tracks", additionalChars = "";
	const string randomChars = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz0123456789";
	string[] removeChars = { "/", "\\", "\"", ":", "?", "<", ">", "|", "*", "~", ".", "@", "#", "$", "%", "^", "(", ")", "{", "}", "[", "]", "`", ";", ",", "!", "'", "+", "=" };

	/*
	[MenuItem("Rocket Kart Racers/Add Tracks to Build", false)]
	public static void SelectTracksToBuild () {
		//AssetAndBuildHandling window = GetWindow<AssetAndBuildHandling> ("Select Tracks to Add");
	}
	*/

	[MenuItem("Rocket Kart Racers/Export-Import Options/Export Tracks", false)]
	public static void SelectTracksToExport () {
		AssetAndBuildHandling window = GetWindow<AssetAndBuildHandling> ("Select Tracks to Export");
		window.optionType = OptionType.Export;
	}

	[MenuItem("Rocket Kart Racers/Export-Import Options/Export Tracks", true)]
	public static bool FindTracks () {
		string[] tempNames = Directory.GetFiles (Application.dataPath + "/Racetrack Scenes");
		trackScenes = new List<string> ();
		toggleTrack = new List<bool> ();

		if (tempNames.Length < 1) {
			Debug.Log ("None");
			return false;
		}
		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);
			}
		}

		return true;
	}

	// Select and Import Asset Packs
	[MenuItem("Rocket Kart Racers/Export-Import Options/Import Track Pack", false)]
	public static void SelectTracksToImport () {
		string selectedFile = EditorUtility.OpenFilePanelWithFilters("Select Tracks to Import", "Asset Packages", new string[] { "Unity Asset Package", "unitypackage" });
		if (selectedFile != "") {
			AssetDatabase.ImportPackage (selectedFile, true);
		}
	}

	void OnGUI () {
		if (optionType == OptionType.Build) {

		} else if (optionType == OptionType.Export) {
			for (int i = 0; i < trackScenes.Count; i++) {
				toggleTrack [i] = GUILayout.Toggle (toggleTrack [i], trackScenes [i]);
			}
			exportName = GUILayout.TextField (exportName);
			if (GUILayout.Button ("Export Selected Tracks")) {
				EditorSceneManager.SaveScene (EditorSceneManager.GetActiveScene ());
				AdjustTrackName ();
				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");
					}
				}
				string[] trackStrings = selectedTrackNames.ToArray ();
				string filename = exportName + ".unitypackage";
				AssetDatabase.ExportPackage (trackStrings,  "Asset Packages/" + filename, ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Interactive);
				this.Close ();
			}
		} else if (optionType == OptionType.Import) {

		}
	}

	void AdjustTrackName () {

		foreach (string c in removeChars) {
			exportName = exportName.Replace (c, string.Empty);
		}

		additionalChars = "";
		for (int i = 0; i < 6; i++) {
			additionalChars += randomChars [Random.Range (0, randomChars.Length)];
		}

		exportName = exportName + "_" + additionalChars;

	}
}
