package com.zcloud.util.ali;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.kms.model.v20160120.GenerateDataKeyRequest;
import com.aliyuncs.kms.model.v20160120.GenerateDataKeyResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.zcloud.entity.PageData;
import com.zcloud.util.Const;
public class SubmitTrans {
	//填入AccessKey信息
	public static DefaultAcsClient initVodClient() throws ClientException {
	    String regionId = Const.ALIYUN_REGIONID;  // 点播服务接入地域
	    DefaultProfile profile = DefaultProfile.getProfile(regionId, Const.ALI_ACCESSKEY, Const.ALI_ACCESSKEY_SECRET);
	    DefaultAcsClient client = new DefaultAcsClient(profile);
	    return client;
	}
	/**
	 * 提交媒体处理作业
	 */
	public static SubmitTranscodeJobsResponse submitTranscodeJobs(DefaultAcsClient client,PageData pd) throws Exception {
	    SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
	    //需要转码的视频ID
	    request.setVideoId(pd.getString("videoId"));
	    //转码模板ID
	    request.setTemplateGroupId(Const.ALI_TEMPLATEGROUPID);
//	    //构建需要替换的水印参数(只有需要替换水印相关信息才需要构建)
	    JSONObject overrideParams = buildOverrideParams();
//	    //覆盖参数，暂只支持水印部分参数替换(只有需要替换水印相关信息才需要传递)
	    request.setOverrideParams(overrideParams.toJSONString());
//	    //构建标准加密配置参数(只有标准加密才需要构建)
//	    JSONObject encryptConfig = buildEncryptConfig(client);
//	    //HLS标准加密配置(只有标准加密才需要传递)
//	    request.setEncryptConfig(encryptConfig.toJSONString());
	    return client.getAcsResponse(request);
	}

	public static GetTranscodeSummaryResponse getTranscodeSummary(DefaultAcsClient client,PageData pd) throws Exception {
	    GetTranscodeSummaryRequest request = new GetTranscodeSummaryRequest();
	    request.setVideoIds(pd.getString("videoId"));
	    return client.getAcsResponse(request);
	}
	/**
	 * 查询转码任务列表
	 */
	public static ListTranscodeTaskResponse listTranscodeTask(DefaultAcsClient client) throws Exception {
	    ListTranscodeTaskRequest request = new ListTranscodeTaskRequest();
	    request.setVideoId("7a58a8da8e3a4bd48c72cade824abd5e");
	    return client.getAcsResponse(request);
	}
//	public static void main(String[] args) throws ClientException {
//	    DefaultAcsClient client = initVodClient();
//	    ListTranscodeTaskResponse response = new ListTranscodeTaskResponse();
//	    try {
//	        response = listTranscodeTask(client);
//	        System.out.println(JSON.toJSON(response));
//	        for (ListTranscodeTaskResponse.TranscodeTask transcodeTask : response.getTranscodeTaskList()) {
//	            //转码任务ID
//	            System.out.println("TranscodeTaskId = " + transcodeTask.getTranscodeTaskId());
//	            //转码任务状态
//	            System.out.println("TaskStatus = " + transcodeTask.getTaskStatus());
//	        }
//	    } catch (Exception e) {
//	        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
//	    }
//	    System.out.println("RequestId = " + response.getRequestId());
//	}

	/**
	 * 以下为调用示例
	 */
//	public static void main(String[] args) throws ClientException {
//	    DefaultAcsClient client = initVodClient();
//	    GetTranscodeSummaryResponse response = new GetTranscodeSummaryResponse();
//	    try {
//	    	PageData pdData  = new PageData();
//	    	pdData.put("videoId", "8e67102e77864c2bad58fd2c5e6c07cb");
//	        response = getTranscodeSummary(client, pdData);
//
//		    System.out.println(JSON.toJSONString(response));
//	        for (GetTranscodeSummaryResponse.TranscodeSummary summary : response.getTranscodeSummaryList()) {
//	            //转码状态
//	            System.out.println("TranscodeStatus = " + summary.getTranscodeStatus());
//	            for (GetTranscodeSummaryResponse.TranscodeSummary.TranscodeJobInfoSummary jobInfoSummary : summary.getTranscodeJobInfoSummaryList()) {
//	                //转码进度
//	                System.out.println("TranscodeProgress = " + jobInfoSummary.getTranscodeProgress());
//	                //转码作业状态
//	                System.out.println("TranscodeJobStatus = " + jobInfoSummary.getTranscodeJobStatus());
//	                //转码作业完成时间
//	                System.out.println("CompleteTime = " + jobInfoSummary.getCompleteTime());
//	            }
//	        }
//	    } catch (Exception e) {
//	        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
//	    }
//	    System.out.println("RequestId = " + response.getRequestId());
//	}
	/**
	 * 以下为调用示例
	 */
	public static void main(String[] args) throws ClientException {
	    DefaultAcsClient client = initVodClient();
	    SubmitTranscodeJobsResponse response = new SubmitTranscodeJobsResponse();
	    try {PageData pdData  = new PageData();
     	pdData.put("videoId", "2b85bcb6e7744b6489c18810c6788e85");
	        response = submitTranscodeJobs(client,pdData);
	        //任务ID
	        System.out.println("JobId = " + response.getTranscodeJobs().get(0).getJobId());
	        System.out.println(JSON.toJSON(response));
	    } catch (Exception e) {
	        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
	    }
	    System.out.println("RequestId = " + response.getRequestId());
	}

