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 CreateNewTrackWindow : SearchableEditorWindow {

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

	public GameObject defaultLevel, defaultPlane;
	public string levelName;
	public string meshName;
	public bool makeLevel = false;
	public bool destroyMe = false;


	[MenuItem("Rocket Kart Racers/Create New Track", false, 1)]
	public static void CreateNewTrack () {
		CreateNewTrackWindow window = GetWindow<CreateNewTrackWindow> ("Create New Track");
	}

	// Update is called once per frame
	void OnGUI () {
		GUILayout.Label ("Name Your Track...");
		trackName = GUILayout.TextField (trackName);
		AdjustTrackName ();
		if (GUILayout.Button ("Create Track")) {
			AssetDatabase.CopyAsset ("Assets/RaceTrack Scenes/Track Starter/Editable Track Area.unity", "Assets/Racetrack Scenes/" + sceneName + ".unity");
			EditorSceneManager.OpenScene ("Assets/Racetrack Scenes/" + sceneName + ".unity", OpenSceneMode.Single);
			this.Close ();
		}
	}

	void AdjustTrackName () {

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

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

		sceneName = trackName + "_" + additionalChars;

	}
}
