using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace MaterialDesignThemes.Wpf
{
    public static class DataGridAssist
    {
        public static readonly DependencyProperty AutoGeneratedCheckBoxStyleProperty = DependencyProperty.RegisterAttached(
            "AutoGeneratedCheckBoxStyle", typeof (Style), typeof (DataGridAssist), new PropertyMetadata(default(Style), AutoGeneratedCheckBoxStylePropertyChangedCallback));

        private static void AutoGeneratedCheckBoxStylePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            ((DataGrid) dependencyObject).AutoGeneratingColumn += (sender, args) =>
            {
                var dataGridCheckBoxColumn = args.Column as DataGridCheckBoxColumn;
                if (dataGridCheckBoxColumn == null) return;

                dataGridCheckBoxColumn.ElementStyle = GetAutoGeneratedCheckBoxStyle(dependencyObject);
            };            
        }

        public static void SetAutoGeneratedCheckBoxStyle(DependencyObject element, Style value)
        {
            element.SetValue(AutoGeneratedCheckBoxStyleProperty, value);
        }

        public static Style GetAutoGeneratedCheckBoxStyle(DependencyObject element)
        {
            return (Style) element.GetValue(AutoGeneratedCheckBoxStyleProperty);
        }

        public static readonly DependencyProperty AutoGeneratedEditingCheckBoxStyleProperty = DependencyProperty.RegisterAttached(
            "AutoGeneratedEditingCheckBoxStyle", typeof (Style), typeof (DataGridAssist), new PropertyMetadata(default(Style), AutoGeneratedEditingCheckBoxStylePropertyChangedCallback));

        private static void AutoGeneratedEditingCheckBoxStylePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            ((DataGrid)dependencyObject).AutoGeneratingColumn += (sender, args) =>
            {
                var dataGridCheckBoxColumn = args.Column as DataGridCheckBoxColumn;
                if (dataGridCheckBoxColumn == null) return;

                dataGridCheckBoxColumn.EditingElementStyle = GetAutoGeneratedEditingCheckBoxStyle(dependencyObject);
            };
        }

        public static void SetAutoGeneratedEditingCheckBoxStyle(DependencyObject element, Style value)
        {
            element.SetValue(AutoGeneratedEditingCheckBoxStyleProperty, value);
        }

        public static Style GetAutoGeneratedEditingCheckBoxStyle(DependencyObject element)
        {
            return (Style) element.GetValue(AutoGeneratedEditingCheckBoxStyleProperty);
        }

        public static readonly DependencyProperty AutoGeneratedEditingTextStyleProperty = DependencyProperty.RegisterAttached(
            "AutoGeneratedEditingTextStyle", typeof (Style), typeof (DataGridAssist), new PropertyMetadata(default(Style), AutoGeneratedEditingTextStylePropertyChangedCallback));

        private static void AutoGeneratedEditingTextStylePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            ((DataGrid)dependencyObject).AutoGeneratingColumn += (sender, args) =>
            {
                var dataGridTextColumn = args.Column as DataGridTextColumn;
                if (dataGridTextColumn == null) return;

                dataGridTextColumn.EditingElementStyle = GetAutoGeneratedEditingTextStyle(dependencyObject);
            };
        }

        public static void SetAutoGeneratedEditingTextStyle(DependencyObject element, Style value)
        {
            element.SetValue(AutoGeneratedEditingTextStyleProperty, value);
        }

        public static Style GetAutoGeneratedEditingTextStyle(DependencyObject element)
        {
            return (Style) element.GetValue(AutoGeneratedEditingTextStyleProperty);
        }        
    }
}