using System;
using System.Net.Http;
using EasyCaching.Core;
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;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Infrastructure;
using System.Linq;
using Microsoft.Extensions.Logging;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Sts.Model.V20150401;
using Microsoft.AspNetCore.Hosting;

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




        /// <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));

                // 创建一个 CookieOptions 对象，用于设置 Cookie 的属性
                var option = new CookieOptions
                {
                    Expires = DateTime.Now.AddMonths(1),
                    HttpOnly = true, // 确保 cookie 只能通过 HTTP 访问
                    SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None, // 设置 SameSite 属性
                    Secure = true // 确保 cookie 只能通过 HTTPS 访问
                };

                HttpContext.Response.Cookies.Append("access_token", returnModel.Data.JWTStr, option);

            }

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

            await provider.SetAsync(userId.ToString(), returnModel.Data.JWTStr, TimeSpan.FromDays(7));
            return returnModel;
        }


        [HttpGet("user/GenerateSTS")]
        public IResponseOutput GenerateSTS( [FromServices] IWebHostEnvironment webHostEnvironment)
        {
           

            var configuration = new ConfigurationBuilder()
              .AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json")
              .Build();



            IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", configuration["AliyunOSS:accessKeyId"], configuration["AliyunOSS:accessKeySecret"]);
            DefaultAcsClient client = new DefaultAcsClient(profile);

            // 创建一个STS请求
            AssumeRoleRequest request = new AssumeRoleRequest
            {
                RoleArn = "acs:ram::1899121822495495:role/oss-upload", // 角色ARN，需要替换为你的角色ARN
                RoleSessionName = "session-name", // 角色会话名称，可自定义
                DurationSeconds = 3600, // 令牌有效期（单位：秒），这里设置为1小时
            };


            AssumeRoleResponse response = client.GetAcsResponse(request);

            // 返回STS令牌信息给前端
            var stsToken = new
            {
                AccessKeyId = response.Credentials.AccessKeyId,
                AccessKeySecret = response.Credentials.AccessKeySecret,
                SecurityToken = response.Credentials.SecurityToken,
                Expiration = response.Credentials.Expiration,

                Region= configuration["AliyunOSS:region"],
                BucketName = configuration["AliyunOSS:bucketName"],
                ViewEndpoint = configuration["AliyunOSS:viewEndpoint"],

            };

            return ResponseOutput.Ok(stsToken);

        }




        [HttpGet, Route("imageShare/ShareImage")]
        [AllowAnonymous]
        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("/showdicom?studyId=f7b67793-8155-0223-2f15-118f2642efb8&type=Share&token=" + token);
        }





        [HttpGet("User/UserRedirect")]
        [AllowAnonymous]
        public async Task<IActionResult> UserRedirect([FromServices] IRepository<User> _userRepository, string url, [FromServices] ILogger<ExtraController> _logger)
        {

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

            var userId = decodeUrl.Substring(decodeUrl.IndexOf("UserId=") + "UserId=".Length, 36);

            var token = decodeUrl.Substring(decodeUrl.IndexOf("access_token=") + "access_token=".Length);

            var lang = decodeUrl.Substring(decodeUrl.IndexOf("lang=") + "lang=".Length, 2);

            var domainStrList = decodeUrl.Split("/").ToList().Take(3).ToList();

            var errorUrl = domainStrList[0] + "//" + domainStrList[2] + "/error";


            if (!await _userRepository.AnyAsync(t => t.Id == Guid.Parse(userId) && t.EmailToken == token && t.IsFirstAdd))
            {
                decodeUrl = errorUrl + $"?lang={lang}&ErrorMessage={System.Web.HttpUtility.UrlEncode(lang == "zh" ? "您的初始化链接已过期" : "Error！The initialization link has expired.    Return")} ";
            }

            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());
        }


        [HttpGet, Route("ip2")]
        [AllowAnonymous]
        public IResponseOutput Get2([FromServices] IHttpContextAccessor _context, [FromServices] IRepository _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());
        }

    }
}
