using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace InABox.DynamicGrid
{
    public delegate void KanbanColumnCollapsedHandler(object sender, bool collapsed);

    /// <summary>
    ///     Interaction logic for KanbanColumn.xaml
    /// </summary>
    public partial class DynamicKanbanColumn : UserControl
    {
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(DynamicKanbanColumn));

        public static readonly DependencyProperty CollapsedProperty =
            DependencyProperty.Register("Collapsed", typeof(bool), typeof(DynamicKanbanColumn));

        public static readonly DependencyProperty HeaderContextMenuProperty =
            DependencyProperty.Register("HeaderContextMenu", typeof(ContextMenu), typeof(DynamicKanbanColumn));

        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(DynamicKanbanColumn));

        public static readonly DependencyProperty ItemContextMenuProperty =
            DependencyProperty.Register("ItemContextMenu", typeof(ContextMenu), typeof(DynamicKanbanColumn));

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(DynamicKanbanColumn));

        private bool _collapsed;


        public DynamicKanbanColumn()
        {
            InitializeComponent();
        }

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set
            {
                ColumnTitle.Content = value;
                SetValue(TitleProperty, value);
            }
        }

        public bool Collapsed
        {
            get => (bool)GetValue(CollapsedProperty);
            set
            {
                _collapsed = value;
                ColumnGrid.ColumnDefinitions[0].Width = value ? new GridLength(0, GridUnitType.Pixel) : new GridLength(1, GridUnitType.Star);
                ColumnGrid.ColumnDefinitions[1].Width = value ? new GridLength(1, GridUnitType.Auto) : new GridLength(0, GridUnitType.Pixel);
                MinWidth = _collapsed ? 35.0F : 300.0F;
                OnCollapsed?.Invoke(this, value);
                SetValue(CollapsedProperty, value);
            }
        }

        public ContextMenu HeaderContextMenu
        {
            get => (ContextMenu)GetValue(HeaderContextMenuProperty);
            set => SetValue(HeaderContextMenuProperty, value);
        }

        public DataTemplate ItemTemplate
        {
            get => (DataTemplate)GetValue(ItemTemplateProperty);
            set => SetValue(ItemTemplateProperty, value);
        }

        public ContextMenu ItemContextMenu
        {
            get => (ContextMenu)GetValue(ItemContextMenuProperty);
            set => SetValue(ItemContextMenuProperty, value);
        }

        public IEnumerable ItemsSource
        {
            get => (IEnumerable)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value);
        }

        public event KanbanColumnCollapsedHandler OnCollapsed;

        private void ExpandColumn_Click(object sender, MouseButtonEventArgs e)
        {
            Collapsed = false;
        }

        private void CollapseColumn_Click(object sender, MouseButtonEventArgs e)
        {
            Collapsed = true;
        }
    }
}