using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Castle.Core.Internal;
using IRaCIS.Application.Contracts;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Contracts.DTO;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Application.Image.QA;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.Service.Inspection.DTO;
using IRaCIS.Core.Application.Service.Inspection.Interface;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;


namespace IRaCIS.Core.API.Controllers
{

    [ApiController, ApiExplorerSettings(GroupName = "Reviewer")]
    [UnitOfWork]
    public class InspectionController : ControllerBase
    {
        private readonly IRepository _repository;
        private readonly IMapper _mapper;
        private readonly IUserInfo _userInfo;
        private readonly ITrialDocumentService _trialDocumentService;
        private readonly IQCListService _qCListService;
        private readonly IHttpContextAccessor _httpContext;
        private readonly ITrialConfigService _trialConfigService;
        private readonly INoneDicomStudyService _noneDicomStudyService;
        private readonly ISubjectService _subjectService;
        private readonly ISubjectVisitService _subjectVisitService;
        private readonly IQCOperationService _qCOperationService;
        private readonly IClinicalDataService _clinicalDataService;
        private readonly IVisitPlanService _visitPlanService;
        private readonly IInspectionService _inspectionService;

        private readonly IRepository<DataInspection> _dataInspectionRepository;
        private delegate Task<IResponseOutput> executionFun(dynamic data);

        public InspectionController(IRepository repository,
            IRepository<DataInspection> _repositoryDataInspection,
            IMapper mapper, IUserInfo userInfo,
            ITrialDocumentService trialDocumentService,
            IRepository<DataInspection> dataInspectionRepository,
            IQCListService _qCListService,
            IHttpContextAccessor httpContext,
            IInspectionService sinspectionService,
            ITrialConfigService _trialConfigService,
            INoneDicomStudyService noneDicomStudyService,
            ISubjectService _subjectService,

            ISubjectVisitService subjectVisitService,
            IQCOperationService qCOperationService,
            IClinicalDataService clinicalDataService,
            IVisitPlanService visitPlanService
            )
        {
            this._repository = repository;
            this._mapper = mapper;
            this._userInfo = userInfo;
            this._inspectionService = sinspectionService;
            this._trialDocumentService = trialDocumentService;
            this._qCListService = _qCListService;
            this._httpContext = httpContext;
            this._trialConfigService = _trialConfigService;
            this._noneDicomStudyService = noneDicomStudyService;
            this._subjectService = _subjectService;
            this._subjectVisitService = subjectVisitService;
            this._qCOperationService = qCOperationService;
            this._clinicalDataService = clinicalDataService;
            this._visitPlanService = visitPlanService;
            this._dataInspectionRepository = dataInspectionRepository;
        }




        #region 获取稽查数据
        /// <summary>
        /// 获取稽查数据
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("Inspection/GetInspectionData")]
        public async Task<PageOutput<GetDataInspectionOutDto>> GetInspectionData(GetDataInspectionDto dto)
        {
            return await _inspectionService.GetInspectionData(dto);
        }
        #endregion


   
      

        /// <summary>
        /// 配置 基础逻辑信息并确认
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns> 
        [HttpPost, Route("Inspection/configTrialBasicInfo/ConfigTrialBasicInfoConfirm")]
        [UnitOfWork]
        public async Task<IResponseOutput> ConfigTrialBasicInfoConfirm(DataInspectionDto<BasicTrialConfig> opt)
        {

            opt.Data.IsTrialBasicLogicConfirmed = true;
            var result = await _trialConfigService.ConfigTrialBasicInfo(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;



        }

        /// <summary>
        /// 签名确认
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("Inspection/configTrialBasicInfo/TrialConfigSignatureConfirm")]
        [UnitOfWork]
        public async Task<IResponseOutput> TrialConfigSignatureConfirm(DataInspectionDto<SignConfirmDTO> opt)
        {
            var result = await _trialConfigService.TrialConfigSignatureConfirm(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }


     


        /// <summary>
        /// 配置流程并确认
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns> 
        [HttpPost, Route("Inspection/configTrialBasicInfo/ConfigTrialProcessInfoConfirm")]
        [UnitOfWork]
        public async Task<IResponseOutput> ConfigTrialProcessInfoConfirm(DataInspectionDto<TrialProcessConfig> opt)
        {
            opt.Data.IsTrialProcessConfirmed = true;
            var result = await _trialConfigService.ConfigTrialProcessInfo(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo,result);
            return result;
        }


      


        /// <summary>
        /// 配置加急信息并确认
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns> 
        [HttpPost, Route("Inspection/configTrialBasicInfo/ConfigTrialUrgentInfoConfirm")]
        [UnitOfWork]
        public async Task<IResponseOutput> ConfigTrialUrgentInfoConfirm(DataInspectionDto<TrialUrgentConfig> opt)
        {
            opt.Data.IsTrialUrgentConfirmed = true;
            var  result=  await _trialConfigService.ConfigTrialUrgentInfo(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }


        /// <summary>
        /// CRC RequestToQC  批量提交
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns>
        [HttpPost, Route("Inspection/QCOperation/CRCRequestToQC")]
        [UnitOfWork]
        public async Task<IResponseOutput> CRCRequestToQC(DataInspectionDto<CRCRequestToQCCommand> opt)
        {
            var result = await _qCOperationService.CRCRequestToQC(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }


        /// <summary>
        /// 设置QC 通过或者不通过   7:QC failed  8：QC passed  
        /// </summary>
        [HttpPost, Route("Inspection/QCOperation/QCPassedOrFailed")]
        [UnitOfWork]
        public async Task<IResponseOutput> QCPassedOrFailed(DataInspectionDto<QCPassedOrFailedDto> opt)
        {
            var result= await _qCOperationService.QCPassedOrFailed(opt.Data.trialId, opt.Data.subjectVisitId, opt.Data.auditState);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }

        /// <summary>
        /// 一致性核查 回退  对话记录不清除  只允许PM回退
        /// </summary>
        [HttpPost, Route("Inspection/QCOperation/CheckBack")]
        [UnitOfWork]
        public async Task<IResponseOutput> CheckBack(DataInspectionDto<IDDto> opt)
        {
            var result =  await _qCOperationService.CheckBack(opt.Data.Id);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }

 



        /// <summary>
        /// CRC  设置已经重传完成
        /// </summary>
        [HttpPost, Route("Inspection/QCOperation/SetReuploadFinished")]
        [UnitOfWork]
        public async Task<IResponseOutput> SetReuploadFinished(DataInspectionDto<CRCReuploadFinishedCommand> opt)
        {
             var result = await _qCOperationService.SetReuploadFinished(opt.Data);
             await _inspectionService.RecordSing(opt.SignInfo, result);
             return result;
        }

        /// <summary>
        /// 更新项目状态
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns>
        [HttpPost, Route("Inspection/TrialConfig/updateTrialState")]
        [UnitOfWork]
        public async Task<IResponseOutput> UpdateTrialState(DataInspectionDto<UpdateTrialStateDto> opt)
        {
            var result = await _trialConfigService.UpdateTrialState(opt.Data.trialId, opt.Data.trialStatusStr, opt.Data.reason);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }


        /// <summary>
        /// 用户 签名某个文档  
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("Inspection/TrialDocument/userConfirm")]
        [UnitOfWork]
        public async Task<IResponseOutput> UserConfirm(DataInspectionDto<UserConfirmCommand> opt)
        {
            var result = await _trialDocumentService.UserConfirm(opt.Data);
            await _inspectionService.RecordSing(opt.SignInfo, result);
            return result;
        }
    }
}
