//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//
// UserWin satellite library | Author: Sergei Y.Kitáev
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Text;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;

using CommonHelpers;

namespace UserWin {

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class UXInput {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool IsSoleKey(KeyEventArgs e,Key key)
{
   return e.Key==key && e.KeyboardDevice.Modifiers==ModifierKeys.None;
}

public static bool IsSoleEnterKey(KeyEventArgs e)
{
   return IsSoleKey(e,Key.Enter);
}

public static bool IsSoleEscapeKey(KeyEventArgs e)
{
   return IsSoleKey(e,Key.Escape);
}

//==================================================================================================

public static bool IsNoneMouseButton(MouseButtonEventArgs e)
{
   return
      e.LeftButton  == MouseButtonState.Released &&
      e.RightButton == MouseButtonState.Released &&
      e.MiddleButton== MouseButtonState.Released &&
      e.XButton1    == MouseButtonState.Released &&
      e.XButton2    == MouseButtonState.Released;
}

public static bool IsSoleLeftMouseButton(MouseButtonEventArgs e)
{
   return
      e.LeftButton  == MouseButtonState.Pressed  &&
      e.RightButton == MouseButtonState.Released &&
      e.MiddleButton== MouseButtonState.Released &&
      e.XButton1    == MouseButtonState.Released &&
      e.XButton2    == MouseButtonState.Released;
}

public static bool IsSoleRightMouseButton(MouseButtonEventArgs e)
{
   return
      e.LeftButton  == MouseButtonState.Released &&
      e.RightButton == MouseButtonState.Pressed  &&
      e.MiddleButton== MouseButtonState.Released &&
      e.XButton1    == MouseButtonState.Released &&
      e.XButton2    == MouseButtonState.Released;
}

//==================================================================================================

public static bool IsSoleLeftMouseButtonSingleClick(MouseButtonEventArgs e)
{
   return
      IsSoleLeftMouseButton(e) &&
      e.ChangedButton == MouseButton.Left &&
      e.ClickCount==1;
}

public static bool IsSoleRightMouseButtonSingleClick(MouseButtonEventArgs e)
{
   return
      IsSoleRightMouseButton(e) &&
      e.ChangedButton == MouseButton.Right &&
      e.ClickCount==1;
}

//--------------------------------------------------------------------------------------------------

public static bool IsSoleLeftMouseButtonDoubleClick(MouseButtonEventArgs e)
{
   return
      IsSoleLeftMouseButton(e) &&
      e.ChangedButton == MouseButton.Left &&
      e.ClickCount==2;
}

//--------------------------------------------------------------------------------------------------

public static bool IsSoleLeftMouseButtonUp(MouseButtonEventArgs e)
{
   return
      IsNoneMouseButton(e) &&
      e.ChangedButton == MouseButton.Left &&
      e.ClickCount==1;
}

public static bool IsSoleRightMouseButtonUp(MouseButtonEventArgs e)
{
   return
      IsNoneMouseButton(e) &&
      e.ChangedButton == MouseButton.Right &&
      e.ClickCount==1;
}

//==================================================================================================

public static string[] ModifierKeysToDisplayStrings(ModifierKeys modifiers)
{
   var list=new List<string>();

   if ((modifiers & ModifierKeys.Control) != default(ModifierKeys))
      list.Add("Ctrl");
   if ((modifiers & ModifierKeys.Alt) != default(ModifierKeys))
      list.Add("Alt");
   if ((modifiers & ModifierKeys.Shift) != default(ModifierKeys))
      list.Add("Shift");
   if ((modifiers & ModifierKeys.Windows) != default(ModifierKeys))
      list.Add("Windows");

   return list.ToArray();
}

public static string ModifierKeysToDisplayString(ModifierKeys modifiers)
{
   var sb = new StringBuilder();
   foreach (var str in ModifierKeysToDisplayStrings(modifiers))
   {
      if (sb.Length!=0)
         sb.Append('+');
      sb.Append(str);
   }
   return sb.ToString();
}

public static string ModifierKeysToDisplayString(Key key,ModifierKeys modifiers)
{
   return modifiers != ModifierKeys.None ?
      string.Format("{1}+{0}",key,ModifierKeysToDisplayString(modifiers)) :
      key.ToString();
}

//--------------------------------------------------------------------------------------------------

public static InputGestureCollection CreateInputs(Key key)
{
   return CreateInputs(key,ModifierKeys.None);
}

public static InputGestureCollection CreateInputs(Key key,ModifierKeys modifiers)
{
   var inputs = new InputGestureCollection();
   inputs.Add( new KeyGesture(
      key, modifiers, ModifierKeysToDisplayString(key,modifiers) ));
   return inputs;
}

//==================================================================================================

public static string ExtractGestureDisplayString(FrameworkElement element)
{
   ICommand                cmd       = null;
   RoutedUICommand         command   = null;
   InputGestureCollection  inputs    = null;
   KeyGesture              keyGesture= null;

   if (element is ButtonBase)
      cmd = ((ButtonBase)element).Command;
   if (cmd!=null)
      command = cmd as RoutedUICommand;
   if (command!=null)
      inputs = command.InputGestures;
   if (inputs!=null && inputs.Count>0)
      keyGesture = inputs[0] as KeyGesture;
   if (keyGesture!=null)
      return keyGesture.DisplayString;

   return null;
}

//==================================================================================================

static UXInput()
{
   UserWin_Library.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UXInput
//**************************************************************************************************

} // UserWin
