package com.platform.modules.live.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.platform.modules.live.dto.ListJobDto;
import com.platform.modules.live.dto.SubmitJobsDto;

/**
 * @Description: 阿里媒体处理
 * @Author: yikaihong
 * @CreateDate: 2019/1/28 15:28
 * @Version: 1.0
 */
public class AliMediaUtils {

    private static IAcsClient client = null;

    static {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",      // 地域ID
                "LTAIRon7x0qHIhdy",      // RAM账号的AccessKey ID
                "X9AqFQ526j9bjv2fqcdz8KQ5GGzP3A"); // RAM账号Access Key Secret
        client = new DefaultAcsClient(profile);
    }

    /**
     * 提交转码任务
     *
     * @param dto
     * @return
     */
    public static SubmitJobsResponse submitJobs(SubmitJobsDto dto) {
        if (dto == null) {
            return null;
        }
        SubmitJobsRequest request = new SubmitJobsRequest();
        request.setInput(dto.getInput());
        request.setOutputBucket(dto.getOutputBucket());
        request.setOutputLocation(dto.getOutputLocation());
        request.setOutputs(dto.getOutputs());
        request.setPipelineId(dto.getPipelineId());
        try {
            SubmitJobsResponse submitJobsResponse = client.getAcsResponse(request);
            return submitJobsResponse;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 取消任务
     *
     * @param jobId
     * @return
     */
    public static CancelJobResponse cancelJobs(String jobId) {
        if (StringUtils.isEmpty(jobId)) {
            return null;
        }
        CancelJobRequest request = new CancelJobRequest();
        request.setJobId(jobId);
        try {
            CancelJobResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询转码作业
     *
     * @param jobId
     * @return
     */
    public static QueryJobListResponse queryJobList(String jobId) {
        if (StringUtils.isEmpty(jobId)) {
            return null;
        }
        QueryJobListRequest request = new QueryJobListRequest();
        request.setJobIds(jobId);
        try {
            QueryJobListResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 列出转码作业
     *
     * @return
     */
    public static ListJobResponse listJob(ListJobDto listJobDto) {
        ListJobRequest request = new ListJobRequest();
        if (listJobDto != null) {
            if (!StringUtils.isEmpty(listJobDto.getNextPageToken())) {
                request.setNextPageToken(listJobDto.getNextPageToken());
            }
            if (listJobDto.getMaximumPageSize() != null) {
                request.setMaximumPageSize(listJobDto.getMaximumPageSize());
            }
            if (!StringUtils.isEmpty(listJobDto.getState())) {
                request.setState(listJobDto.getState());
            }
            if (!StringUtils.isEmpty(listJobDto.getPipelineId())) {
                request.setPipelineId(listJobDto.getPipelineId());
            }
            if (!StringUtils.isEmpty(listJobDto.getStartOfJobCreatedTimeRange())) {
                request.setStartOfJobCreatedTimeRange(listJobDto.getStartOfJobCreatedTimeRange());
            }
            if (!StringUtils.isEmpty(listJobDto.getEndOfJobCreatedTimeRange())) {
                request.setEndOfJobCreatedTimeRange(listJobDto.getEndOfJobCreatedTimeRange());
            }
        }
        try {
            ListJobResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询管道
     *
     * @param pipelineIds
     * @return
     */
    public static QueryPipelineListResponse queryPipelineList(String pipelineIds) {
        QueryPipelineListRequest request = new QueryPipelineListRequest();
        request.setPipelineIds(pipelineIds);
        try {
            QueryPipelineListResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 管道通知
     *
     * @param pipelineId
     * @param name
     * @param state
     * @param notifyConfig
     * @return
     */
    public static UpdatePipelineResponse updatePipeline(String pipelineId, String name, String state, String notifyConfig) {
        UpdatePipelineRequest request = new UpdatePipelineRequest();
        request.setPipelineId(pipelineId);
        request.setName(name);
        request.setState(state);
        if(!StringUtils.isEmpty(notifyConfig)){
            request.setNotifyConfig(notifyConfig);
        }
        try {
            UpdatePipelineResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
//        String str = "http://zkdx-media-process.oss-cn-hangzhou.aliyuncs.com/20190128/test.mp4";
//        System.err.println("de5f97e6e9a142c2823c77841f404e8f3e843f92523747d5bc506e12f05f19e8".length());
    }
}