using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace CustomControls
{
    /// <summary>
    /// Interakční logika pro TextBlox.xaml
    /// </summary>
    public partial class LabeledTextbox : UserControl
    {
        public LabeledTextbox()
        {
            InitializeComponent();
            LayoutRoot.DataContext = this;
        }

        [Description("Text written to label"), Category("Data")]
        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValueDp(TextProperty, value);
            }
        }

        [Description("Value in textbox"), Category("Data")]
        public string Value
        {
            get
            {
                return (string)GetValue(ValueProperty);
            }
            set
            {
                SetValueDp(ValueProperty, value);
            }
        }

        [Description("Visibility of LabeledTextbox"), Category("Data")]
        public string Visible
        {
            get
            {
                return (string)GetValue(VisibleProperty);
            }
            set
            {
                SetValueDp(VisibleProperty, value);
            }
        }
        [Description("Enabling of LabeledTextbox"), Category("Data")]
        public string Enabled
        {
            get
            {
                return (string)GetValue(EnabledProperty);
            }
            set
            {
                SetValueDp(EnabledProperty, value);
            }
        }
        [Description("Minimal textbox width"), Category("Data")]
        public string TextboxWidth
        {
            get
            {
                return (string)GetValue(TextboxWidthProperty);
            }
            set
            {
                SetValueDp(TextboxWidthProperty, value);
            }
        }

        [Description("Texbox readonly property"), Category("Data")]
        public bool TextboxReadonly
        {
            get
            {
                return (bool)GetValue(TextboxReadProperty);
            }
            set
            {
                SetValueDp(TextboxReadProperty, value);
            }
        }

        [Description("Button command"), Category("Data")]
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }


        [Description("Button command parameter"), Category("Data")]
        public object CommandPar
        {
            get { return (object)GetValue(CommandParProperty); }
            set { SetValue(CommandParProperty, value); }
        }

        private void Data_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    string MBText = "";
                    string Header = "";

                    var dict = Application.Current.Resources.MergedDictionaries;

                    var current = Application.Current.Resources.MergedDictionaries.FirstOrDefault(
                           m => m.Source.OriginalString.Contains("Resources_"));

                    if (current.Source.ToString().Contains("Czech"))
                    {
                        MBText = "Provést změny?";
                        Header = "Upozornění na změnu hodnoty";
                    }
                    else
                    {
                        MBText = "Accept changes?";
                        Header = "Value change notification";
                    }


                    var result = MessageBox.Show(MBText, Header, MessageBoxButton.YesNo, MessageBoxImage.Question);

                    if (result == MessageBoxResult.Yes)
                    {
                        DependencyProperty prop = ValueProperty;
                        BindingExpression binding = BindingOperations.GetBindingExpression(this, prop);
                        if (binding != null) { binding.UpdateSource(); }
                        Keyboard.ClearFocus();

                        if (Command != null)
                            Command.Execute(CommandPar);
                    }
                    else
                    {
                        DependencyProperty prop = ValueProperty;
                        BindingExpression binding = BindingOperations.GetBindingExpression(this, prop);
                        if (binding != null) { binding.UpdateTarget(); }
                        Keyboard.ClearFocus();
                    }
                }
            }
            catch (Exception ex)
            {
                //Wrong texbox type
            }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(LabeledTextbox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(String), typeof(LabeledTextbox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register("Visible", typeof(String), typeof(LabeledTextbox), new FrameworkPropertyMetadata("Visible", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty EnabledProperty = DependencyProperty.Register("Enabled", typeof(String), typeof(LabeledTextbox), new FrameworkPropertyMetadata("true", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty TextboxWidthProperty = DependencyProperty.Register("TextboxWidth", typeof(String), typeof(LabeledTextbox), new FrameworkPropertyMetadata("100", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty TextboxReadProperty = DependencyProperty.Register("TextboxReadonly", typeof(bool), typeof(LabeledTextbox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(LabeledTextbox));
        public static readonly DependencyProperty CommandParProperty = DependencyProperty.Register("CommandPar", typeof(object), typeof(LabeledTextbox), new FrameworkPropertyMetadata(null));

        public event PropertyChangedEventHandler PropertyChanged;
        public void SetValueDp(DependencyProperty property, object value, [System.Runtime.CompilerServices.CallerMemberName] string p = null)
        {
            SetCurrentValue(property, value);
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p));
        }


    }
}
