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

namespace Passer.Humanoid {

	[InitializeOnLoad]
	public class HumanoidVisitors {

		public static List<string> visitors = new List<string>();

		static HumanoidVisitors() {
			//CheckVisitors();
			CheckScenes();
		}

		private const string fileName = "HumanoidVisitors.cs";

		private static void CheckScenes() {
			EditorBuildSettingsScene[] editorBuildSettingsScenes = EditorBuildSettings.scenes;
			foreach (EditorBuildSettingsScene editorBuildSettingsScene in editorBuildSettingsScenes) {
				string localSceneName = editorBuildSettingsScene.path;
				if (!visitors.Contains(localSceneName))
					visitors.Add(localSceneName);
			}
		}

		//private static void CheckVisitors() {
		//	string[] names = Directory.GetFiles(Application.dataPath, "HumanoidVisitors.cs", SearchOption.AllDirectories);
		//	int dataPathLength = Application.dataPath.Length;

		//	int filenameLength = fileName.Length;
		//	foreach (string name in names) {
		//		string dirName = name.Substring(0, name.Length - filenameLength);

		//		string[] sceneNames = Directory.GetFiles(dirName, "*.unity");

		//		foreach (string sceneName in sceneNames) {
		//			string localSceneName = "Assets" + sceneName.Substring(dataPathLength);
		//			localSceneName = localSceneName.Replace('\\', '/');
		//			if (!visitors.Contains(localSceneName))
		//				visitors.Add(localSceneName);
		//		}
		//	}
		//}

		private static bool SceneExists(string scenePath) {
			foreach (EditorBuildSettingsScene buildScene in EditorBuildSettings.scenes) {
				if (buildScene.path == scenePath)
					return true;
			}
			return false;
		}

		private static void AddScene(string scenePath) {
            if (SceneExists(scenePath))
                return;

            //Debug.Log("Add " + scenePath);

			EditorBuildSettingsScene[] editorBuildSettingsScenes = EditorBuildSettings.scenes;
			EditorBuildSettingsScene[] newEditorBuildSettingsScenes = new EditorBuildSettingsScene[editorBuildSettingsScenes.Length + 1];

			int i;
			for (i = 0; i < editorBuildSettingsScenes.Length; i++) {
				newEditorBuildSettingsScenes[i] = editorBuildSettingsScenes[i];
			}
			newEditorBuildSettingsScenes[i] = new EditorBuildSettingsScene(scenePath, true);
			EditorBuildSettings.scenes = newEditorBuildSettingsScenes;
		}

        private static void RemoveScene(string scenePath) {
            if (!SceneExists(scenePath))
                return;

            //Debug.Log("Remove " + scenePath);

            EditorBuildSettingsScene[] editorBuildSettingsScenes = EditorBuildSettings.scenes;
            EditorBuildSettingsScene[] newEditorBuildSettingsScenes = new EditorBuildSettingsScene[editorBuildSettingsScenes.Length - 1];

            int i;
            int j = 0;
            bool found = false;
            for (i = 0; i < editorBuildSettingsScenes.Length; i++) {
                if (editorBuildSettingsScenes[i].path != scenePath) {
                    // Check if we do not find the scene to remove
                    if (j < editorBuildSettingsScenes.Length - 1)
                        newEditorBuildSettingsScenes[j++] = editorBuildSettingsScenes[i];
                }
                else
                    found = true;
            }
            if (found)
                EditorBuildSettings.scenes = newEditorBuildSettingsScenes;
        }

        public static void SetPersonalHumanoid(string oldSceneName, string newSceneName) {
            if (!HumanoidVisitors.visitors.Contains(newSceneName))
                return;

            RemoveScene(oldSceneName);
            AddScene(newSceneName);
        }
    }
}