	/**
	 * 构建HLS标准加密的配置信息
	 * @return
	 * @throws ClientException
	 */
	public static JSONObject buildEncryptConfig(DefaultAcsClient client) throws ClientException {
	    //点播给用户在KMS(密钥管理服务)中的Service Key，可在用户密钥管理服务对应的区域看到描述为vod的service key
	    String serviceKey = "<您的Service Key>";
	    //随机生成一个加密的密钥，返回的response包含明文密钥以及密文密钥
	    //视频标准加密只需要传递密文密钥即可
	    //注意：KMS Client建议单独初始化来保证正确的接入区域，可参考VOD初始化方式，传入正确的KMS服务区域。
	    GenerateDataKeyResponse response = generateDataKey(client, serviceKey);
	    JSONObject encryptConfig = new JSONObject();
	    //解密接口地址，该参数需要将每次生成的密文密钥与接口URL拼接生成，表示每个视频的解密的密文密钥都不一样
	    //至于Ciphertext这个解密接口参数的名称，用户可自行制定，这里只作为参考参数名称
	    encryptConfig.put("DecryptKeyUri", "http://example.aliyundoc.com/decrypt?" +
	            "Ciphertext=" + response.getCiphertextBlob());
	    //密钥服务的类型，目前只支持KMS
	    encryptConfig.put("KeyServiceType", "KMS");
	    //密文密钥
	    encryptConfig.put("CipherText", response.getCiphertextBlob());
	    return encryptConfig;
	}

	/**
	 * 1、构建覆盖参数，目前只支持图片水印文件地址、文字水印的内容覆盖；
	 * 2、需要替换的水印信息对应水印ID必须是关联在指定的模板ID(即TranscodeTemplateId)中；
	 * 3、不支持通过媒体处理接口去增加一个没有关联上的水印
	 * 注意：图片水印的文件存储源站需要和发起转码的视频存储源站一致
	 * @return
	 */
	public static JSONObject buildOverrideParams() {
	    JSONObject overrideParams = new JSONObject();
	    JSONArray watermarks = new JSONArray();
	    //图片水印文件地址替换
//	    JSONObject watermark1 = new JSONObject();
//	    //模板上面关联需要替换的水印文件图片水印ID
//	    watermark1.put("WatermarkId", "2ea587477c5a1bc8b57****");
//	    //需要替换成对应图片水印文件的OSS地址，水印文件存储源站需要和视频存储源站一致
//	    watermark1.put("FileUrl", "https:192.168.0.1/16");

	    //文字水印内容替换
	    JSONObject watermark2 = new JSONObject();
	    //模板上面关联需要替换内容的文字水印ID
	    watermark2.put("WatermarkId", "623bf217d2aa65e1ed051df06f17e09a");
	    //需要替换成对应的内容
	    watermark2.put("Content", "用户ID：6****");
	    watermarks.add(watermark2);
	    overrideParams.put("Watermarks", watermarks);
	    return overrideParams;
	}

	/**
	 * 生成加密需要的密钥，response中包含密文密钥和明文密钥，用户只需要将密文密钥传递给点播即可
	 * 注意：KeySpec 必须传递AES_128，且不能设置NumberOfBytes
	 * @param client KMS-SDK客户端
	 * @param serviceKey 点播提供生成密钥的service key，在用户的密钥管理服务中可看到描述为vod的加密key
	 * @return
	 * @throws ClientException
	 */
	public static GenerateDataKeyResponse generateDataKey(DefaultAcsClient client, String serviceKey) throws ClientException {
	    GenerateDataKeyRequest request = new GenerateDataKeyRequest();
	    request.setKeyId(serviceKey);
	    request.setKeySpec("AES_128");
	    return client.getAcsResponse(request);
	}
}
