using System;
using System.Net.Http;
using EasyCaching.Core;
using gRPC.ZHiZHUN.AuthServer.protos;
using Grpc.Net.Client;
using Grpc.Net.Client.Configuration;
using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Auth;
using IRaCIS.Core.Application.Filter;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;
using IRaCIS.Core.Application.Interfaces;
using System.Threading.Tasks;
using IRaCIS.Application.Services;

namespace IRaCIS.Api.Controllers
{
    /// <summary>
    /// 医生基本信息 、工作信息  专业信息、审核状态
    /// </summary>
    [ApiController, ApiExplorerSettings(GroupName = "Reviewer")]
    public class ExtraController : ControllerBase
    {


        /// <summary>
        ///  获取医生详情 
        /// </summary>
        /// <param name="attachmentService"></param>
        /// <param name="_doctorService"></param>
        /// <param name="_educationService"></param>
        /// <param name="_trialExperienceService"></param>
        /// <param name="_researchPublicationService"></param>
        /// <param name="_vacationService"></param>
        /// <param name="doctorId"></param>
        /// <returns></returns>
        [HttpGet, Route("doctor/getDetail/{doctorId:guid}")]
       
        public  async Task<IResponseOutput<DoctorDetailDTO>> GetDoctorDetail([FromServices] IAttachmentService attachmentService, [FromServices] IDoctorService _doctorService,
            [FromServices] IEducationService _educationService, [FromServices] ITrialExperienceService _trialExperienceService,
            [FromServices] IResearchPublicationService _researchPublicationService, [FromServices] IVacationService _vacationService, Guid doctorId)
        {
            var education = await _educationService.GetEducation(doctorId);

            var sowList = _doctorService.GetDoctorSowList(doctorId);
            var ackSowList = _doctorService.GetDoctorAckSowList(doctorId);

            var doctorDetail = new DoctorDetailDTO
            {
                AuditView =await _doctorService.GetAuditState(doctorId),
                BasicInfoView = await _doctorService.GetBasicInfo(doctorId),
                EmploymentView = await _doctorService.GetEmploymentInfo(doctorId),
                //AttachmentList = attachmentService.GetAttachments(doctorId),

                EducationList = education.EducationList,
                PostgraduateList = education.PostgraduateList,

                TrialExperienceView = await _trialExperienceService.GetTrialExperience(doctorId),
                ResearchPublicationView = await _researchPublicationService.GetResearchPublication(doctorId),

                SpecialtyView =await _doctorService.GetSpecialtyInfo(doctorId),
                InHoliday = (await _vacationService.OnVacation(doctorId)).IsSuccess,
                IntoGroupInfo = _doctorService.GetDoctorIntoGroupInfo(doctorId),
                SowList = sowList,
                AckSowList = ackSowList
            };

            return ResponseOutput.Ok(doctorDetail);
        }





        [HttpPost, Route("enroll/downloadResume/{trialId:guid}/{language}")]
        [TypeFilter(typeof(TrialResourceFilter))]
        [AllowAnonymous]
        public async Task<IResponseOutput<string>> DownloadResume([FromServices] IFileService _fileService, int language, Guid trialId, Guid[] doctorIdArray)
        {
            var userId = Guid.Parse(User.FindFirst("id").Value);
            var zipPath = await _fileService.CreateOfficialResumeZip(language, doctorIdArray);

            return ResponseOutput.Ok(zipPath);
        }




