package com.ruixuan.jiaoyang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;
import com.ruixuan.jiaoyang.service.ICloudVideoProcessService;
import com.ruixuan.jiaoyang.utils.CosConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
@Slf4j
@Service
public class CloudVideoProcessServiceImpl implements ICloudVideoProcessService {

    private static RestTemplate restTemplate = new RestTemplate();

    private static final String SECRET_ID = "AKIDE6IpMi8fJQRCg1iuWzFajjRs43kbbets";
    private static final String SECRET_KEY = "tXzuwMfplTTK3c9GFUyETilasvQfePu9";
    private static final String COS_LOCATION = "ap-chongqing";
    private static final String COS_BUCKET_MEDIA = "live-1301855440";

    @Override
    public String videoCutPictureHandle(String videoPath, String outputPath) {
        String auth = generateAuth(SECRET_ID, SECRET_KEY, "/snapshot", HttpMethodName.POST);
        if (auth.isEmpty()) {
            log.error("腾讯云Authorization获取失败");
        }
        //拼接请求数据万象jobs接口的参数
        JSONObject input = new JSONObject();
        input.put("Object", videoPath);
        JSONObject output = new JSONObject();
        output.put("Region", COS_LOCATION);
        output.put("Bucket", COS_BUCKET_MEDIA);
        output.put("Object", outputPath);
        JSONObject request = new JSONObject();
        request.put("Input", input);
        request.put("Output", output);
        request.put("Time", 0);

        //请求media-1301855440.ci.ap-chongqing.myqcloud.com/snapshot 接口
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", auth);
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<>(request.toJSONString(), headers);
        String response = null;
        try {
            response = restTemplate.exchange(CosConstant.URL_SNAPSHOT, HttpMethod.POST, requestEntity, String.class).getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        if (response.isEmpty()) {
            return CosConstant.URL_COS_ADDRESS + "error.jpg";
        } else {
            return CosConstant.URL_COS_ADDRESS + outputPath;
        }
    }

    public static String generateAuth(String secretId, String secretKey, String path, HttpMethodName httpMethodName) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        COSSigner signer = new COSSigner();
        //设置过期时间为1个小时
        Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L);
        //授权的签名
        return signer.buildAuthorizationStr(
                httpMethodName,
                path,
                cred,
                expiredTime
        );
    }
}
