using IRaCIS.Core.Domain.Share;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Filters;
using Magicodes.ExporterAndImporter.Core.Models;
using Magicodes.ExporterAndImporter.Excel;
using MediatR;
using MiniExcelLibs.Attributes;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace IRaCIS.Core.Application.MediatR.CommandAndQueries
{
    public class ConsistencyVerificationRequest : IRequest<string>
    {
        public List<CheckViewModel> ETCList { get; set; } = new List<CheckViewModel>();

        public Guid TrialId { get; set; }
    }

    public class CheckDBModel : CheckViewModel
    {

        public Guid SubjectVisitId { get; set; }

        public Guid StudyId { get; set; }
    }



    public class ImportResultFilteTest : IImportResultFilter
    {


        public ImportResult<T> Filter<T>(ImportResult<T> importResult) where T : class, new()
        {
            if (typeof(T).IsAssignableFrom(typeof(CheckViewModel)))
            {
                var data = (List<CheckViewModel>)importResult.Data;

                var dt = DateTime.Now ;

                foreach (var item  in data)
                {

                    var index= data.IndexOf(item);
                    if ( DateTime.TryParse(item.StudyDate, out dt) == false)
                    {
                       
                        importResult.RowErrors.Add(new DataRowErrorInfo() { RowIndex = index, FieldErrors = new Dictionary<string, string> { { StaticData.International("ConsistencyVerification_Tech") , StaticData.International("ConsistencyVerification_Time") } } });
                    }
                }               
            }
        
            return importResult;
        }
    }


    public class ParamInfoDto 
    {
        public string Modality { get; set; }

        public string StudyDate { get; set; }

        //public int  ErrorType { get; set; }



    }


    [ExcelImporter(/*ImportResultFilter = typeof(ImportResultFilteTest),*/ IsLabelingError = true)]

    public class CheckViewModel
    {
        //[Required(ErrorMessage = "中心编号不能为空")]
        [ImporterHeader(Name = "Site ID", AutoTrim = true)]
        [ExcelColumnIndex("Site ID")]
        public string SiteCode { get; set; } = string.Empty;


        //[Required(ErrorMessage = "受试者筛选号不能为空")]
        [ImporterHeader(Name = "Subject ID", AutoTrim = true)]
        [ExcelColumnName("Subject ID")]
        public string SubjectCode { get; set; } = string.Empty;

        //[Required(ErrorMessage = "访视名称不能为空")]
        [ImporterHeader(Name = "Visit Name", AutoTrim = true)]
        [ExcelColumnName("Visit Name")]
        public string VisitName { get; set; } = string.Empty;



        //[Required(ErrorMessage = "检查日期不能为空")]
        [CanConvertToTime(ErrorMessage = "Does not conform to Stusy Date format")]

        [ImporterHeader(Name = "Stusy Date", AutoTrim = true)]
        [ExcelColumnName("Stusy Date")]
        public string StudyDate { get; set; } = string.Empty;



        //[Required(ErrorMessage = "Modality不能为空")]
        [ImporterHeader(Name = "Modality", AutoTrim = true)]
        [ExcelColumnName("Modality")]
        public string Modality { get; set; } = string.Empty;




        public override bool Equals(object? obj)
        {
            if (obj == null) return false;

            var checkModel = obj as CheckViewModel;

            if (checkModel is not null)
            {
                return SiteCode == checkModel.SiteCode && SubjectCode == checkModel.SubjectCode && VisitName == checkModel.VisitName && StudyDate == checkModel.StudyDate && Modality == checkModel.Modality;
            }
            else
            {
                return false;
            }
        }
        public override int GetHashCode()
        {
            return (SiteCode + SubjectCode + VisitName + StudyDate + Modality).GetHashCode();
        }
    }

    public class VisitPlanInfluenceSubjectVisitDTO
    {
        [ExporterHeader(IsIgnore = true)]
        public Guid StudyId { get; set; }

        [ExporterHeader(IsIgnore = true)]
        public Guid TrialId { get; set; }

        [ExporterHeader(IsIgnore = true)]
        public Guid SubjectVisitId { get; set; }

        [ExporterHeader(DisplayName = "中心编号")]
        public string TrialSiteCode { get; set; } = string.Empty;

        [ExporterHeader(DisplayName = "受试者")]
        public string SubjectCode { get; set; } = string.Empty;

        [ExporterHeader(DisplayName = "访视名称")]
        public string VisitName { get; set; } = string.Empty;

        [ExporterHeader(DisplayName = "检查时间", Format = "yyyy-mm-dd hh:mm:ss")]
        public DateTime StudyTime { get; set; }

        [ExporterHeader(DisplayName = "检查技术")]
        public string Modality { get; set; } = string.Empty;

        [ExporterHeader(IsIgnore = true)]
        public bool IsDicomStudy { get; set; }

        [ExporterHeader(DisplayName = "影像类型")]
        public string ImageType => IsDicomStudy ? "Dicom" : "非Dicom";

        [ExporterHeader(DisplayName = "历史窗口")]
        public string HistoryWindow { get; set; } = string.Empty;

        [ExporterHeader(DisplayName = "之前超窗调整后没超窗")]
        [ValueMapping(text: "yes", true)]
        [ValueMapping(text: "no", false)]
        public bool IsOverWindowNowNotOverWindow { get; set; }

        [ExporterHeader(DisplayName = "目前窗口")]
        public string NowWindow { get; set; } = string.Empty;

        [ExporterHeader(IsIgnore = true)]
        public DateTime CreateTime { get; set; }
    }

}
