using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Data;

using DevExpress.Xpf.Grid;

namespace BeWo.Validation
{
    public static class ValidationTrigger
    {
        private static FrameworkElement _FirstInvalidElement;

        public static bool Validate(DependencyObject pParent, ICollection<string> pFrameworkElementsToExclude)
        {
            _FirstInvalidElement = null;
            TriggerValidation(pParent, pFrameworkElementsToExclude);

            if (_FirstInvalidElement != null)
            {
                _FirstInvalidElement.Focus();
            }

            return _FirstInvalidElement == null;
        }

        public static bool Validate(DependencyObject pParent)
        {
            _FirstInvalidElement = null;
            TriggerValidation(pParent, null);

            if (_FirstInvalidElement != null)
            {
                _FirstInvalidElement.Focus();
            }

            return _FirstInvalidElement == null;
        }

        private static void TriggerValidation(DependencyObject pElement, ICollection<string> pFrameworkElementsToExclude)
        {
            if (!(pElement is GridControl))
            {
                var pFrameworkElement = pElement as FrameworkElement;
                var pFrameworkContentElement = pElement as FrameworkContentElement;
                var pDoNotFilterElementBecauseOfName = true;

                if (pFrameworkElementsToExclude != null)
                {
                    if (pFrameworkElement != null)
                    {
                        pDoNotFilterElementBecauseOfName = pFrameworkElementsToExclude.Contains(pFrameworkElement.Name);
                    }
                    else if (pFrameworkContentElement != null)
                    {
                        pDoNotFilterElementBecauseOfName = pFrameworkElementsToExclude.Contains(pFrameworkContentElement.Name);
                    }
                }

                if ((pElement is FrameworkElement || pElement is FrameworkContentElement) && pDoNotFilterElementBecauseOfName)
                {
                    var lFields = pElement.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

                    foreach (var iField in lFields)
                    {
                        if (typeof(DependencyProperty).IsAssignableFrom(iField.FieldType))
                        {
                            var lDP = iField.GetValue(pElement) as DependencyProperty;
                            if (lDP != null)
                            {
                                var lExpression = BindingOperations.GetBindingExpression(pElement, lDP);
                                if (lExpression != null)
                                {
                                    var val = lExpression.ParentBinding.ValidationRules.FirstOrDefault() as AttributeValidator;
                                    if (val != null && val.ExpressionToValidate == null)
                                    {
                                        val.ExpressionToValidate = lExpression;
                                    }

                                    lExpression.UpdateSource();

                                    if (_FirstInvalidElement == null && lExpression.HasError)
                                    {
                                        _FirstInvalidElement = (FrameworkElement)pElement;
                                    }
                                }
                            }
                        }
                    }
                }

                if (pElement is FrameworkElement)
                {
                    foreach (var iChildElement in LogicalTreeHelper.GetChildren(pElement).OfType<DependencyObject>())
                    {
                        TriggerValidation(iChildElement, pFrameworkElementsToExclude);
                    }
                }
            }
        }
    }
}