using AutoMapper;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Service.Reading.Dto;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Infra.EFCore.Common;

namespace IRaCIS.Core.Application.Service
{
    public class ReadingConfig : Profile
    {
        public ReadingConfig()
        {
            CreateMap<ReadingPeriodSetAddOrEdit, ReadingPeriodSet>();

            CreateMap<AddOrUpdateTumorAssessmentInDto, TumorAssessment>();

            CreateMap<ReadingPeriodSet, ReadingPeriodSetView>()
                .ForMember(d => d.SubjectVisitName, u => u.MapFrom(s => s.VisitStage==null?string.Empty: s.VisitStage.VisitName))
                .ForMember(d => d.SiteIds, u => u.MapFrom(s => s.ReadingPeriodSites.Select(x => x.SiteId)))
                .ForMember(d => d.SiteCodes, u => u.MapFrom(s => s.ReadingPeriodSites.Select(x => x.TrialSite.TrialSiteCode)))
                .ForMember(d => d.PlanCount, u => u.MapFrom(s => s.ReadingPeriodPlanList.Count));

            //CreateMap<ReadingClinicalDataView, GetReadingClinicalDataListOutDto>();
           // CreateMap<ReadingClinicalData, GetReadingClinicalDataListOutDto>()
           //.ForMember(d => d.ClinicalDataLevel, u => u.MapFrom(s => s.ClinicalDataTrialSet.ClinicalDataLevel))
           //.ForMember(d => d.ClinicalDataLevelName, u => u.MapFrom(s => s.ClinicalDataTrialSet.ClinicalDataSetName))
           //.ForMember(d => d.ClinicalUploadType, u => u.MapFrom(s => s.ClinicalDataTrialSet.ClinicalUploadType));
           //.ForMember(d => d.FileCount, u => u.MapFrom(s => s.ReadingClinicalDataPDFList.Count()));

            CreateMap<ReadingClinicalDataPDF, GetReadingClinicalDataPDFListOutDto>();

            //.ForMember(d => d.SiteNames, u => u.MapFrom(s => s.ReadingPeriodSites.SelectMany(x => x.Site.SiteName).ToList()));

            CreateMap<ReadingClinicalData, AddOrUpdateReadingClinicalDataDto>().ReverseMap();

            CreateMap<ReadingClinicalDataPDF, AddOrUpdateReadingClinicalDataPDFDto>().ReverseMap();

            CreateMap<ClinicalDataTrialSet, ClinicalDataTrialSetAddOrEdit>().ReverseMap();

            CreateMap<ClinicalDataSystemSet, ClinicalDataSystemSetAddOrEdit>().ReverseMap();

            CreateMap<ClinicalDataTrialSet, ClinicalDataTrialSetView>()
                    .ForMember(d => d.IsUsed, u => u.MapFrom(s => s.ReadingClinicalDataList.Count()>0));
            CreateMap<ClinicalDataSystemSet, ClinicalDataSystemSetView>();

            CreateMap<PreviousPDF, PreviousPDFAddOrEdit>().ReverseMap();

            CreateMap<PreviousPDF, PreviousPDFView>();

            CreateMap<ReadModuleAddOrEdit, ReadModule>();

            CreateMap<TumorAssessment, TumorAssessmentView>();

            CreateMap<ReadingTableAnswerRowInfo, CopyTableAnswerRowInfo>()
                .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id))
                 .ForMember(dest => dest.ReadingQuestionTrial, opt => opt.Ignore());

            #region 阅片问题

            // 忽略列
            CreateMap<ReadingQuestionTrial, DicomReadingQuestionAnswer>()
                 .ForMember(dest => dest.ReadingCriterionPage, opt => opt.Ignore())
                 .ForMember(dest => dest.ParentReadingQuestionTrial, opt => opt.Ignore())
                 .ForMember(dest => dest.RelevanceReadingQuestionTrial, opt => opt.Ignore())
                 .ForMember(dest => dest.ReadingQuestionCriterionTrial, opt => opt.Ignore());


