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()
        {


            //是否英文环境
            var isEn_Us=false;

            CreateMap<ShortcutKey, DefaultShortcutKeyView>();

            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<ReadingCriterionDictionary, CriterionDictionaryInfo>()
             .ForMember(d => d.ChildGroup, u => u.MapFrom(s => s.Dictionary.ChildGroup))
            .ForMember(d => d.Code, u => u.MapFrom(s => s.Dictionary.Code))
             .ForMember(d => d.Description, u => u.MapFrom(s => s.Dictionary.Description))
              .ForMember(d => d.ShowOrder, u => u.MapFrom(s => s.Dictionary.ShowOrder))
               .ForMember(d => d.ParentCode, u => u.MapFrom(s => s.Dictionary.Parent.Code))
                .ForMember(d => d.Value, u => u.MapFrom(s => s.Dictionary.Value))
                 .ForMember(d => d.ValueCN, u => u.MapFrom(s => s.Dictionary.ValueCN));



            //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(t => t.TrialCriterionNameList, u => u.MapFrom(c => c.TrialClinicalDataSetCriteriaList.Select(t => t.TrialReadingCriterion.CriterionName)))
                    .ForMember(t => t.TrialCriterionIdList, u => u.MapFrom(c => c.TrialClinicalDataSetCriteriaList.Select(t => t.TrialReadingCriterion.Id)))
                    .ForMember(d => d.IsUsed, u => u.MapFrom(s => s.ReadingClinicalDataList.Count()>0));

            CreateMap<ClinicalDataSystemSet, ClinicalDataSystemSetView>();
                //.ForMember(t=>t.SystemCriterionNameList,u=>u.MapFrom(c=>c.SystemClinicalDataCriterionList.Select(t=>t.SystemReadingCriterion.CriterionName)))
                //.ForMember(t => t.SystemCriterionIdList, u => u.MapFrom(c => c.SystemClinicalDataCriterionList.Select(t => t.SystemReadingCriterion.Id)));

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

            CreateMap<PreviousPDF, PreviousPDFView>();

            CreateMap<ReadModuleAddOrEdit, ReadModule>();

            CreateMap<TumorAssessment, TumorAssessmentView>();


            CreateMap<CopyTableAnswerRowInfo, ReadingTableAnswerRowInfo>();

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

            #region 阅片问题

            
            // 忽略列
            CreateMap<ReadingQuestionTrial, DicomReadingQuestionAnswer>()
                    .ForMember(d => d.QuestionName, u => u.MapFrom(s => isEn_Us?s.QuestionEnName:s.QuestionName))
                 .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.OrderMark, u => u.MapFrom(s => s.ReadingQuestionTrial.OrderMark))
                   .ForMember(d => d.RowIndex, u => u.MapFrom(s => s.RowIndex.ToString()))
                   .ForMember(d => d.RowIndexNum, u => u.MapFrom(s => s.RowIndex))
                   .ForMember(d => d.RowId, u => u.MapFrom(s => s.Id));



            CreateMap<ReadingTableQuestionTrial, TableQuestionTrial>()
                .ForMember(d => d.QuestionName, u => u.MapFrom(s => isEn_Us ? s.QuestionEnName : s.QuestionName));
            CreateMap<ReadingTableQuestionAnswer, ReadingTableQuestionAnswerInfo>()
                 .ForMember(d => d.ShowOrder, u => u.MapFrom(s => s.ReadingQuestionTrial.ShowOrder))
                   .ForMember(d => d.QuestionMark, u => u.MapFrom(s => s.ReadingTableQuestionTrial.QuestionMark));

            CreateMap<ReadingQuestionTrial, TrialReadQuestionData>()
                  .ForMember(d => d.QuestionName, u => u.MapFrom(s => isEn_Us ? s.QuestionEnName : s.QuestionName))
                .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>()
                 .ForMember(d => d.QuestionName, u => u.MapFrom(s => isEn_Us ? s.QuestionEnName : s.QuestionName));

            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()))
             .ForMember(d => d.LesionType, u => u.MapFrom(s => s.ReadingQuestionTrial == null ?null : s.ReadingQuestionTrial.LesionType));


            CreateMap<ReadingTableQuestionSystem, ReadingTrialTableQuestionData>()
             .ForMember(dest => dest.DependParentQuestion, opt => opt.Ignore())
         .ForMember(d => d.OriginalId, u => u.MapFrom(s => s.Id))
          .ForMember(d => d.SystemTableQuestionId, 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<ReadingQuestionSystem, AddOrUpdateReadingQuestionSystemInDto>();
            CreateMap<AddOrUpdateReadingQuestionSystemInDto, ReadingQuestionSystem>();
            CreateMap<ReadingQuestionSystem, ReadingQuestionSystemView>()
               .ForMember(d => d.GroupName, u => u.MapFrom(s => s.GroupInfo==null?s.GroupName:s.GroupInfo.GroupName))
              .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.ParentDictionaryCode, u => u.MapFrom(s => s.ParentReadingQuestionSystem.DictionaryCode))
              .ForMember(d => d.ParentQuestionGenre, u => u.MapFrom(s => s.ParentReadingQuestionSystem.QuestionGenre))
              .ForMember(d => d.RelevanceShowOrder, u => u.MapFrom(s => s.RelevanceReadingQuestionSystem.ShowOrder))
              .ForMember(d => d.RelevanceDictionaryCode, u => u.MapFrom(s => s.RelevanceReadingQuestionSystem.DictionaryCode))
              .ForMember(d => d.RelevanceQuestionGenre, u => u.MapFrom(s => s.RelevanceReadingQuestionSystem.QuestionGenre));
            

            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.GroupName, u => u.MapFrom(s => s.GroupInfo == null ? s.GroupName : s.GroupInfo.GroupName))
                    .ForMember(d => d.GroupEnName, u => u.MapFrom(s => s.GroupInfo == null ? s.GroupEnName : s.GroupInfo.GroupEnName))
                .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.ParentQuestionGenre, u => u.MapFrom(s => s.ParentReadingQuestionTrial.QuestionGenre))
             .ForMember(d => d.ParentDictionaryCode, u => u.MapFrom(s => s.ParentReadingQuestionTrial.DictionaryCode))
              .ForMember(d => d.RelevanceShowOrder, u => u.MapFrom(s => s.RelevanceReadingQuestionTrial.ShowOrder))
              .ForMember(d => d.RelevanceDictionaryCode, u => u.MapFrom(s => s.RelevanceReadingQuestionTrial.DictionaryCode))
              .ForMember(d => d.RelevanceQuestionGenre, u => u.MapFrom(s => s.RelevanceReadingQuestionTrial.QuestionGenre)); ;
            #endregion

            #region IR阅片
            CreateMap<ReadingQuestionTrial, GetTrialReadingQuestionOutDto>()
                  .ForMember(d => d.QuestionName, u => u.MapFrom(s => isEn_Us ? s.QuestionEnName : s.QuestionName))
                .ForMember(x=>x.Id, y=>y.MapFrom(z=>z.Id));

            //CreateMap<VisitTask, GetReadingPastResultListOutDto>()
            //      .ForMember(x => x.VisitTaskId, y => y.MapFrom(z => z.Id))
            //     .ForMember(x => x.JudgeResultArm, y => y.MapFrom(z => (z.JudgeResultTask==null?null:z.JudgeResultTask.ArmEnum)));
            #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.FileList, y => y.MapFrom(n => n.FileList))
                 .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
        }
    }
   
}
