package com.aiotagro.common.core.utils;

import com.aiotagro.common.core.constant.TencentCloudConstants;
import com.alibaba.fastjson2.JSON;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Carson 腾讯云盘工具类
 * @package_name com.aiotagro.common.core.utils
 * @date 2024/3/7 12:05
 */
public class TencentCloudUtil {
    private static final Logger logger = LoggerFactory.getLogger(TencentCloudUtil.class);


    // 1. 初始化用户身份信息
    static COSCredentials cred = new BasicCOSCredentials(TencentCloudConstants.TENCENT_CLOUD_SECRETID, TencentCloudConstants.TENCENT_CLOUD_SECRETKEY);
    static Credential cred1 = new Credential(TencentCloudConstants.TENCENT_CLOUD_AIPITE_SECRETID, TencentCloudConstants.TENCENT_CLOUD_AIPITE_SECRETKEY);
    //    // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
    static Region region = new Region(TencentCloudConstants.TENCENT_CLOUD_REGION);
    static ClientConfig clientConfig = new ClientConfig(region);
    // 3 生成cos客户端
    static COSClient cosClient = new COSClient(cred, clientConfig);
    static SmsClient smsClient;
    static{
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        smsClient = new SmsClient(cred1, "ap-nanjing", clientProfile);
    }

    /**
     * 文件上传
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static String upload(MultipartFile multipartFile) throws IOException {

        // 当前日期
        String currentDate = NewDateUtils.getCurrentDate("yyyy/MM/dd");
        //获件名
        String fileName = multipartFile.getOriginalFilename();
        // 文件名前缀
        String originalName = fileName.substring(0, fileName.lastIndexOf("."));
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        String name = originalName + NewDateUtils.getCurrentDate(NewDateUtils.DATE_TIME_FORMAT_SHORT) + suffixName;
        // 指定文件上传到 COS 上的路径
        String key = "iotPlateform/" + currentDate + "/" + name;
        // 创建 ObjectMetadata 对象，并设置内容长度
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(multipartFile.getSize());
        PutObjectRequest putObjectRequest = new PutObjectRequest(TencentCloudConstants.TENCENT_CLOUD_BUCKET, key, multipartFile.getInputStream(), objectMetadata);
        // 上传
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        logger.info("腾讯文件上传结果 {}", JSON.toJSONString(putObjectResult));
        String targetUrl = "https://" + TencentCloudConstants.TENCENT_CLOUD_BUCKET + ".cos." + TencentCloudConstants.TENCENT_CLOUD_REGION + ".myqcloud.com/" + key;
        logger.info("腾讯文件访问url {}", targetUrl);
        return targetUrl;
    }

    /**
     * 获取COS客户端
     * @return COS客户端实例
     */
    public static COSClient getCosClient() {
        return cosClient;
    }

}