            CreateMap<ReadingTableAnswerRowInfo, GetTableAnswerRowInfoOutDto>()
                  .ForMember(d => d.ShowOrder, u => u.MapFrom(s => s.ReadingQuestionTrial.ShowOrder))
                   .ForMember(d => d.RowIndex, u => u.MapFrom(s => s.RowIndex.ToString()))
                   .ForMember(d => d.RowId, u => u.MapFrom(s => s.Id));



            CreateMap<ReadingTableQuestionTrial, TableQuestionTrial>();
            CreateMap<ReadingTableQuestionAnswer, ReadingTableQuestionAnswerInfo>()
                 .ForMember(d => d.ShowOrder, u => u.MapFrom(s => s.ReadingQuestionTrial.ShowOrder));

            CreateMap<ReadingQuestionTrial, TrialReadQuestionData>()
                .ForMember(d => d.PageShowOrder, u => u.MapFrom(s => s.ReadingCriterionPage.ShowOrder))
                .ForMember(d => d.PageName, u => u.MapFrom(s => s.ReadingCriterionPage.PageName))
                .ForMember(d => d.IsPublicPage, u => u.MapFrom(s => s.ReadingCriterionPage.IsPublicPage));
            CreateMap<ReadingQuestionSystem, GetSystemReadingQuestionOutDto>();

            CreateMap<ReadingTableQuestionSystem, TableQuestionDataInfo>();
            CreateMap<ReadingTableQuestionTrial, TableQuestionDataInfo>();

            CreateMap<ReadingTableQuestionSystem, ReadingTableQuestionSystemView>()
                    .ForMember(d => d.DependShowOrder, u => u.MapFrom(s => s.DependParentQuestion.ShowOrder));


            CreateMap<ReadingTableQuestionTrial, ReadingTableQuestionTrialView>()
                  .ForMember(d => d.DependShowOrder, u => u.MapFrom(s => s.DependParentQuestion.ShowOrder));
            CreateMap<ReadingTableQuestionSystemAddOrEdit, ReadingTableQuestionSystem>();

            CreateMap<ReadingTableQuestionTrialAddOrEdit, ReadingTableQuestionTrial>();


            CreateMap<ReadingCriterionPageAddOrEdit, ReadingCriterionPage>();

            CreateMap<ReadingQuestionTrial, ReadingQuestionSystem>();

            CreateMap<Dictionary, OrganDictionary>();

