using AutoMapper;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Contracts.Dicom.DTO;
using IRaCIS.Core.Application.Contracts.DTO;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Models;

namespace IRaCIS.Core.Application.Service
{
    public class ImageAndDocConfig : Profile
    {
        public ImageAndDocConfig()
        {
            CreateMap<AddOrUpdateStudyDto, DicomStudy>()
                .ForMember(d => d.SeriesList, u => u.Ignore());
            CreateMap<AddOrUpdateSeriesDto, DicomSeries>()
                 .ForMember(d => d.DicomInstanceList, u => u.Ignore()); 
            CreateMap<AddInstanceDto, DicomInstance>();

            CreateMap<Report, ReportDTO>();
            CreateMap<ImageLabel, ImageLabelDTO>();
            CreateMap<DicomSeries, DicomSeriesWithLabelDTO>();

            //影像上传 检查
            CreateMap<DicomStudy, VerifyStudyDto>()
                .ForMember(d => d.SubjectId, u => u.MapFrom(s => s.Subject.Id))
                .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName))
                .ForMember(d => d.SubjectCode, u => u.MapFrom(s => s.Subject.Code))
                .ForMember(d => d.FirstName, u => u.MapFrom(s => s.Subject.FirstName))
                .ForMember(d => d.LastName, u => u.MapFrom(s => s.Subject.LastName));



            CreateMap<DicomStudy, StudyDTO>().IncludeMembers(t => t.Subject, u => u.SubjectVisit)
            .ForMember(d => d.UploaderFirstName, u => u.MapFrom(s => s.Site.SiteName))
            .ForMember(d => d.SiteName, u => u.MapFrom(s => s.Site.SiteName))
            .ForMember(d => d.UploaderFirstName, u => u.MapFrom(s => s.Uploader.FirstName))
            .ForMember(d => d.UploaderLastName, u => u.MapFrom(s => s.Uploader.LastName))
              .ForMember(d => d.UploadedTime, u => u.MapFrom(s => s.CreateTime));

            CreateMap<Subject, StudyDTO>();

            CreateMap<SubjectVisit, StudyDTO>();

            CreateMap<DicomStudy, RelationStudyDTO>()
                .ForMember(d => d.StudyId, u => u.MapFrom(s => s.Id))
                .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName));

            CreateMap<DicomStudy, DicomStudyBasicDTO>();


            CreateMap<DicomSeries, QASeriesInfoDto>()
                  .ForMember(o => o.StudyTime, t => t.MapFrom(u => u.DicomStudy.StudyTime))

                .ForMember(o => o.StudyCode, t => t.MapFrom(u => u.DicomStudy.StudyCode))
                 //.ForMember(o => o.InstanceList, t => t.MapFrom(u => u.DicomInstanceList.Select(t=>t.Id).ToArray()))
                 // .ForMember(o => o.InstancePathList, t => t.MapFrom(u => u.DicomInstanceList.OrderBy(t=>t.InstanceNumber).Select(t => t.Path)))
                ;

            CreateMap<DicomStudy, QAStudyInfoDTO>()
                .ForMember(o => o.UploadedTime, t => t.MapFrom(u => u.CreateTime))
                .ForMember(o => o.Uploader, t => t.MapFrom(u => u.Uploader.LastName + " / " + u.Uploader.FirstName))
                .ForMember(o => o.StudyId, t => t.MapFrom(u => u.Id))
                .ForMember(o => o.Modalities, t => t.MapFrom(u => string.Join('、', u.SeriesList.Select(t => t.Modality).Distinct()) ));



            CreateMap<StudyCommand, DicomStudy>();

            //CreateMap<ImageQACommand, ImageQA>();

            CreateMap<ImageShareCommand, ImageShare>();

            CreateMap<StudyDTFAddOrUpdateCommand, StudyDTF>();



            CreateMap<StudyStatusDetail, StudyStatusDetailDTO>();

            CreateMap<DicomInstance, DicomInstanceDTO>()
                 .ForMember(o => o.IsDeleted, t => t.MapFrom(u => u.DicomSerie.IsDeleted))
                  .ForMember(o => o.IsReading, t => t.MapFrom(u => u.DicomSerie.IsReading));
            CreateMap<DicomStudy, DicomStudyDTO>();
            CreateMap<DicomSeries, DicomSeriesDTO>();



            CreateMap<SystemAnonymization, SystemAnonymizationAddOrEdit>().ReverseMap();

            CreateMap<SystemAnonymization, SystemAnonymizationView>();



            CreateMap<DicomStudy, UnionStudyViewModel>()
                //.ForMember(d => d.DicomStudyCode, u => u.MapFrom(s => s.StudyCode))
                //.ForMember(d => d.Modality, u => u.MapFrom(s => s.Modalities))
                .ForMember(d => d.Bodypart, u => u.MapFrom(s => s.BodyPartExamined))
                .ForMember(d => d.StudyTime, u => u.MapFrom(s => s.StudyTime))
                .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
                .ForMember(d => d.IsDicom, u => u.MapFrom(s => true))
                 .ForMember(d => d.Count, u => u.MapFrom(s => s.SeriesCount))
                .ForMember(d => d.VisitNum, u => u.MapFrom(s => s.SubjectVisit.VisitNum))
                .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName));

            CreateMap<NoneDicomStudy, UnionStudyViewModel>()
                //.ForMember(d => d.NoneDicomCode, u => u.MapFrom(s => s.Code))
                //.ForMember(d => d.Modality, u => u.MapFrom(s => s.Modalities))
                .ForMember(d => d.Bodypart, u => u.MapFrom(s => s.BodyPart))
                .ForMember(d => d.StudyTime, u => u.MapFrom(s => s.ImageDate))
                .ForMember(d => d.TrialSiteCode, u => u.MapFrom(s => s.TrialSite.TrialSiteCode))
                .ForMember(d => d.TrialSiteAliasName, u => u.MapFrom(s => s.TrialSite.TrialSiteAliasName))
                .ForMember(d => d.IsDicom, u => u.MapFrom(s => false))
                 .ForMember(d => d.Count, u => u.MapFrom(s => s.NoneDicomFileList.Count()))
                .ForMember(d => d.VisitNum, u => u.MapFrom(s => s.SubjectVisit.VisitNum))
                .ForMember(d => d.VisitName, u => u.MapFrom(s => s.SubjectVisit.VisitName));




        }
    }
   
}
