using Exam.Infrastructure.Extensions;
using Hotline.Application.Exam.Extensions;
using Hotline.Exams.Trains;
using Hotline.Share.Requests.Train;
using JiebaNet.Segmenter.Common;
using System.Linq.Expressions;
using Hotline.Application.Exam.Core.Utilities;

namespace Hotline.Application.Exam.QueryExtensions.Trains
{
    public static class TrainPlanQueryExtensions
    {
        public static Expression<Func<ExamTrainPlan,bool>> GetExpression(this TrainPlanPagedRequest trainPlanPagedRequest)
        {
            Expression<Func<ExamTrainPlan, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainPlan>()
                .AndIF(trainPlanPagedRequest.Code.IsNotNullOrEmpty(), x => x.Code.Contains(trainPlanPagedRequest.Code))
                .AndIF(trainPlanPagedRequest.Name.IsNotNullOrEmpty(), x => x.Name.Contains(trainPlanPagedRequest.Name))
                .AndIF(trainPlanPagedRequest.MaxStartTime.IsNotNull(), x => x.TrainStartTime <= trainPlanPagedRequest.MaxStartTime)
                .AndIF(trainPlanPagedRequest.MinStartTime.IsNotNull(), x => x.TrainEndTime >= trainPlanPagedRequest.MinStartTime)
                .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamTrainPlanTemplate,bool>> GetTemplateExpression(this TrainPlanPagedRequest trainPlanPagedRequest)
        {
            Expression<Func<ExamTrainPlanTemplate, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainPlanTemplate>()
                .AndIF(trainPlanPagedRequest.TrainTemplateIds.IsNotNullOrEmpty(), x => trainPlanPagedRequest.TrainTemplateIds.Contains(x.TrainTemplateId))
                .ToExpression();

            return expression;
        }


        public static Expression<Func<ExamTrainPlan, bool>> GetExpression(this TrainResultReportPagedRequest trainResultReportPagedRequest)
        {
            Expression<Func<ExamTrainPlan, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainPlan>()
                .AndIF(trainResultReportPagedRequest.StartTime.IsNotNull(), x => x.TrainStartTime >= trainResultReportPagedRequest.StartTime)
                .AndIF(trainResultReportPagedRequest.EndTime.IsNotNull(), x => x.TrainStartTime <= trainResultReportPagedRequest.EndTime)
                .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamTrainTemplate, bool>> GetTemplateExpression(this TrainResultReportPagedRequest trainResultReportPagedRequest)
        {
            Expression<Func<ExamTrainTemplate, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainTemplate>()
                .AndIF(trainResultReportPagedRequest.Keyword.IsNotNullOrEmpty(), x => x.Name.Contains(trainResultReportPagedRequest.Keyword))
                .ToExpression();

            return expression;
        }
    }
}
