using BPA.SAAS.Manage.Comm.Const;
using BPA.SAAS.Manage.Core.Org;
using COSXML.Auth;
using COSXML.Model.Object;
using COSXML.Transfer;
using COSXML;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BPA.SAAS.Manage.Application.System.Dtos;

namespace BPA.SAAS.Manage.Application.System.Services
{
    public class TransferUploadObjectModel
    {

        private readonly ISqlSugarClient _db;
        private CosXml cosXml;
        public TransferUploadObjectModel(ISqlSugarClient db)
        {
            var cosConfig = CosConfig.cosInfoOptions;
            _db = db;   // 推荐操作
            CosXmlConfig config = new CosXmlConfig.Builder()
                .SetRegion(cosConfig.Region) // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
                .Build();

            string secretId = cosConfig.SecretId;   // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            string secretKey = cosConfig.SecretKey; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            long durationSecond = 6000;          //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
                secretKey, durationSecond);

            cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }



        /// 高级接口上传文件
        public async Task<string> TransferUploadFile(FileInputInfoDto inputDto, byte[] data)
        {
            var cosConfig = CosConfig.cosInfoOptions;
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();
            // 手动设置开始分块上传的大小阈值为10MB，默认值为5MB
            transferConfig.DivisionForUpload = 10 * 1024 * 1024;
            // 手动设置分块上传中每个分块的大小为2MB，默认值为1MB
            transferConfig.SliceSizeForUpload = 2 * 1024 * 1024;


            string GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
            string baseUrl = "/Franchisee/";
            if (!string.IsNullOrWhiteSpace(GroupId))
            {
                var Companycheck = _db.Queryable<BPA_Company>().Where(x => x.Id == GroupId).First();
                baseUrl = baseUrl + Companycheck.Code + "/";
            }
            DateTime dt = DateTime.Now;
            baseUrl = baseUrl + inputDto.Directory + "/" + dt.ToFileTime().ToString() + "." + inputDto.FileExtension;



            try
            {
                // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
                string bucket = cosConfig.Bucket;
                string cosPath = baseUrl; //对象在存储桶中的位置标识符，即称对象键

                // byte[] data = new byte[1024]; // 二进制数据

                PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, data);
                // 发起上传
                PutObjectResult result = cosXml.PutObject(putObjectRequest);
                Console.WriteLine(result.GetResultInfo());
                return @"https://" + cosConfig.Bucket + ".cos.ap-chengdu.myqcloud.com/" + cosPath;
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
            return "";

        }


    }
}
