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


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

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


        [Description("Is checked property"), Category("Data")]
        public bool IsChecked
        {
            get
            {
                return (bool)GetValue(IsCheckedProperty);
            }
            set
            {
                SetValueDp(IsCheckedProperty, value);
            }
        }

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

        /// <summary>
        /// Checkbox checked event firing command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if(Command != null)
                Command.Execute(null);
            e.Handled = true;

        }

        /// <summary>
        /// Checkbox updating value on textbox click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            IsChecked = IsChecked == true ? false : true;
            e.Handled = true;
        }

      

        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(LabeledCheckbox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(LabeledCheckbox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(LabeledCheckbox));


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


    }
}
