using CrazyStudio.Core.Common;
using CrazyStudio.Core.Common.AliyunExt.Oss.Tools;
using CrazyStudio.Core.Common.Eitities;
using CrazyStudio.Core.Common.Tools;
using Duende.IdentityModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Text.Json;
using System.Threading.Tasks;
using XPrintServer.Api.Dto;
using XPrintServer.Api.Entities;
using XPrintServer.Api.Tools;
using XPrintServer.DataModel;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Api.Controllers.Aliyun
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OssController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly AliyunInfos _aliyunInfos;
        private readonly OssTool _ossTool;

        private readonly SM2Infos _sm2Infos;
        public OssController(IHttpContextAccessor httpContextAccessor, AliyunInfos aliyunInfos, OssTool ossTool, SM2Infos sm2Infos)
        {
            _httpContextAccessor = httpContextAccessor;
            _aliyunInfos = aliyunInfos;
            _ossTool = ossTool;
            _sm2Infos = sm2Infos;
        }




        /// <summary>
        /// 获取OSS加密信息
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        //[Authorize]
        public object? GetOssInfo()
        {
            //var tokenHandler = new JwtSecurityTokenHandler();
            //var headers = _httpContextAccessor.HttpContext!.Request.Headers["Authorization"];
            //var tokenInfo = tokenHandler.ReadJwtToken(headers.ElementAt(0).Replace("Bearer ", ""));

            //var claimName = tokenInfo.Claims.Where(c => c.Type == JwtClaimTypes.Name).SingleOrDefault();
            //if (claimName != null)
            //{
            var result = StsTool.GetSTS(_aliyunInfos!, _httpContextAccessor.HttpContext!.Session.Id);
            return new
            {
                Token = result,
                _aliyunInfos!.Oss.Bucket,
                Endpoint = _aliyunInfos.Oss.Region + ".aliyuncs.com"
            };
            //}
            //return null;
        }


        [HttpPost]
        public string GetOssObjectName([FromBody] string url)
        {
            string objectName = url.Replace($"https://{_aliyunInfos.Oss.Bucket}.{_aliyunInfos.Oss.Region.Replace("-internal", "")}.aliyuncs.com/", string.Empty)
                .Replace($"http://{_aliyunInfos.Oss.Bucket}.{_aliyunInfos.Oss.Region.Replace("-internal", "")}.aliyuncs.com/", string.Empty);

            return objectName;
        }


        [HttpPost]
        public void DeleteOssObject([FromBody] OssOperation operation)
        {
            if (SM2Tool.Decrypt(operation.PasswordKey, _sm2Infos.PrivateKey) == Global.OssPasswordKey)
            {
                if(operation.Type == OperationType.Delete)
                {
                    _ossTool.DeleteObject(_aliyunInfos.Oss.Bucket, operation.ObjectName);
                }
            }
        }
    }
}