            CreateMap<ReadingQuestionSystem, ReadingQuestionSystemData>()
                  .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id));

            CreateMap<ReadingTableQuestionSystem, ReadingTableQuestionSystemData>()
               .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id));

            CreateMap<ReadingQuestionSystem, ReadingQuestionTrial>()
                  .ForMember(d => d.ReadingQuestionSystemId, u => u.MapFrom(s => s.Id));

            CreateMap<ReadingTableAnswerRowInfo, TableAnsweRowInfo>()
             .ForMember(d => d.MergeName, u => u.MapFrom(s => s.MergeRow == null ? string.Empty : s.MergeRow.ReadingQuestionTrial.OrderMark + s.MergeRow.RowIndex.GetLesionMark()))
             .ForMember(d => d.SplitName, u => u.MapFrom(s => s.SplitRow == null ? string.Empty : s.SplitRow.ReadingQuestionTrial.OrderMark + s.SplitRow.RowIndex.GetLesionMark()));


            CreateMap<ReadingTableQuestionSystem, ReadingTrialTableQuestionData>()
             .ForMember(dest => dest.DependParentQuestion, opt => opt.Ignore())
         .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id));

            CreateMap<ReadingTableQuestionTrial, ReadingTrialTableQuestionData>()
                .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id));

            CreateMap<ReadingQuestionCriterionSystem, ReadingQuestionCriterionTrial>()
                 .ForMember(dest => dest.ReadingQuestionTrialList, opt => opt.Ignore())
                 .ForMember(d => d.ReadingQuestionCriterionSystemId, u => u.MapFrom(s => s.Id)); ;
                

            CreateMap<AddOrUpdateReadingQuestionCriterionSystemInDto, ReadingQuestionCriterionSystem>();
            CreateMap<ReadingQuestionCriterionSystem, ReadingQuestionCriterionSystemView>()
                  .ForMember(d => d.QuestionCount, u => u.MapFrom(s => s.ReadingQuestionSystemList.Count()));
                   //.ForMember(d => d.IsEnable, u => u.MapFrom(s => s.Dictionary.IsEnable))
                   //.ForMember(d => d.ShowOrder, u => u.MapFrom(s => s.Dictionary.ShowOrder)); 


            CreateMap<AddOrUpdateReadingQuestionSystemInDto, ReadingQuestionSystem>();
            CreateMap<ReadingQuestionSystem, ReadingQuestionSystemView>()
           .ForMember(d => d.ParentQuestionName, u => u.MapFrom(s => s.ParentReadingQuestionSystem==null?string.Empty: s.ParentReadingQuestionSystem.QuestionName))
             .ForMember(d => d.ParentQuestionShowOrder, u => u.MapFrom(s => s.ParentReadingQuestionSystem.ShowOrder))
             .ForMember(d => d.RelevanceShowOrder, u => u.MapFrom(s => s.RelevanceReadingQuestionSystem.ShowOrder));


            CreateMap<AddOrUpdateReadingQuestionCriterionTrialInDto, ReadingQuestionCriterionTrial>();
            CreateMap<ReadingQuestionCriterionTrial, ReadingQuestionCriterionTrialView>()
                 .ForMember(d => d.QuestionCount, u => u.MapFrom(s => s.ReadingQuestionTrialList.Count())); 


            CreateMap<AddOrUpdateReadingQuestionTrialInDto, ReadingQuestionTrial>();
            CreateMap<ReadingQuestionTrial, ReadingQuestionTrialView>()
                 .ForMember(d => d.ParentQuestionName, u => u.MapFrom(s => s.ParentReadingQuestionTrial == null ? string.Empty : s.ParentReadingQuestionTrial.QuestionName))
            .ForMember(d => d.ParentQuestionShowOrder, u => u.MapFrom(s => s.ParentReadingQuestionTrial.ShowOrder))
              .ForMember(d => d.RelevanceShowOrder, u => u.MapFrom(s => s.RelevanceReadingQuestionTrial.ShowOrder));
            #endregion

            #region IR阅片
            CreateMap<ReadingQuestionTrial, GetTrialReadingQuestionOutDto>()
                .ForMember(x=>x.Id, y=>y.MapFrom(z=>z.Id));

            CreateMap<VisitTask, GetReadingPastResultListOutDto>()
                  .ForMember(x => x.VisitTaskId, y => y.MapFrom(z => z.Id));
            #endregion

            #region 医学审核
            CreateMap<ReadingMedicineSystemQuestionAddOrEdit, ReadingMedicineSystemQuestion>();
            CreateMap<ReadingMedicineSystemQuestion, ReadingMedicineSystemQuestionView>()
                .ForMember(x => x.ParentShowOrder, y => y.MapFrom(n => n.ParentQuestion.ShowOrder));


            CreateMap<TrialDataFromSystem, ReadingMedicineTrialQuestion>();

            CreateMap<ReadingMedicineTrialQuestionAddOrEdit, ReadingMedicineTrialQuestion>();
            CreateMap<ReadingMedicineTrialQuestion, ReadingMedicineTrialQuestionView>()
                .ForMember(x=>x.ParentShowOrder,y=>y.MapFrom(n=>n.ParentQuestion.ShowOrder));

            CreateMap<ReadingMedicineTrialQuestion, GetMedicineQuestionPreviewOutDto>();


            CreateMap<TaskMedicalReview, MedicalReviewInfo>()
                .ForMember(x => x.TaskMedicalReviewId, y => y.MapFrom(n => n.Id));

            CreateMap<ReadingMedicalReviewDialog, GetMedicalReviewDialogOutDto>()
                 .ForMember(x => x.CreateUserName, y => y.MapFrom(n => n.CreateUser.UserName));

            #endregion

            #region 阅片部位


            CreateMap<CriterionNidus, CriterionNidusView>();
            CreateMap<CriterionNidus, CriterionNidusAddOrEdit>().ReverseMap();

            CreateMap<OrganInfoAddOrEdit, OrganInfo>();
            CreateMap<OrganInfo, OrganInfoView>(); 
            #endregion
        }
    }
   
}
