using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Data;
using DevExpress.Xpf.Grid;

namespace ValidationLibrary
{
    public static class ValidationTrigger
    {
        private static FrameworkElement _FirstInvalidElement;

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

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

            return _FirstInvalidElement == null;
        }

        private static void TriggerValidation(DependencyObject pElement)
        {
            if (!(pElement is GridControl))
            {
                if (pElement is FrameworkElement || pElement is FrameworkContentElement)
                {
                    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))
                    {
                        if (iChildElement is DependencyObject)
                        {
                            TriggerValidation((DependencyObject) iChildElement);
                        }
                    }
                }
            }
        }

        // static List<DependencyObject> GetChilds(DependencyObject pElement)
        // {
        // List<DependencyObject> result = new List<DependencyObject>();
        // for (int i = 0; i < VisualTreeHelper.GetChildrenCount(pElement); i++)
        // result.Add(VisualTreeHelper.GetChild(pElement, i));
        // return result;
        // }
    }
}