using System;
using System.Linq.Expressions;
using VCommon.Linq.Expressions.Predicate;
using VCommon.VApplication.Dto;
using VCommon.VEntity;

namespace VCommon.VApplication.Linq.Expressions
{
    public static class PredicateExpressionBuilderExtensionMethods
    {
        public static PredicateWrap<T> DateTimeBetween<T>(this PredicateBuilder<T> me, Expression<Func<T, DateTime?>> selector, DateTimeRange filter)
        {
            return null == filter ? null
                : me.Between(selector, filter.BeginTime, filter.EndTime);
        }

        public static PredicateWrap<T> FilterByEnable<T>(this PredicateBuilder<T> me, IPassiveFilter filter) where T : IPassive
        {
            return true != filter?.IsEnable.HasValue ? null
                : me.Equal(ExpressionHelper.Property<T, bool>(nameof(IPassive.IsEnable)), filter.IsEnable.Value);
        }

        public static PredicateWrap<T> SearchByName<T>(this PredicateBuilder<T> me, IStringSearchFilter filter) where T : IHaveName
        {
            return string.IsNullOrWhiteSpace(filter?.Search) ? null
                : me.StringContains(ExpressionHelper.Property<T, string>(nameof(IHaveName.Name)), filter.Search);
        }

        public static PredicateWrap<T> SearchBy<T>(this PredicateBuilder<T> me, Expression<Func<T, string>> selector, IStringSearchFilter filter)
        {
            return string.IsNullOrWhiteSpace(filter?.Search) ? null
                : me.StringContains(selector, filter.Search);
        }
    }
}