package com.xxl.job.executor.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.mps.v20190612.MpsClient;
import com.tencentcloudapi.mps.v20190612.models.*;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class CosUtil {
    public static final String secretId = "AKIDE6IpMi8fJQRCg1iuWzFajjRs43kbbets";
    public static final String secretKey = "tXzuwMfplTTK3c9GFUyETilasvQfePu9";
    public static final String region = "ap-chongqing";
    public static final String bucket = "media-1301855440";
    public static final String host = "https://" + bucket +".cos.ap-chongqing.myqcloud.com";


    //创建采样视频截图模板
    public static void createSampleSnapshotTemplate(){
        try{

            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("mps.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            MpsClient client = new MpsClient(cred, region, clientProfile);

            String params = "{\"Name\":\"sampleSnapshotTemplate1\",\"SampleType\":\"Percent\",\"SampleInterval\":7}";

            CreateSampleSnapshotTemplateRequest req = CreateSampleSnapshotTemplateRequest.fromJsonString(params, CreateSampleSnapshotTemplateRequest.class);

            CreateSampleSnapshotTemplateResponse resp = client.CreateSampleSnapshotTemplate(req);

            log.info(CreateSampleSnapshotTemplateRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
        }
    }

    /**
     * 视频处理模块
     * 查询已经创建的模板
     */
    public static void DescribeSampleSnapshotTemplates(){
            try{
                Credential cred = new Credential(secretId, secretKey);

                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setEndpoint("mps.tencentcloudapi.com");

                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);

                MpsClient client = new MpsClient(cred, region, clientProfile);

                String params = "{}";
                DescribeSampleSnapshotTemplatesRequest req = DescribeSampleSnapshotTemplatesRequest.fromJsonString(params, DescribeSampleSnapshotTemplatesRequest.class);

                DescribeSampleSnapshotTemplatesResponse resp = client.DescribeSampleSnapshotTemplates(req);

                log.info(DescribeSampleSnapshotTemplatesRequest.toJsonString(resp));
            } catch (TencentCloudSDKException e) {
                log.error(e.toString());
            }
    }

    //查询抽帧任务状态
    public static String describeTaskDetail(String taskId){
            try{
                Credential cred = new Credential(secretId, secretKey);

                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setEndpoint("mps.tencentcloudapi.com");

                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);

                MpsClient client = new MpsClient(cred, region, clientProfile);

                String params = "{\"TaskId\":\"" + taskId + "\"}";
                DescribeTaskDetailRequest req = DescribeTaskDetailRequest.fromJsonString(params, DescribeTaskDetailRequest.class);

                DescribeTaskDetailResponse resp = client.DescribeTaskDetail(req);

                log.info(DescribeTaskDetailRequest.toJsonString(resp));
                return DescribeTaskDetailRequest.toJsonString(resp);
            } catch (TencentCloudSDKException e) {
                log.error(e.toString());
                return null;
            }
    }

    //抽帧      42095--已经创建好的采样截图模板，每7%抽一张，一共15张，第一张首帧
    //videoPath视频路径，包括文件名 高清.mp4     snapshotPath 视频截图路径（包括名字） /高清
    public static String processMedia(String videoPath, String snapshotPath){
        try{
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("mps.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            MpsClient client = new MpsClient(cred, region, clientProfile);

            String params = "{\"InputInfo\":{\"Type\":\"COS\",\"CosInputInfo\":{\"Bucket\":\"" + bucket + "\",\"Region\":\"" + region + "\",\"Object\":\"" + videoPath + "\"}},\"MediaProcessTask\":{\"SampleSnapshotTaskSet\":[{\"Definition\":42095,\"OutputStorage\":{\"Type\":\"COS\",\"CosOutputStorage\":{\"Bucket\":\""+ bucket +"\",\"Region\":\"" + region + "\"}},\"OutputObjectPath\":\"" + snapshotPath + "\"}]}}";

            ProcessMediaRequest req = ProcessMediaRequest.fromJsonString(params, ProcessMediaRequest.class);

            ProcessMediaResponse resp = client.ProcessMedia(req);

            log.info(ProcessMediaRequest.toJsonString(resp));
            return ProcessMediaRequest.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            return null;
        }
    }


    public static void main(String[] args) {
        //创建采样截图模板
        createSampleSnapshotTemplate();
        //查询采样截图模板
        //DescribeSampleSnapshotTemplates();
        //查询抽帧任务状态
        //describeTaskDetail();

    }


}
