using System;
using System.Collections;
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 ComboBlox.xaml
    /// </summary>
    public partial class LabeledCombobox : UserControl
    {
        public LabeledCombobox()
        {
            InitializeComponent();
            LayoutRoot.DataContext = this;
        }

        public EventHandler SelectionChanged;

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

        [Description("Visibility of LabeledCombobox"), Category("Data")]
        public string Visible
        {
            get
            {
                return (string)GetValue(VisibleProperty);
            }
            set
            {
                SetValueDp(VisibleProperty, value);
            }
        }

        [Description("Minimal combobox width"), Category("Data")]
        public string ComboWidth
        {
            get
            {
                return (string)GetValue(ComboWidthProperty);
            }
            set
            {
                SetValueDp(ComboWidthProperty, value);
            }
        }

        [Description("Item source for combobox"), Category("Data")]
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
                Data.ItemsSource = value;
            }
        }

        [Description("Selected index"), Category("Data")]
        public object SelectedIndex
        {
            get { return (object)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, 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); }
        }

        /// <summary>
        /// Event for firing command when selection is changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectionChangedCbox(object sender, RoutedEventArgs e)
        {
            if (Command != null)
                Command.Execute(CommandPar);
            e.Handled = true;
        }

       

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(LabeledCombobox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register("Visible", typeof(String), typeof(LabeledCombobox), new FrameworkPropertyMetadata("Visible", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty ComboWidthProperty = DependencyProperty.Register("ComboWidth", typeof(String), typeof(LabeledCombobox), new FrameworkPropertyMetadata("100", FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(LabeledCombobox), null);
        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(object), typeof(LabeledCombobox), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.PropertyChanged));
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(LabeledCombobox));
        public static readonly DependencyProperty CommandParProperty = DependencyProperty.Register("CommandPar", typeof(object), typeof(LabeledCombobox), new FrameworkPropertyMetadata(null));


        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));
        }
    }
}