        /// <summary>  系统用户登录接口[New] </summary>
        [HttpPost, Route("user/login")]
        [AllowAnonymous]
        public  async Task<IResponseOutput<LoginReturnDTO>>  Login(UserLoginDTO loginUser, [FromServices] IEasyCachingProvider provider, [FromServices] IUserService _userService,
            [FromServices] ITokenService _tokenService, [FromServices] IConfiguration configuration)
        {

            var returnModel = await _userService.Login(loginUser.UserName, loginUser.Password);

            if (returnModel.IsSuccess)
            {
                #region GRPC 调用鉴权中心，因为服务器IIS问题 http/2   故而没法使用

                ////重试策略
                //var defaultMethodConfig = new MethodConfig
                //{
                //    Names = { MethodName.Default },
                //    RetryPolicy = new RetryPolicy
                //    {
                //        MaxAttempts = 3,
                //        InitialBackoff = TimeSpan.FromSeconds(1),
                //        MaxBackoff = TimeSpan.FromSeconds(5),
                //        BackoffMultiplier = 1.5,
                //        RetryableStatusCodes = { Grpc.Core.StatusCode.Unavailable }
                //    }
                //};

                //#region  unable to trust the certificate then the gRPC client can be configured to ignore the invalid certificate

                //var httpHandler = new HttpClientHandler();
                //// Return `true` to allow certificates that are untrusted/invalid
                //httpHandler.ServerCertificateCustomValidationCallback =
                //    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;


                //////这一句是让grpc支持本地 http  如果本地访问部署在服务器上，那么是访问不成功的 
                //AppContext.SetSwitch(
                //    "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

                //#endregion



                //var grpcAdress = configuration.GetValue<string>("GrpcAddress");
                ////var grpcAdress = "http://localhost:7200";

                //var channel = GrpcChannel.ForAddress(grpcAdress, new GrpcChannelOptions
                //{
                //    HttpHandler = httpHandler,
                //    ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } }

                //});
                ////var channel = GrpcChannel.ForAddress(grpcAdress);
                //var grpcClient = new TokenGrpcService.TokenGrpcServiceClient(channel);

                //var userInfo = returnModel.Data.BasicInfo;

                //var tokenResponse = grpcClient.GetUserToken(new GetTokenReuqest()
                //{
                //    Id = userInfo.Id.ToString(),
                //    ReviewerCode = userInfo.ReviewerCode,
                //    IsAdmin = userInfo.IsAdmin,
                //    RealName = userInfo.RealName,
                //    UserTypeEnumInt = (int)userInfo.UserTypeEnum,
                //    UserTypeShortName = userInfo.UserTypeShortName,
                //    UserName = userInfo.UserName
                //});

                //returnModel.Data.JWTStr = tokenResponse.Token;

                #endregion

                returnModel.Data.JWTStr = _tokenService.GetToken(IRaCISClaims.Create(returnModel.Data.BasicInfo));

            }

            var userId = returnModel.Data.BasicInfo.Id.ToString();
            provider.Set(userId, userId, TimeSpan.FromMinutes(AppSettings.LoginExpiredTimeSpan));
            return returnModel;
        }



        [HttpGet, Route("imageShare/ShareImage")]
        public IResponseOutput ShareImage([FromServices] ITokenService _tokenService)
        {
            var token = _tokenService.GetToken(IRaCISClaims.Create(new UserBasicInfo()
            {
                Id = Guid.Empty,
                IsReviewer = false,
                IsAdmin = false,
                RealName = "Share001",
                UserName = "Share001",
                Sex = 0,
                //UserType = "ShareType",
                UserTypeEnum = UserTypeEnum.ShareImage,
                Code = "ShareCode001",
            }));
            return ResponseOutput.Ok("/#/preview?studyId=d8e7ed1c-4b59-a483-563d-4f05bd4f8921&token=" + token);
        }




        //外部用户  邮件链接调用 以及跳转逻辑

        [HttpGet("trialExternalUser/ExternalUserJoinTrial")]
        [AllowAnonymous]
        public async Task<IActionResult> ExternalUserJoinTrial([FromServices] ITrialExternalUserService _trialExternalUserService, Guid trialId, Guid trialExternalUserId, string url)
        {
            await _trialExternalUserService.UserConfirmJoinTrial(trialId, trialExternalUserId);


            var decodeUrl = System.Web.HttpUtility.UrlDecode(url);


            return Redirect(decodeUrl);
        }





        [HttpGet, Route("ip")]
        [AllowAnonymous]
        public IResponseOutput Get([FromServices] IHttpContextAccessor _context, [FromServices] IUserService _userService)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"RemoteIpAddress：{_context.HttpContext.Connection.RemoteIpAddress}");

            if (Request.Headers.ContainsKey("X-Real-IP"))
            {
                sb.AppendLine($"X-Real-IP：{Request.Headers["X-Real-IP"].ToString()}");
            }

            if (Request.Headers.ContainsKey("X-Forwarded-For"))
            {
                sb.AppendLine($"X-Forwarded-For：{Request.Headers["X-Forwarded-For"].ToString()}");
            }
            return ResponseOutput.Ok(sb.ToString());
        }


    }
}
