/*[MenuItem("FMOD/Show Banks")]
         * public static void ShowBanks()
         * {
         *  UpdateCache();
         *  StringBuilder message = new StringBuilder();
         *  foreach(var bankRef in eventCache.EditorBanks)
         *  {
         *      message.AppendLine(String.Format("{0}\t\t{1}", Path.GetFileNameWithoutExtension(bankRef.Path), bankRef.LoadResult.ToString()));
         *  }
         *  EditorUtility.DisplayDialog("Loaded Banks", message.ToString(), "OK");
         * }
         *
         * [MenuItem("FMOD/Force Import")]
         * public static void ForceImport()
         * {
         *  eventCache = ScriptableObject.CreateInstance<EventCache>();
         *  AssetDatabase.CreateAsset(eventCache, "Assets/Resources/" + CacheAssetName + ".asset");
         *  UpdateCache();
         * }
         *
         * [MenuItem("FMOD/Force Cache Reload")]
         * public static void ForceReload()
         * {
         *  eventCache = Resources.Load(CacheAssetName) as EventCache;
         *  UpdateCache();
         * }*/

        private static void BuildTargetChanged()
        {
            UpdateCache();

            // Copy over assets for the new platform
            EditorUtils.CopyToStreamingAssets();
        }
        static void OnCacheChange()
        {
            Settings.Instance.MasterBank = new BankRef(MasterBank.Name);

            Settings.Instance.Banks.Clear();
            foreach (var bankRef in eventCache.EditorBanks)
            {
                if (bankRef != MasterBank)
                {
                    Settings.Instance.Banks.Add(new BankRef(bankRef.Name));
                }
            }
            EditorUtility.SetDirty(Settings.Instance);
            EditorUtility.SetDirty(eventCache);

            EditorUtils.CopyToStreamingAssets();

            EventBrowser eventBrowser = EditorWindow.GetWindow <EventBrowser>("FMOD Events");

            eventBrowser.Repaint();
        }
        public override void OnInspectorGUI()
        {
            Settings settings = target as Settings;

            EditorUtility.SetDirty(settings);

            hasBankSourceChanged = false;

            GUIStyle style = new GUIStyle(GUI.skin.label);

            style.richText = true;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("<b>Source</b>", GUI.skin.textField, style);
            int selection = settings.HasSourceProject ? 0 : 1;

            selection = GUILayout.SelectionGrid(selection, new GUIContent[2] {
                new GUIContent("Project"), new GUIContent("Bank Folder")
            }, 2, GUILayout.ExpandWidth(false));
            settings.HasSourceProject = selection == 0;
            EditorGUILayout.EndHorizontal();

            if (selection == 0)
            {
                EditorGUILayout.BeginHorizontal();
                string oldPath = settings.SourceProjectPath;
                EditorGUILayout.PrefixLabel("Studio Project Path", GUI.skin.textField, style);
                settings.SourceProjectPath = EditorGUILayout.TextField(GUIContent.none, settings.SourceProjectPath);
                if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false)))
                {
                    GUI.FocusControl(null);
                    string path = EditorUtility.OpenFilePanel("Locate Studio Project", oldPath, "fspro");
                    if (!String.IsNullOrEmpty(path))
                    {
                        settings.SourceProjectPath = path;
                        this.Repaint();
                    }
                }
                EditorGUILayout.EndHorizontal();

                // Cache in settings for runtime access in play-in-editor mode
                settings.SourceBankPath = EditorUtils.GetBankDirectory();

                settings.HasPlatforms = true;

                // First time project path is set or changes, copy to streaming assets
                if (settings.SourceProjectPath != oldPath)
                {
                    hasBankSourceChanged = true;
                }
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                string oldPath = settings.SourceBankPath;
                EditorGUILayout.PrefixLabel("Banks Folder Path", GUI.skin.textField, style);
                settings.SourceBankPath = EditorGUILayout.TextField(GUIContent.none, settings.SourceBankPath);
                if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false)))
                {
                    GUI.FocusControl(null);
                    var path = EditorUtility.OpenFolderPanel("Locate Studio Banks", oldPath, null);
                    if (!String.IsNullOrEmpty(path))
                    {
                        settings.SourceBankPath = path;
                    }
                }
                EditorGUILayout.EndHorizontal();

                settings.HasPlatforms = EditorUtils.GetBankPlatforms().Length > 0;

                // First time project path is set or changes, copy to streaming assets
                if (settings.SourceBankPath != oldPath)
                {
                    hasBankSourceChanged = true;
                }
            }


            if ((settings.HasSourceProject && !File.Exists(settings.SourceProjectPath)) || !Directory.Exists(settings.SourceBankPath))
            {
                return;
            }

            // ----- PIE ----------------------------------------------
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("<b>Play In Editor Settings</b>", style);
            EditorGUI.indentLevel++;
            DisplayParentBool("Live Update", settings.LiveUpdateSettings, FMODPlatform.PlayInEditor);
            if (settings.IsLiveUpdateEnabled(FMODPlatform.PlayInEditor))
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel(" ");
                #if UNITY_5_0 || UNITY_5_1
                GUILayout.Label("Unity 5.0 or 5.1 detected: Live update will listen on port <b>9265</b>", style);
