using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using IRaCIS.Core.Application.Auth;

namespace IRaCIS.Application.Services
{
    [ApiExplorerSettings(GroupName = "Trial")]
    public class SubjectService : BaseService, ISubjectService
    {
        private readonly IRepository<Subject> _subjectRepository;
        private readonly IRepository<SubjectVisit> _subjectVisitRepository;

        public SubjectService(IRepository<Subject> subjectRepository, IRepository<SubjectVisit> subjectVisitRepository)
        {
            _subjectRepository = subjectRepository;
            _subjectVisitRepository = subjectVisitRepository;
        }

        /// <summary>
        ///  添加或更新受试者信息[New]
        /// </summary>
        /// <param name="subjectCommand">state:1-访视中，2-出组。0-全部</param>
        /// <returns></returns>

        [TrialAudit(AuditType.SubjectAudit, AuditOptType.AddOrUpdateSubject)]
        [TypeFilter(typeof(TrialResourceFilter), Arguments = new object[] { "AfterStopCannNotOpt" })]
        //[Authorize(Policy = IRaCISPolicy.PM_APM_CRC_QC)]
        public async Task<IResponseOutput<string>> AddOrUpdateSubject([FromBody] SubjectCommand subjectCommand)
        {
            var svlist = new List<SubjectVisit>();
            if (await _repository.AnyAsync<Trial>(t => t.Id == subjectCommand.TrialId && !t.VisitPlanConfirmed))
            {
                //---项目访视计划没有确认。请联系项目经理确认项目访视计划后，再添加受试者。
                return ResponseOutput.NotOk(_localizer["Subject_NoConfirmedPlan"]);
            }

            var verifyExp1 = new EntityVerifyExp<Subject>()
            {
                VerifyExp = u => u.Code == subjectCommand.Code && u.TrialId == subjectCommand.TrialId,
                //---已存在具有相关受试者编号的受试者。
                VerifyMsg = _localizer["Subject_DuplicateSubjectNum"]
            };


            Subject? mapedSubject = null;

            if (subjectCommand.Id == null) //insert
            {


                mapedSubject = await _subjectRepository.InsertFromDTOAsync(subjectCommand, false, verifyExp1);


            }
            else //update
            {

                mapedSubject = await _subjectRepository.UpdateFromDTOAsync(subjectCommand, false, false, verifyExp1/*, verifyExp2*/);

            }



            await _subjectRepository.SaveChangesAsync();

            return ResponseOutput.Ok(mapedSubject.Id.ToString());

        }

        [HttpPut]
        [UnitOfWork]
        //[Authorize(Policy = IRaCISPolicy.PM_APM_CRC_QC)]
        [TypeFilter(typeof(TrialResourceFilter), Arguments = new object[] { "AfterStopCannNotOpt" })]
        public async Task<IResponseOutput> UpdateSubjectStatus(SubjectStatusChangeCommand subjectStatusChangeCommand)
        {
            await _subjectRepository.UpdateFromDTOAsync(subjectStatusChangeCommand, true);
            return ResponseOutput.Ok();
        }


        [HttpDelete("{trialId:guid}/{id:guid}")]
        [TypeFilter(typeof(TrialResourceFilter), Arguments = new object[] { "AfterStopCannNotOpt" })]
        [UnitOfWork]
        //[Authorize(Policy = IRaCISPolicy.PM_APM_CRC)]
        public async Task<IResponseOutput> DeleteSubject(Guid id)
        {

            if (await _subjectVisitRepository.AnyAsync(u => u.SubjectId == id && u.VisitExecuted == VisitExecutedEnum.Executed))
            {
                //---该受试者已经有访视已经上传影像，不允许删除。
                return ResponseOutput.NotOk(_localizer["Subject_UploadedVisitNoDelete"]);
            }

            await _subjectRepository.UpdatePartialFromQueryAsync(id, x => new Subject
            {
                IsDeleted = true,
            });
            await _subjectVisitRepository.UpdatePartialFromQueryAsync(u => u.SubjectId == id, x => new SubjectVisit()
            {
                IsDeleted = true,
            });

            var isSuccess = await _subjectRepository.SaveChangesAsync();

            return ResponseOutput.Result(isSuccess);
        }

        /// <summary> 分页获取受试者列表[New] </summary>
        /// /// <param name="param">state:1-访视中，2-出组。0-全部</param>
        [HttpPost]
        public async Task<IResponseOutput<PageOutput<SubjectQueryView>>> GetSubjectList(SubjectQueryParam param)
        {

            var subjectQuery = _subjectRepository.Where(u => u.TrialId == param.TrialId)
                  .WhereIf(!string.IsNullOrWhiteSpace(param.Code), t => t.Code.Contains(param.Code))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.Name), t => t.ShortName.Contains(param.Name))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.Sex), t => t.Sex.Contains(param.Sex))
                  .WhereIf(param.Status != null, t => t.Status == param.Status)
                  .WhereIf(param.SiteId != null, t => t.SiteId == param.SiteId)
                  // CRC  只负责他管理site的受试者
                  .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CRA, t => t.TrialSite.CRCUserList.Any(t => t.UserId == _userInfo.Id))
                  .ProjectTo<SubjectQueryView>(_mapper.ConfigurationProvider);


            var pageList = await subjectQuery.ToPagedListAsync(param.PageIndex, param.PageSize, param.SortField == string.Empty ? "Code" : param.SortField, param.Asc);

            var trialConfig = await _repository.Where<Trial>(t => t.Id == param.TrialId).ProjectTo<TrialSubjectConfig>(_mapper.ConfigurationProvider).FirstOrDefaultAsync().IfNullThrowException();

            return ResponseOutput.Ok(pageList, trialConfig);

        }

        /// <summary>
        /// 计划外访视  获取受试者选择下拉框列表
        /// </summary>
        [HttpGet("{siteId:guid}/{trialId:guid}")]
        public List<SubjectSelect> GetSubjectListBySiteId(Guid siteId, Guid trialId)
        {
            var query = _subjectRepository.Where(t => t.SiteId == siteId && t.TrialId == trialId && t.Status == SubjectStatus.OnVisit).Select(u => new SubjectSelect()
            {
                Code = u.Code,
                FirstName = u.FirstName,
                LastName = u.LastName,
                Sex = u.Sex,
                SubjectId = u.Id,
                Age = u.Age,
                MRN = u.MedicalNo,
                Status = u.Status,

                FirstGiveMedicineTime = u.FirstGiveMedicineTime
            });
            return query.ToList();
        }



     
    }
}
