using AutoMapper;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;

namespace IRaCIS.Core.Application.Service
{
    public class AllocationConfig : Profile
    {
        public AllocationConfig()
        {

            CreateMap<TaskAllocationRule, TaskAllocationRuleView>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));

            List<Guid> subjectIdList = new List<Guid>();
            bool isJudgeDoctor = false;


            CreateMap<TaskAllocationRule, DoctorVisitTaskStatView>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName))
                 //.ForMember(o => o.ArmList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(c => c.TrialId == u.TrialId).Select(t => t.ArmEnum).Distinct()))
                 .ForMember(o => o.TotalTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count()))
                 .ForMember(o => o.SelfUndoTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId == u.DoctorUserId && t.ReadingTaskState != ReadingTaskState.HaveSigned)))
                 .ForMember(o => o.TotalSubjectCount, t => t.MapFrom(u => u.Trial.SubjectList.Count()))
                 .ForMember(o => o.SelfApplyedTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId == u.DoctorUserId)))
                 .ForMember(o => o.ApplyedTotalTaskCount, t => t.MapFrom(u => u.Trial.VisitTaskList.Count(t => t.DoctorUserId != null)))

                 .ForMember(o => o.SelfApplyedSubjectCount, t => t.MapFrom(u => u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && t.SubjectArmVisitTaskList.Any(c => c.DoctorUserId != null)).Count()))
                 .ForMember(o => o.SelfSubjectCount, t => t.MapFrom(u => u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId).Count()))

                 .ForMember(o => o.ApplyedTotalSubjectCount, t => t.MapFrom(u => u.Trial.SubjectList.Count(c => c.SubjectVisitTaskList.Any(d => d.DoctorUserId != null))))



                 //该医生未应用Subject 数量
                 .ForMember(o => o.WaitApplySelfSubjectCount, t => t.MapFrom(u =>
                  subjectIdList.Count == 0 ? u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                  : u.Trial.SubjectDoctorUserList.Where(t => t.DoctorUserId == u.DoctorUserId && subjectIdList.Contains(t.SubjectId) && t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                  ))


                 .ForMember(o => o.WaitApplyTotalSubjectCount, t => t.MapFrom(u =>
                  subjectIdList.Count == 0 ? u.Trial.SubjectList.Where(t => t.SubjectVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                  : u.Trial.SubjectList.Where(t => subjectIdList.Contains(t.Id) && t.SubjectVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any(c => c.DoctorUserId == null)).Count()
                 ))

                 .ForMember(o => o.WaitApplySelfTaskCount, t => t.MapFrom(u =>
                 subjectIdList.Count == 0 ? u.Trial.SubjectDoctorUserList.Where(d => d.DoctorUserId == u.DoctorUserId).SelectMany(t => t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null)).Count()
                 : u.Trial.SubjectDoctorUserList.Where(d => d.DoctorUserId == u.DoctorUserId && subjectIdList.Contains(d.SubjectId)).SelectMany(t => t.SubjectArmVisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null)).Count()
                                                                ))

                  .ForMember(o => o.WaitApplyTotalTaskCount, t => t.MapFrom(u =>
                         subjectIdList.Count == 0 ? u.Trial.VisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null).Count()
                 : u.Trial.VisitTaskList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.DoctorUserId == null && subjectIdList.Contains(t.SubjectId)).Count()))





                 ;





            CreateMap<SubjectVisit, VisitGenerataTaskDTO>();


            CreateMap<VisitTask, VisitTaskView>()
                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));




            CreateMap<VisitTask, JudgeVisitTaskView>()
                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName))

                 .ForMember(o => o.HistoryReadingDoctorUserList, t => t.MapFrom(u => u.JudgeVisitList));

            CreateMap<VisitTask, ReadingTaskView>()
                  .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));



            CreateMap<VisitTaskReReading, IRReReadingTaskView>().IncludeMembers(t => t.OriginalReReadingTask)
                 .ForMember(o => o.ReReadingOriginalTaskCode, t => t.MapFrom(u => u.OriginalReReadingTask.TaskCode))
                 .ForMember(o => o.Id, t => t.MapFrom(u => u.Id))

                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.OriginalReReadingTask.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.OriginalReReadingTask.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.OriginalReReadingTask.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.OriginalReReadingTask.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.OriginalReReadingTask.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.OriginalReReadingTask.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.OriginalReReadingTask.DoctorUser.UserTypeRole.UserTypeShortName)); 

            CreateMap<VisitTask, IRReReadingTaskView>()
               .ForMember(o => o.Id, t => t.Ignore());



            CreateMap<VisitTaskReReading, ReReadingTaskView>().IncludeMembers(t => t.NewReReadingTask)
                 .ForMember(o => o.ReReadingOriginalTaskCode, t => t.MapFrom(u => u.OriginalReReadingTask.TaskCode))
                 .ForMember(o => o.Id, t => t.MapFrom(u => u.Id))

                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.NewReReadingTask.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.NewReReadingTask.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.NewReReadingTask.Subject.Code))
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.NewReReadingTask.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.NewReReadingTask.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.NewReReadingTask.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.NewReReadingTask.DoctorUser.UserTypeRole.UserTypeShortName));

            CreateMap<VisitTask, ReReadingTaskView>()
                .ForMember(o => o.Id, t => t.Ignore());

            CreateMap<VisitTask, IRHaveReadView>()
                 .ForMember(o => o.SiteId, t => t.MapFrom(u => u.Subject.SiteId))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Subject.Code))
                 ;




            CreateMap<VisitTask, HistoryReadingDoctorUser>()
                 .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.JudgeTaskCode, t => t.MapFrom(u => u.TaskCode))
                 .ForMember(o => o.DoctorUserId, t => t.MapFrom(u => u.DoctorUser.Id));


            CreateMap<Subject, SubjectAssignView>()
                 .ForMember(o => o.SubjectId, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.TrialSite.TrialSiteCode))
                 .ForMember(o => o.SubjectCode, t => t.MapFrom(u => u.Code))
                 .ForMember(o => o.IsJudge, t => t.MapFrom(u => isJudgeDoctor))
                 .ForMember(o => o.IsAssignedDoctorUser, t => t.MapFrom(u => u.SubjectDoctorList.Where(t => isJudgeDoctor ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Any()))


                 .ForMember(o => o.TotalDoctorUserList, t => t.MapFrom(u => u.SubjectDoctorList.Where(t => isJudgeDoctor ? true : t.ArmEnum != Arm.JudgeArm).OrderBy(t => t.ArmEnum)));

            CreateMap<SubjectUser, AssignDoctorView>()
                //.ForMember(o => o.AssignTime, t => t.MapFrom(u => u.AssignTime))
                .ForMember(o => o.UserCode, t => t.MapFrom(u => u.DoctorUser.UserCode))
                 .ForMember(o => o.UserName, t => t.MapFrom(u => u.DoctorUser.UserName))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.DoctorUser.FullName))
                 .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.DoctorUser.UserTypeRole.UserTypeShortName));



            CreateMap<TaskAllocationRuleAddOrEdit, TaskAllocationRule>();


            CreateMap<User, TrialDoctorUserSelectView>()
                 .ForMember(o => o.DoctorUserId, t => t.MapFrom(u => u.Id))
                 .ForMember(o => o.FullName, t => t.MapFrom(u => u.FullName));

            CreateMap<TaskMedicalReview, TaskMedicalReviewView>().IncludeMembers(t => t.VisitTask)
                 .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.VisitTask.DoctorUser))
                 .ForMember(o => o.MedicalManagerUser, t => t.MapFrom(u => u.MedicalManagerUser));
            CreateMap<VisitTask, TaskMedicalReviewView>()
                .ForMember(o => o.TrialSiteCode, t => t.MapFrom(u => u.Subject.TrialSite.TrialSiteCode));

            CreateMap<User, UserSimpleInfo>()

                  .ForMember(o => o.UserId, t => t.MapFrom(u => u.Id))
             .ForMember(o => o.UserCode, t => t.MapFrom(u => u.UserCode))
              .ForMember(o => o.UserName, t => t.MapFrom(u => u.UserName))
              .ForMember(o => o.FullName, t => t.MapFrom(u => u.FullName))
              .ForMember(o => o.UserTypeShortName, t => t.MapFrom(u => u.UserTypeRole.UserTypeShortName));



            CreateMap<TaskMedicalReviewRule, TaskMedicalReviewRuleView>()
              .ForMember(o => o.DoctorUser, t => t.MapFrom(u => u.DoctorUser))
              .ForMember(o => o.ActualVisitTaskList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Visit && t.TaskState == TaskState.Effect && t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))
              .ForMember(o => o.ActualJudgeTaskList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Judge && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))
              .ForMember(o => o.ActualGlobalTaskList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Global && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))
              .ForMember(o => o.ActualTumorTaskList, t => t.MapFrom(u => u.DoctorVisitTaskList.Where(t => t.ReadingCategory == ReadingCategory.Oncology && t.TaskState == TaskState.Effect &&
              t.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.Id, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))

              .ForMember(o => o.GeneratedGlobalTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Global && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))
              .ForMember(o => o.GeneratedJudgeTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Judge && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })))
              .ForMember(o => o.GeneratedTumorTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Oncology && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() {TaskId = t.VisitTaskId,TrialId=t.TrialId,DoctorUserId=t.DoctorUserId })))
              .ForMember(o => o.GeneratedVisitTaskList, t => t.MapFrom(u => u.TaskMedicalReviewList.Where(t => t.VisitTask.ReadingCategory == ReadingCategory.Visit && t.VisitTask.TaskState == TaskState.Effect && t.VisitTask.ReadingTaskState == ReadingTaskState.HaveSigned).OrderBy(t => t.VisitTask.SignTime).Select(t => new TaskBasicIdView() { TaskId = t.VisitTaskId, TrialId = t.TrialId, DoctorUserId = t.DoctorUserId })));

            CreateMap<TaskMedicalReviewRuleAddOrEdit, TaskMedicalReviewRule>();




        }
    }

}
