using System;
using System.Collections.Generic;

namespace Volpe.Cafe.Resources
{
    [Serializable]
    internal static class IconThemeHelper
    {
        private const string DefaultIconThemeName = "glaze";
        private const string DefaultIconSizeName = "22";

        // init icon-name lookups
        private static Dictionary<IconName, string> IconNames = new Dictionary<IconName, string>()
        {
            [IconName.Apply           ] = "apply",
            [IconName.Browse          ] = "browse",
            [IconName.Cancel          ] = "cancel",
            [IconName.Configure       ] = "configure",
            [IconName.Document        ] = "document",
            [IconName.Error           ] = "error",
            [IconName.Exit            ] = "exit",
            [IconName.FileClose       ] = "file_close",
            [IconName.FileNew         ] = "file_new",
            [IconName.FileOpen        ] = "file_open",
            [IconName.FileSave        ] = "file_save",
            [IconName.FileSaveAs      ] = "file_saveas",
            [IconName.Find            ] = "find",
            [IconName.Folder          ] = "folder",
            [IconName.Info            ] = "info",
            [IconName.ModelManual     ] = "model_manual",
            [IconName.ModelRef        ] = "model_ref",
            [IconName.ModelRefCS      ] = "model_ref_cs",
            [IconName.Next            ] = "next",
            [IconName.OK              ] = "ok",
            [IconName.PauseModel      ] = "pause_model",
            [IconName.Previous        ] = "previous",
            [IconName.Refresh         ] = "refresh",
            [IconName.StartModel      ] = "start_model",
            [IconName.StopModel       ] = "stop_model",
            [IconName.Warning         ] = "warning",
            [IconName.WindowFullscreen] = "window_fullscreen",
            [IconName.WindowRestore   ] = "window_restore",
            [IconName.Zoom            ] = "zoom",
            [IconName.ZoomMinus       ] = "zoom_minus",
            [IconName.ZoomPlus        ] = "zoom_plus",
            [IconName.ZoomRestore     ] = "zoom_restore"
        };

        /// <summary>
        /// Computes and returns the full icon name from the specified icon <paramref name="theme"/>,
        /// <paramref name="size"/>, and <paramref name="name"/>.
        /// </summary>
        public static string GetFullIconName(IconTheme theme, IconSize size, IconName name)
        {
            return "ThemeIcon_" + GetIconTheme(theme) + "_" + GetIconSize(size) + "_" + GetIconName(name);
        }

        /// <summary>
        /// Computes and returns the full icon name from the specified icon <paramref name="theme"/>, size (in pixels),
        /// and <paramref name="name"/>.
        /// </summary>
        public static string GetFullIconName(IconTheme theme, int sizeInPixels, IconName name)
        {
            return GetFullIconName(theme, PixelsToIconSize(sizeInPixels), name);
        }

        /// <summary>
        /// Returns a string representation of the specified icon <paramref name="theme"/>.
        /// </summary>
        public static string GetIconTheme(IconTheme theme)
        {
            switch (theme) {
                case IconTheme.Glaze: return "glaze";

                default:
                    return DefaultIconThemeName;
            }
        }

        /// <summary>
        /// Returns a string representation of the specified icon <paramref name="size"/>.
        /// </summary>
        public static string GetIconSize(IconSize size)
        {
            switch (size) {
                case IconSize.x22: return "22";
                case IconSize.x32: return "32";
                case IconSize.x48: return "48";
                case IconSize.x64: return "64";

                default:
                    return DefaultIconSizeName;
            }
        }

        /// <summary>
        /// Returns a string representation of the specified icon <paramref name="name"/>.
        /// </summary>
        public static string GetIconName(IconName name)
        {
            if (IconNames.TryGetValue(name, out string value)) {
                return value;
            }

            // if provided icon-name is not valid, default to "cancel" icon
            return IconNames[IconName.Cancel];
        }

        /// <summary>
        /// Converts the specified icon size from pixels to best available <see cref="IconSize"/> value.
        /// </summary>
        /// <param name="sizeInPixels">The desired size, in pixels, of an icon.</param>
        /// <returns>The <see cref="IconSize"/> value that best maps to the desired pixel size of an icon.</returns>
        public static IconSize PixelsToIconSize(int sizeInPixels)
        {
            return (sizeInPixels > 48) ? IconSize.x64 :
                   (sizeInPixels > 32) ? IconSize.x48 :
                   (sizeInPixels > 22) ? IconSize.x32 : IconSize.x22;
        }

    }
}
