using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ZeroLevel.Specification
{
    /// <summary>
    /// Enables the efficient, dynamic composition of query predicates.
    /// </summary>
    public static class PredicateBuilder
    {
        /// <summary>
        /// Creates a predicate that evaluates to true.
        /// </summary>
        public static Expression<Func<T, bool>> True<T>() { return param => true; }

        /// <summary>
        /// Creates a predicate that evaluates to false.
        /// </summary>
        public static Expression<Func<T, bool>> False<T>() { return param => false; }

        #region Creates a predicate expression from the specified lambda expression.

        /// <summary>
        /// Creates a predicate expression from the specified lambda expression.
        /// </summary>
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate)
        { return predicate; }

        /// <summary>
        /// Creates a predicate expression from the specified lambda expression.
        /// </summary>
        public static Expression<Func<T, bool>> CreateFromFunc<T>(Func<T, bool> predicate)
        {
            var method = predicate.Method;
            var parameters = method.GetParameters().Select(p => Expression.Variable(p.ParameterType)).ToArray();
            var target = predicate?.Target;
            var call = Expression.Call(predicate.Method.IsStatic ? null : Expression.New(target.GetType()),
                method,
                parameters);
            return Expression.Lambda<Func<T, bool>>(call, parameters);
        }

        #endregion Creates a predicate expression from the specified lambda expression.

        #region Combines the first predicate with the second using the logical "and".

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.AndAlso);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> AndFunc<T>(this Expression<Func<T, bool>> first, Func<T, bool> second)
        {
            return first.Compose(CreateFromFunc(second), Expression.AndAlso);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> And<T>(this Func<T, bool> first, Expression<Func<T, bool>> second)
        {
            return CreateFromFunc(first).Compose(second, Expression.AndAlso);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> AndFunc<T>(this Func<T, bool> first, Func<T, bool> second)
        {
            return CreateFromFunc(first).Compose(CreateFromFunc(second), Expression.AndAlso);
        }

        #endregion Combines the first predicate with the second using the logical "and".

        #region Combines the first predicate with the second using the logical "or".

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.OrElse);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> OrFunc<T>(this Expression<Func<T, bool>> first, Func<T, bool> second)
        {
            return first.Compose(CreateFromFunc(second), Expression.OrElse);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> Or<T>(this Func<T, bool> first, Expression<Func<T, bool>> second)
        {
            return CreateFromFunc(first).Compose(second, Expression.OrElse);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> OrFunc<T>(this Func<T, bool> first, Func<T, bool> second)
        {
            return CreateFromFunc(first).Compose(CreateFromFunc(second), Expression.OrElse);
        }

        #endregion Combines the first predicate with the second using the logical "or".

        #region Negates the predicate.

        /// <summary>
        /// Negates the predicate.
        /// </summary>
        public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
        {
            var negated = Expression.Not(expression.Body);
            return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
        }

        /// <summary>
        /// Negates the predicate.
        /// </summary>
        public static Expression<Func<T, bool>> Not<T>(this Func<T, bool> predicate)
        {
            var expression = CreateFromFunc(predicate);
            var negated = Expression.Not(expression.Body);
            return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
        }

        #endregion Negates the predicate.

        #region Helpers

        /// <summary>
        /// Combines the first expression with the second using the specified merge function.
        /// </summary>
        private static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            // zip parameters (map from parameters of second to parameters of first)
            var map = first.Parameters
                .Select((f, i) => new { f, s = second.Parameters[i] })
                .ToDictionary(p => p.s, p => p.f);

            // replace parameters in the second lambda expression with the parameters in the first
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

            // create a merged lambda expression with parameters from the first expression
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        private class ParameterRebinder : ExpressionVisitor
        {
            private readonly Dictionary<ParameterExpression, ParameterExpression> map;

            private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
            {
                this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
            }

            public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
            {
                return new ParameterRebinder(map).Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression p)
            {
                ParameterExpression replacement;
                if (map.TryGetValue(p, out replacement))
                {
                    p = replacement;
                }
                return base.VisitParameter(p);
            }
        }

        #endregion Helpers
    }
}