﻿#region Copyright

/* -----------------------------------------------------------------------------
 *  Copyright © 2024 • Chomp Studios • All Rights Reserved
 * -----------------------------------------------------------------------------
 *  Date: 28/12/2024
 *  -----------------------------------------------------------------------------
 *  Description: Helper functions for localisation
 *  -----------------------------------------------------------------------------
 */

#endregion

#region Imports

using UnityEditor;
using UnityEngine;
using Workflow_Toolkit.Utilities.Localisation;

#endregion

#if UNITY_EDITOR

namespace Workflow_Toolkit.Utilities {
    public static class LanguageElements {
        /// <summary>
        ///     Visually displays the name of the language, rather than the code.
        /// </summary>
        /// <param name="code">the <see cref="Workflow_Toolkit.Utilities.Localisation.LanguageCode" /> enum.</param>
        /// <returns>the display name of the langauge you have selected</returns>
        public static string GetLanguageDisplayName(LanguageCode code) {
            switch (code) {
                case LanguageCode.ENGLISH:
                    return "English";
                case LanguageCode.DUTCH:
                    return "Dutch (Nederland)";
                case LanguageCode.PORTUGUESE:
                    return "Portuguese (Português)";
                case LanguageCode.SPANISH:
                    return "Spanish (Español)";
                case LanguageCode.DANISH:
                    return "Danish (Dansk)";
                default:
                    return code.ToString();
            }
        }

        /// <summary>
        ///     gets the JSON File names of the selected language
        /// </summary>
        /// <param name="languageCode"></param>
        /// <returns>the <see cref="Workflow_Toolkit.Utilities.Localisation.LanguageCode" /> enum.</returns>
        public static string GetLanguageFileName(LanguageCode languageCode) {
            return languageCode switch {
                LanguageCode.ENGLISH => "en-GB",
                LanguageCode.DUTCH => "nl-NL",
                LanguageCode.PORTUGUESE => "pt-PT",
                LanguageCode.SPANISH => "es-MX",
                LanguageCode.DANISH => "da-DK",
                _ => "en-GB"
            };
        }

        /// <summary>
        ///     Applies the language by calling the LocalisationManager LoadLanguage() function, then refreshes all windows.
        /// </summary>
        /// <param name="languageCode">the <see cref="Workflow_Toolkit.Utilities.Localisation.LanguageCode" /> enum.</param>
        public static void ApplyLanguage(LanguageCode languageCode) {
            LocalisationManager.Instance.LoadLanguage(languageCode);
            RefreshAllEditorWindows();
            EditorUtility.DisplayDialog("Language Changed",
                $"Language has been changed to {GetLanguageDisplayName(languageCode)}.", "OK");
        }

        /// <summary>
        ///     Refreshes all editor windows using a foreach loop.
        /// </summary>
        public static void RefreshAllEditorWindows() {
            foreach (var window in Resources.FindObjectsOfTypeAll<EditorWindow>()) window.Repaint();
        }
    }
}
#endif