using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Application.Service.Reading.Dto;
using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Hosting;
using MiniExcelLibs.Attributes;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace IRaCIS.Application.Contracts
{


    public class TrialSiteCRCListDTO
    {
        public Guid Id { get; set; }

        public Guid TrialId { get; set; }

        public Guid UserId { get; set; }

        public Guid IdentityUserId { get; set; }

        public string UserType { get; set; }

        public string Phone { get; set; } = String.Empty;

        public string EMail { get; set; } = string.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;

        public string FullName { get; set; } = String.Empty;

        public bool IsDeleted { get; set; }

        public DateTime? DeletedTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public DateTime CreateTime { get; set; }

        public Guid TrialSiteId { get; set; }
    }

    public class UserTrialDTO : UserTrialCommand
    {
        [JsonIgnore]
        public string State => IsDeleted ? "退出" : "加入";


        [DictionaryTranslateAttribute("IsUserExitTrial")]
        public bool IsDeleted { get; set; }

        [ExcelFormat("yyyy-MM-dd HH:mm:ss")]
        public DateTime? DeletedTime { get; set; }

        public Guid? TrialSiteId { get; set; }
        public string Phone { get; set; } = String.Empty;
        public DateTime UpdateTime { get; set; }

        [ExcelFormat("yyyy-MM-dd HH:mm:ss")]
        public DateTime CreateTime { get; set; }


        public string UserType { get; set; } = String.Empty;
        public string UserRealName { get; set; } = String.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;

        public string EMail { get; set; } = string.Empty;

    }

    public class TrialUserSelct : UserTrialCommand
    {
        public string Phone { get; set; } = String.Empty;

        public string Email { get; set; } = string.Empty;

        public string RealName { get; set; } = String.Empty;
        public string UserName { get; set; } = String.Empty;
    }

    public class TrialUserBasicInfo
    {
        public Guid UserTypeId { get; set; }

        public string UserName { get; set; }

        public string FullName { get; set; }

        public Guid UserId { get; set; }
    }

    public class TrialIdentityUserBasicInfo
    {
        public Guid IdentityUserId { get; set; }

        public string UserName { get; set; }

        public string FullName { get; set; }
    }

    public class IdentityUserJoinedTrialQuery : PageInput
    {
        [NotDefault]
        public Guid IdentityUserId { get; set; }
        public string? TrialCode { get; set; }

        public string? ResearchProgramNo { get; set; }

        public string? ExperimentName { get; set; }

        public Guid? UserTypeId { get; set; }

        public bool? IsDeleted { get; set; }
    }

    public class IdentityUserJoinedTrialView
    {
        public Guid TrialId { get; set; }
        public string TrialCode { get; set; }

        public string ResearchProgramNo { get; set; }

        public string ExperimentName { get; set; }

        public string TrialStatusStr { get; set; }

        public DateTime TrialCreateTime { get; set; }

        public List<UserTypeSelectDto> TrialUserRoleList { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime? RemoveTime { get; set; }

        public DateTime? JoinTime { get; set; }


        public string Roles => string.Join(',', TrialUserRoleList.Select(t => t.UserTypeShortName));
        public string JoinTimeStr => JoinTime?.ToString("yyyy-MM-dd") ?? string.Empty;
        public string RemoveTimeStr => RemoveTime?.ToString("yyyy-MM-dd") ?? string.Empty;

    }

    public class TrialMaintenanceDTO : UserTrialCommand
    {

        [DictionaryTranslateAttribute("IsUserExitTrial")]
        public bool IsDeleted { get; set; }

        [DictionaryTranslateAttribute("IsSiteDisable")]

        public bool RoleIsDeleted { get; set; }

        public DateTime? DeletedTime { get; set; }


        public DateTime? RemoveTime { get; set; }

        public string RemoveTimeStr => RemoveTime?.ToString("yyyy-MM-dd") ?? string.Empty;


        public DateTime? JoinTime { get; set; }

        public string JoinTimeStr => JoinTime?.ToString("yyyy-MM-dd") ?? string.Empty;

        public DateTime CreateTime { get; set; }


        public string Phone { get; set; } = String.Empty;

        public string EMail { get; set; } = string.Empty;


        public DateTime UpdateTime { get; set; } = DateTime.Now;

        public Guid UserTypeId { get; set; }
        public string UserType { get; set; } = String.Empty;

        public UserTypeEnum UserTypeEnum { get; set; }

        public string OrganizationName { get; set; } = String.Empty;

        public string UserRealName { get; set; } = String.Empty;
        public string UserName { get; set; } = String.Empty;


    }

    public class TrialIdentityUserDto
    {
        public Guid Id { get; set; }

        public Guid TrialId { get; set; }

        public Guid IdentityUserId { get; set; }

        public string Phone { get; set; } = String.Empty;

        public string EMail { get; set; } = string.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;

        public string FullName { get; set; } = String.Empty;

        public bool IsDeleted { get; set; }

        public UserStateEnum Status { get; set; }

        public DateTime CreateTime { get; set; }
        public DateTime UpdateTime { get; set; }

        [ExcelFormat("yyyy-MM-dd")]
        public DateTime? RemoveTime { get; set; }

        [ExcelFormat("yyyy-MM-dd")]
        public DateTime? JoinTime { get; set; }

        //public string JoinTimeStr => JoinTime?.ToString("yyyy-MM-dd") ?? string.Empty;
        //public string RemoveTimeStr => RemoveTime?.ToString("yyyy-MM-dd") ?? string.Empty;


        public List<TrialIdentityUserRoleDto> TrialUserRoleList { get; set; }

    }


    public class TrialUserRoleScreeningDto
    {
        public Guid TrialId { get; set; }

        public Guid UserId => Id;
        public Guid Id { get; set; }

        public Guid IdentityUserId { get; set; }

        public string Phone { get; set; } = String.Empty;

        public string EMail { get; set; } = string.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;

        public string FullName { get; set; } = String.Empty;

        public string UserTypeShortName { get; set; }

        public bool IsSelect { get; set; }
    }

    public class TrialIdentityUserRoleDto
    {
        public Guid Id { get; set; }

        public string UserTypeShortName { get; set; }

        public UserTypeEnum UserTypeEnum { get; set; }

        public bool IsUserRoleDisabled { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreateTime { get; set; }
        public DateTime UpdateTime { get; set; }
    }


    public class TrialCRCUploadImageList_Export : ExcelExportInfo
    {



    }



    /// <summary>
    /// 导出文件
    /// </summary>
    public class ExportTemplateAsyncDto
    {
        /// <summary>
        /// 模板Code
        /// </summary>
        public string TemplateCode { get; set; }

        /// <summary>
        /// 导出的文件名
        /// </summary>
        public string ExportFileName { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public object Data { get; set; }
    }

    public class ExportTemplateServiceDto : ExportTemplateAsyncDto
    {
        public IRepository<CommonDocument> commonDocumentRepository { get; set; }

        public IWebHostEnvironment hostEnvironment { get; set; }

        public bool IsEnglish { get; set; }
    }

    public class VisitJudgeExportInfo : ExcelExportInfo
    {
        public int VisitCount { get; set; }

        public int JudgeVisitCount { get; set; }

        public string JudgeRatioStr => VisitCount == 0 ? $"NA" : $"{Math.Round((decimal)JudgeVisitCount * 100 / VisitCount, 2)}%";
    }

    public class ReadingPeriodJudgeExportInfo : ExcelExportInfo
    {
        public int SubjectCount { get; set; }

        public int judgeSubjectCount { get; set; }

        public string SubjectJudgeRatioStr => SubjectCount == 0 ? $"NA" : $"{Math.Round((decimal)judgeSubjectCount * 100 / SubjectCount, 2)}%";


        public int ReadingPeriodCount { get; set; }

        public int judgeReadingPeriodCount { get; set; }

        public string ReadingPeriodJudgeRatioStr => ReadingPeriodCount == 0 ? $"NA" : $"{Math.Round((decimal)judgeReadingPeriodCount * 100 / ReadingPeriodCount, 2)}%";

        public List<DoctorJudgeRatio> DoctorPeriodStatList { get; set; }

        public List<DoctorJudgeRatio> DoctorSubjectStatList { get; set; }
    }


    public class ExcelExportInfo : TrialSelectDTO
    {
        public string CurrentTime { get; set; }

        public object List { get; set; }

    }

    public class TrialUserExportDTO : ExcelExportInfo
    {

        public List<TrialMaintenanceDTO> TrialUserList { get; set; } = new List<TrialMaintenanceDTO>();
    }


    public class TrialSiteUserExportDto : ExcelExportInfo
    {

        public List<SiteUserExportDTO> TrialSiteUserList { get; set; } = new List<SiteUserExportDTO>();
    }
    public class SiteUserExportDTO : UserTrialDTO
    {

        public string TrialSiteCode { get; set; } = String.Empty;
        public string TrialSiteAliasName { get; set; } = String.Empty;
    }

    public class TrialSiteUserSummaryExportDto : ExcelExportInfo
    {

        public List<TrialSiteUserSummaryDto> TrialSiteUserList { get; set; } = new List<TrialSiteUserSummaryDto>();

    }

    public class TrialSiteUserSummaryDto : TrialSiteUserSurveyView
    {


        public string TrialSiteCode { get; set; } = String.Empty;
        public string TrialSiteAliasName { get; set; } = String.Empty;

        public string UserRealName => LastName + " / " + FirstName;


        //public string IsGenerateAccountStr => IsGenerateAccount ? "是" : "否";


        //public string StateStr => State.GetDescription();

    }



    public class SiteCRCCommand : UserTrialCommand
    {
        public Guid TrialSiteId { get; set; }

        public Guid UserTypeId { get; set; }
        public string UserType { get; set; } = String.Empty;
        public string UserRealName { get; set; } = String.Empty;


        public UserTypeEnum UserTypeEnum { get; set; }


        public string OrganizationName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;
    }



    public class UserTrialCommand
    {
        public Guid? Id { get; set; }
        public Guid UserId { get; set; }
        public Guid TrialId { get; set; }
    }





    public class SiteStatDTO : SiteStatSimpleDTO
    {

        public bool IsHaveSiteSurveyRecord { get; set; }

        //public int? VisitCount { get; set; }
        //public int? SubjectCount { get; set; }


    }

    public class SiteStatSimpleDTO
    {
        public Guid Id { get; set; }

        public Guid TrialSiteId { get; set; }

        public string Country { get; set; }

        public string TrialSiteCode { get; set; } = String.Empty;

        public string TrialSiteName { get; set; } = String.Empty;
        public string TrialSiteAliasName { get; set; } = String.Empty;

        public bool IsDeleted { get; set; }

        public DateTime? DeletedTime { get; set; }

        public DateTime? EnabledTime { get; set; }

        public DateTime UpdateTime { get; set; }

        public DateTime CreateTime { get; set; }

        //public string Site { get; set; } = String.Empty;
        //public string SiteCode { get; set; } = String.Empty;
        //public string Hospital { get; set; } = String.Empty;
        //public string City { get; set; } = String.Empty;
        //public string Country { get; set; } = String.Empty;
        //public string DirectorName { get; set; } = String.Empty;
        //public string DirectorPhone { get; set; } = String.Empty;
        //public string ContactName { get; set; } = String.Empty;
        //public string ContactPhone { get; set; } = String.Empty;
        //public string Address { get; set; } = String.Empty;

        public List<string> CallingAEList { get; set; }
        public List<string> UserNameList { get; set; } = new List<string>();

        public int? VisitCount { get; set; }
        public int? SubjectCount { get; set; }
    }




    public class TrialMaintenanceExportQuery
    {
        public Guid TrialId { get; set; } = Guid.Empty;
        public string UserRealName { get; set; } = string.Empty;

        public Guid? UserTypeId { get; set; }

        public string UserName { get; set; } = string.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public bool? IsDeleted { get; set; }
    }

    public class TrialMaintenanceQuery : PageInput
    {
        public Guid TrialId { get; set; } = Guid.Empty;
        public string UserRealName { get; set; } = string.Empty;

        public Guid? UserTypeId { get; set; }

        public List<UserTypeEnum> UserTypeList { get; set; } = new List<UserTypeEnum>();

        public string UserName { get; set; } = string.Empty;

        public string OrganizationName { get; set; } = String.Empty;

        public bool? IsDeleted { get; set; }

    }

    public class SiteCRCQuery : TrialMaintenanceQuery
    {
        [NotDefault]
        public Guid TrialSiteId { get; set; }

    }



    public class TrialUserQuery : PageInput
    {
        public Guid TrialId { get; set; }
        public string UserName { get; set; } = string.Empty;

        public string UserRealName { get; set; } = string.Empty;
        public string OrganizationName { get; set; } = String.Empty;

        public UserTypeEnum? UserTypeEnum { get; set; }

        public bool? IsUserRoleDisabled { get; set; }

        public string? EMail { get; set; }
    }

    public class TrialUserScreeningDTO : TrialUserAddCommand
    {

        public int? Sex { get; set; } // 1-男  2-女

        public string Phone { get; set; } = string.Empty;
        public string EMail { get; set; } = string.Empty;

        public string DepartmentName { get; set; } = String.Empty;
        public string PositionName { get; set; } = String.Empty;

        public string UserName { get; set; } = String.Empty;

        public bool IsSelect { get; set; }



        public Guid UserTypeId { get; set; }
        public string UserType { get; set; } = string.Empty;
        public UserTypeEnum UserTypeEnum { get; set; }

        public string OrganizationName { get; set; } = string.Empty;
        public string UserRealName { get; set; } = string.Empty;


    }


    public class TrialUserAddCommand
    {
        [NotDefault]
        public Guid IdentityUserId { get; set; }

        [NotDefault]
        public Guid UserId { get; set; }

        [NotDefault]
        public Guid TrialId { get; set; }



    }

    public class TrialUserSendJoinEmailCommand
    {
        [NotDefault]
        public Guid TrialId { get; set; }

        [NotDefault]
        public Guid IdentityUserId { get; set; }

        public string BaseUrl { get; set; } = string.Empty;
        public string RouteUrl { get; set; } = string.Empty;

    }
    public class UpdateTrialUserRoleCommand
    {
        public List<Guid> IdList { get; set; }

        public bool IsDeleted { get; set; }
    }

    public class UpdateTrialUserCommand
    {
        [NotDefault]
        public Guid Id { get; set; }

        public Guid TrialId { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime? RemoveTime { get; set; }

        public DateTime? JoinTime { get; set; }
    }

}
