using Exam.Infrastructure.Extensions;
using Hotline.Exams.Trains;
using Hotline.Share.Requests.Exam;
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 TrainRecordQueryExtensions
    {
        public static Expression<Func<ExamTrainPlan,bool>> GetExpression(this TrainRecordPagedRequest trainRecordPagedRequest)
        {
            Expression<Func<ExamTrainPlan, bool>> expression = m => m.Id != null;

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

            return expression;
        }

        public static Expression<Func<ExamTrainRecord, bool>> GetTrainRecordExpression(this TrainRecordPagedRequest trainRecordPagedRequest)
        {
            Expression<Func<ExamTrainRecord, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainRecord>()
                .AndIF(trainRecordPagedRequest.UserId.IsNotNull(), x => x.UserId == trainRecordPagedRequest.UserId)
                .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamTrainPractice, bool>> GetExpression(this TrainQuestionRequest trainQuestionRequest)
        {
            Expression<Func<ExamTrainPractice, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainPractice>()
                .AndIF(trainQuestionRequest.QuestionId.IsNotNullOrEmpty(), x => x.QuestionId == trainQuestionRequest.QuestionId).ToExpression();

            return expression;
        }

        public static Expression<Func<ExamTrainPractice,bool>> GetExpression(this TrainPracticeRequest trainPracticeRequest)
        {
            Expression<Func<ExamTrainPractice, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamTrainPractice>()
                .AndIF(trainPracticeRequest.TrainPracticeId.IsNotNullOrEmpty(), x => x.Id == trainPracticeRequest.TrainPracticeId).ToExpression();

            return expression;
        }
    }
}
