// RibbonHotKeyHelper.cs
// Copyright (c) 2016 Bentley Systems, Incorporated. All Rights Reserved.

using Haestad.Support.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Text;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class used to reduce the size of the RibbonHotKey implementation and also allow the generation of unique string keys
    /// (via ToString()) without having to create a temporary RibbonHotKey instance.
    /// </summary>
    internal class RibbonHotKeyHelper
    {
        #region Constructor
        private RibbonHotKeyHelper()
        {
        }
        #endregion

        #region Internal Static Methods
        internal static string ToString(ModifierKeys modifierKey, Key triggerKey)
        {
            if (GetIsNoneHotKey(modifierKey, triggerKey))
                return "__none__"; // I18N. OK. 

            if (!GetIsValidShortCut(modifierKey, triggerKey))
                return String.Empty;

            if (modifierKey == ModifierKeys.None)
                return KeyToString(triggerKey);
            else
                return TextManager.Current["hfui_RibbonHotKey", new string[] { ModifierKeyToString(modifierKey), KeyToString(triggerKey) }];
        }

        internal static bool GetIsValidShortCut(ModifierKeys modifierKey, Key triggerKey)
        {
            if (triggerKey == Key.None)
                return false;

            if (modifierKey == ModifierKeys.None)
            {
                if (triggerKey >= Key.F1 && triggerKey <= Key.F24)
                    return true;
            }

            if (modifierKey == ModifierKeys.Alt && (triggerKey == Key.LeftAlt || triggerKey == Key.RightAlt))
                return true;

            if (modifierKey == ModifierKeys.Control)
                return true;
            if (modifierKey == ModifierKeys.Alt)
                return true;
            if (modifierKey == ModifierKeys.Shift)
                return true;
            if (modifierKey == (ModifierKeys.Control | ModifierKeys.Shift))
                return true;

            return false;
        }
        #endregion

        #region Private Static Methods
        private static bool GetIsNoneHotKey(ModifierKeys modifierKey, Key triggerKey)
        {
            return modifierKey == ModifierKeys.None && triggerKey == Key.None;
        }

        private static string ModifierKeyToString(ModifierKeys modifierKey)
        {
            switch (modifierKey)
            {
                case ModifierKeys.Alt:
                    return TextManager.Current["hfui_AltKey"];
                case ModifierKeys.Control:
                    return TextManager.Current["hfui_ControlKey"];
                case ModifierKeys.Shift:
                    return TextManager.Current["hfui_ShiftKey"];
                case ModifierKeys.Control | ModifierKeys.Shift:
                    return TextManager.Current["hfui_ControlShift"];
                default:
                    return String.Empty;
            }
        }

        private static string KeyToString(Key key)
        {
            switch (key)
            {
                case Key.D0:
                    return "0"; // I18N. OK.
                case Key.D1:
                    return "1"; // I18N. OK.
                case Key.D2:
                    return "2"; // I18N. OK.
                case Key.D3:
                    return "3"; // I18N. OK.
                case Key.D4:
                    return "4"; // I18N. OK.
                case Key.D5:
                    return "5"; // I18N. OK.
                case Key.D6:
                    return "6"; // I18N. OK.
                case Key.D7:
                    return "7"; // I18N. OK.
                case Key.D8:
                    return "8"; // I18N. OK.
                case Key.D9:
                    return "9"; // I18N. OK.

                // TODO 3: <whar> Other custom conversions as support is required.

                default:
                    return key.ToString();
            }
        }
        #endregion
    }
}
