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

namespace Passer {
    using Humanoid;

    [CustomEditor(typeof(SiteNavigator))]
    public class SiteNavigator_Editor : Editor {
        protected SiteNavigator siteNavigator;

        protected string[] siteNames;

        #region Enable

        protected virtual void OnEnable() {
            siteNavigator = (SiteNavigator)target;

            InitializeSiteNames();
        }

        protected virtual void InitializeSiteNames() {
            EditorBuildSettingsScene[] editorBuildSettingsScenes = EditorBuildSettings.scenes;
            int siteCount = editorBuildSettingsScenes.Length - HumanoidVisitors.visitors.Count;

            List<string> siteList = new List<string>();
            siteList.Add("-none-");
            int j = 0;
            for (int i = 0; i < siteCount; i++) {
                if (!editorBuildSettingsScenes[j].enabled) {
                    j++;
                    continue;
                }

                string sceneName = editorBuildSettingsScenes[j].path;
                int lastSlash = sceneName.LastIndexOf('/');
                sceneName = sceneName.Substring(lastSlash + 1);
                sceneName = sceneName.Substring(0, sceneName.Length - 6); // remove .unity

                bool isVisitor = HumanoidVisitors.visitors.Contains(sceneName);
                if (!isVisitor) {
                    //siteNames[i] = sceneName;
                    siteList.Add(sceneName);
                    j++;
                }
            }
            siteNames = siteList.ToArray();//new string[siteCount];
        }

        #endregion Enable

        #region Inspector

        public override void OnInspectorGUI() {
            serializedObject.Update();

            LoadSiteAtStartInspector();
            StartSiteInspector();
            StartSceneInspector();

            serializedObject.ApplyModifiedProperties();
        }

        protected void LoadSiteAtStartInspector() {
            GUIContent text = new GUIContent(
                "Load Site at Start",
                ""
                );

            SerializedProperty loadSiteAtStartProp = serializedObject.FindProperty(nameof(siteNavigator.loadSiteAtStart));
            loadSiteAtStartProp.boolValue = EditorGUILayout.Toggle(text, loadSiteAtStartProp.boolValue);
        }

        protected void StartSiteInspector() {
            GUIContent text = new GUIContent(
                "Start Site",
                ""
                );

            SerializedProperty startSiteProp = serializedObject.FindProperty(nameof(siteNavigator.startSite));
            startSiteProp.stringValue = EditorGUILayout.TextField(text, startSiteProp.stringValue);
        }

        protected void StartSceneInspector() {
            GUIContent text = new GUIContent(
                "Start Scene",
                ""
                );

            SerializedProperty startSceneProp = serializedObject.FindProperty(nameof(siteNavigator.startScene));

            if (siteNames == null || siteNames.Length == 0) {
                startSceneProp.stringValue = "";
                return;
            }

            int ix = 0;
            for (; ix < siteNames.Length; ix++) {
                if (siteNames[ix] == startSceneProp.stringValue)
                    break;
            }
            if (ix == siteNames.Length) ix = 0;
            if (Application.isPlaying) {
                using (new EditorGUI.DisabledScope(true)) {
                    EditorGUILayout.Popup(text, ix, siteNames);
                }
            }
            else {

                ix = EditorGUILayout.Popup(text, ix, siteNames);
                startSceneProp.stringValue = siteNames[ix];
            }
        }

        #endregion Inspector
    }
}