#else
                GUILayout.Label("Live update will listen on port <b>9264</b>", style);
                #endif
                EditorGUILayout.EndHorizontal();
            }
            DisplayParentSpeakerMode("Speaker Mode", settings.SpeakerModeSettings, FMODPlatform.PlayInEditor);
            if (settings.HasPlatforms)
            {
                DisplayParentBuildDirectory("Bank Platform", settings.BankDirectorySettings, FMODPlatform.PlayInEditor);
            }
            EditorGUI.indentLevel--;

            // ----- Default ----------------------------------------------
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("<b>Default Settings</b>", style);
            EditorGUI.indentLevel++;
            DisplayParentBool("Live Update", settings.LiveUpdateSettings, FMODPlatform.Default);
            if (settings.IsLiveUpdateEnabled(FMODPlatform.Default))
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel(" ");
                #if UNITY_5_0 || UNITY_5_1
                GUILayout.Label("Unity 5.0 or 5.1 detected: Live update will listen on port <b>9265</b>", style);
#else
                GUILayout.Label("Live update will listen on port <b>9264</b>", style);
                #endif
                EditorGUILayout.EndHorizontal();
            }
            DisplayParentSpeakerMode("Speaker Mode", settings.SpeakerModeSettings, FMODPlatform.Default);
            DisplayParentFreq("Sample Rate", settings.SampleRateSettings, FMODPlatform.Default);
            if (settings.HasPlatforms)
            {
                bool prevChanged = GUI.changed;
                DisplayParentBuildDirectory("Bank Platform", settings.BankDirectorySettings, FMODPlatform.Default);
                hasBankSourceChanged |= !prevChanged && GUI.changed;
            }
            DisplayParentInt("Virtual Channel Count", settings.VirtualChannelSettings, FMODPlatform.Default, 0, 2048);
            DisplayParentInt("Real Channel Count", settings.RealChannelSettings, FMODPlatform.Default, 0, 2048);
            EditorGUI.indentLevel--;


            // ----- Plugins ----------------------------------------------
            EditorGUILayout.Separator();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("<b>Plugins</b>", GUI.skin.button, style);
            if (GUILayout.Button("Add Plugin", GUILayout.ExpandWidth(false)))
            {
                settings.Plugins.Add("");
            }
            EditorGUILayout.EndHorizontal();

            EditorGUI.indentLevel++;
            for (int count = 0; count < settings.Plugins.Count; count++)
            {
                EditorGUILayout.BeginHorizontal();
                settings.Plugins[count] = EditorGUILayout.TextField("Plugin " + (count + 1).ToString() + ":", settings.Plugins[count]);

                if (GUILayout.Button("Delete Plugin", GUILayout.ExpandWidth(false)))
                {
                    settings.Plugins.RemoveAt(count);
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUI.indentLevel--;


            // ----- Windows ----------------------------------------------
            DisplayPlatform(FMODPlatform.Desktop, null);
            DisplayPlatform(FMODPlatform.Mobile, new FMODPlatform[] { FMODPlatform.MobileHigh, FMODPlatform.MobileLow, FMODPlatform.PSVita });
            DisplayPlatform(FMODPlatform.Console, new FMODPlatform[] { FMODPlatform.XboxOne, FMODPlatform.PS4, FMODPlatform.WiiU });

            if (hasBankSourceChanged)
            {
                EditorUtils.CopyToStreamingAssets();
            }
        }