using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Castle.Core.Internal;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Application.Image.QA;
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;
using Microsoft.EntityFrameworkCore;

namespace IRaCIS.Core.API.Controllers
{

    [ApiController, ApiExplorerSettings(GroupName = "Reviewer")]
    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 ITrialConfigService _trialConfigService;
        private readonly ISubjectService _subjectService;
        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,
            IInspectionService sinspectionService,
        ITrialConfigService _trialConfigService,
            ISubjectService _subjectService)
        {
            _repository = repository;
            _mapper = mapper;
            _userInfo = userInfo;
            this._inspectionService = sinspectionService;
            this._trialDocumentService = trialDocumentService;
            this._qCListService = _qCListService;
            this._trialConfigService = _trialConfigService;
            this._subjectService = _subjectService;
            this._dataInspectionRepository = dataInspectionRepository;
        }

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

        ///// <summary>
        ///// 用户 签名某个文档   签署文件
        ///// </summary>
        ///// <param name="opt"></param>
        ///// <returns></returns>
        //[HttpPost, Route("Inspection/trialDocument/userConfirm")]
        //[UnitOfWork]
        //public async Task<IResponseOutput> UserConfirm(TrialDocumentConfirmDTO opt)
        //{
        //    var fun = _trialDocumentService.UserConfirm;
        //    return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        //}


        #region setting
        ///// <summary>
        ///// 配置 基础逻辑信息
        ///// </summary>
        ///// <param name="opt"></param>
        ///// <returns></returns> 
        //[HttpPut, Route("configTrialBasicInfo/configTrialBasicInfo")]
        //[UnitOfWork]
        //public async Task<IResponseOutput> ConfigTrialBasicInfo(InsBasicTrialConfig opt)
        //{
        //    var fun = _trialConfigService.ConfigTrialBasicInfo;
        //    return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        //}

        ///// <summary>
        ///// 配置流程
        ///// </summary>
        ///// <param name="opt"></param>
        ///// <returns></returns> 
        //[HttpPut, Route("configTrialBasicInfo/configTrialProcessInfo")]
        //[UnitOfWork]
        //public async Task<IResponseOutput> ConfigTrialProcessInfo(InsTrialProcessConfig opt)
        //{
        //    var fun = _trialConfigService.ConfigTrialProcessInfo;
        //    return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        //}

        ///// <summary>
        ///// 配置加急信息
        ///// </summary>
        ///// <param name="opt"></param>
        ///// <returns></returns> 
        //[HttpPut, Route("configTrialBasicInfo/ConfigTrialUrgentInfo")]
        //[UnitOfWork]
        //public async Task<IResponseOutput> ConfigTrialUrgentInfo(InsTrialUrgentConfig opt)
        //{
        //    var fun = _trialConfigService.ConfigTrialUrgentInfo;
        //    return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        //}


        #endregion



        #region 受试者
        /// <summary>
        /// 添加或更新受试者信息[New]
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns>
        //[TypeFilter(typeof(TrialResourceFilter))]
        [HttpPost, Route("Inspection/Subject/AddOrUpdateSubject")]
        [UnitOfWork]
        public async Task<IResponseOutput> AddOrUpdateSubject(InsSubjectCommand opt)
        {

            opt.AuditInfo.SubjectId = opt.OptCommand.Id;
            var fun = _subjectService.AddOrUpdateSubject;
            return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        }

        /// <summary>
        /// 修改受试者状态
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns>
        /// 
        [HttpPut, Route("Inspection/Subject/UpdateSubjectStatus")]
        [UnitOfWork]
        public async Task<IResponseOutput> UpdateSubjectStatus(InsUpdateSubjectStatus opt)
        {
            var fun = _subjectService.UpdateSubjectStatus;
            return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun);
        }


        /// <summary>
        /// 删除受试者
        /// </summary>
        /// <param name="opt"></param>
        /// <returns></returns>
        [HttpDelete, Route("Inspection/Subject/DeleteSubject")]
        [UnitOfWork]
        public async Task<IResponseOutput> DeleteSubject(InsDeleteSubjectDto opt)
        {
            var fun = _subjectService.DeleteSubject;
            var data = await _subjectService.DeleteSubject(opt.OptCommand.SubjectId);
            return await _inspectionService.Enforcement(opt.OptCommand, opt.AuditInfo, opt.SignInfo, fun, data);
        }
        #endregion



    }




}