using Exam.Infrastructure.Extensions;
using Exam.Share;
using Hotline.Share.Requests.Exam;
using Hotline.Share.Requests.Train;
using JiebaNet.Segmenter.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Hotline.Application.Exam.Core.Utilities;
using Hotline.Exams.ExamManages;

namespace Hotline.Application.Exam.QueryExtensions.ExamManages
{
    public static class UserExamQueryExtensions
    {
        public static Expression<Func<ExamUserExam,bool>> GetExpression(this UserExamPagedRequest userExamPagedRequest)
        {
            Expression<Func<ExamUserExam, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamUserExam>()
                .AndIF(userExamPagedRequest.MinScore.IsNotNull(), x => x.Score >= userExamPagedRequest.MinScore)
                .AndIF(userExamPagedRequest.MaxScore.IsNotNull(), x => x.Score <= userExamPagedRequest.MaxScore)
                .And(x=>x.UserId == userExamPagedRequest.UserId)
                .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamManage,bool>> GetExamManageExpression(this UserExamPagedRequest userExamPagedRequest)
        {
            Expression<Func<ExamManage, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamManage>()
                .AndIF(userExamPagedRequest.Name.IsNotNullOrEmpty(), x => x.Name.Contains(userExamPagedRequest.Name))
                .AndIF(userExamPagedRequest.ExamType.IsNotNull(), x => x.ExamType == userExamPagedRequest.ExamType)
                .AndIF(userExamPagedRequest.MinStartTime.IsNotNull(),x=>x.StartTime >= userExamPagedRequest.MinStartTime)
                .AndIF(userExamPagedRequest.MaxStartTime.IsNotNull(),x=>x.StartTime <= userExamPagedRequest.MaxStartTime)
                .AndIF(userExamPagedRequest.MinEndTime.IsNotNull(), x => x.EndTime >= userExamPagedRequest.MinEndTime)
                .AndIF(userExamPagedRequest.MaxEndTime.IsNotNull(),x=>x.EndTime <= userExamPagedRequest.MaxEndTime)
                .AndIF(userExamPagedRequest.MinTotalScore.IsNotNull(),x=>x.TotalScore >= userExamPagedRequest.MinTotalScore)
                .AndIF(userExamPagedRequest.MaxTotalScore.IsNotNull(),x=>x.TotalScore <= userExamPagedRequest.MaxTotalScore)
                .ToExpression();

            return expression;
        }

    
        public static Expression<Func<ExamUserExam,bool>> GetExpression(this GradingExamRequest gradingExamRequest)
        {
            Expression<Func<ExamUserExam, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamUserExam>()
                .AndIF(gradingExamRequest.ExamId.IsNotNullOrEmpty(), x => x.ExamId == gradingExamRequest.ExamId)
                .AndIF(gradingExamRequest.UserId.IsNotNullOrEmpty(), x => x.UserId == gradingExamRequest.UserId).ToExpression();

            return expression;
        }

        public static Expression<Func<ExamManage,bool>> GetExpression(this UnExamUserReportPagedRequest unExamUserReportPagedRequest)
        {
            Expression<Func<ExamManage, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamManage>()
            .AndIF(unExamUserReportPagedRequest.StartTime.IsNotNull(), x => x.StartTime >= unExamUserReportPagedRequest.StartTime)
            .AndIF(unExamUserReportPagedRequest.EndTime.IsNotNull(), x => x.StartTime <= unExamUserReportPagedRequest.EndTime)
            .AndIF(unExamUserReportPagedRequest.Keyword.IsNotNull(), x => x.Name.Contains(unExamUserReportPagedRequest.Keyword))
            .And(x=>x.ExamStatus == Share.Enums.Exams.EExamStatus.Complete)
            .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamUserExam, bool>> GetUserExamExpression(this UnExamUserReportPagedRequest unExamUserReportPagedRequest)
        {
            Expression<Func<ExamUserExam, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamUserExam>()
                .And(x=>x.ExamStatus == Share.Enums.Exams.EExamStatus.Absent)
            .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamManage,bool>> GetExpression(this UserExamResultReportPagedRequest userExamResultReportPagedRequest)
        {
            Expression<Func<ExamManage, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamManage>()
            .AndIF(userExamResultReportPagedRequest.StartTime.IsNotNull(), x => x.StartTime >= userExamResultReportPagedRequest.StartTime)
            .AndIF(userExamResultReportPagedRequest.EndTime.IsNotNull(), x => x.StartTime <= userExamResultReportPagedRequest.EndTime)
            .AndIF(userExamResultReportPagedRequest.Keyword.IsNotNull(), x => x.Name.Contains(userExamResultReportPagedRequest.Keyword))
            .ToExpression();

            return expression;
        }

        public static Expression<Func<ExamUserExam, bool>> GetUserExamExpression(this UserExamResultReportPagedRequest userExamResultReportPagedRequest)
        {
            Expression<Func<ExamUserExam, bool>> expression = m => m.Id != null;

            expression = ExpressionableUtility.CreateExpression<ExamUserExam>()
            .AndIF(userExamResultReportPagedRequest.MinScore.IsNotNull(), x => x.Score >= userExamResultReportPagedRequest.MinScore)
            .AndIF(userExamResultReportPagedRequest.MaxScore.IsNotNull() && userExamResultReportPagedRequest.MinScore.IsNull(), x => x.Score <= userExamResultReportPagedRequest.MaxScore || x.Score== null)
            .AndIF(userExamResultReportPagedRequest.MaxScore.IsNotNull() && userExamResultReportPagedRequest.MinScore.IsNotNull(), x => x.Score <= userExamResultReportPagedRequest.MaxScore)
            .And(x=>x.ExamStatus == Share.Enums.Exams.EExamStatus.Complete)
            .ToExpression();

            return expression;
        }
    }
}
