package com.platform.plugs.live.handlers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.live.model.v20161101.AddLivePullStreamInfoConfigRequest;
import com.aliyuncs.live.model.v20161101.AddLivePullStreamInfoConfigResponse;
import com.aliyuncs.live.model.v20161101.DeleteLivePullStreamInfoConfigRequest;
import com.aliyuncs.live.model.v20161101.DeleteLivePullStreamInfoConfigResponse;
import com.aliyuncs.live.model.v20161101.DescribeLivePullStreamConfigRequest;
import com.aliyuncs.live.model.v20161101.DescribeLivePullStreamConfigResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.platform.plugs.live.config.LivePullStreamConfig;

/**
 * @Author weiyan
 * @Date 2021/1/25
 **/
public class LivePullStreamHandler implements ILivePullStreamHandler {
	
	private LivePullStreamConfig livePullStreamConfig;
	
	private DefaultAcsClient client;
	
	@Override
	public void configLivePullStream(LivePullStreamConfig livePullStreamConfig) {
		this.livePullStreamConfig = livePullStreamConfig;
		this.client = new DefaultAcsClient(DefaultProfile.getProfile(livePullStreamConfig.getApiRegionId(), livePullStreamConfig.getAccessKeyId(), livePullStreamConfig.getAccessKeySecret()));
	}

	@Override
	public AddLivePullStreamInfoConfigResponse addLivePullStreamInfoConfig(String appName, String streamName,
			String sourceUrl, String startTime, String endTime) throws  ServerException,ClientException {
		AddLivePullStreamInfoConfigRequest request = new AddLivePullStreamInfoConfigRequest();
		request.setDomainName(livePullStreamConfig.getDomainName());
        request.setAppName(appName);
        request.setStreamName(streamName);
        request.setSourceUrl(sourceUrl);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setRegionId(livePullStreamConfig.getApiRegionId());
        
       return client.getAcsResponse(request);
	}

	@Override
	public DescribeLivePullStreamConfigResponse describeLivePullStreamConfig() throws  ServerException,ClientException {
		DescribeLivePullStreamConfigRequest request = new DescribeLivePullStreamConfigRequest();
		request.setRegionId(livePullStreamConfig.getApiRegionId());
        request.setDomainName(livePullStreamConfig.getDomainName());
        return client.getAcsResponse(request);
	}

	@Override
	public DeleteLivePullStreamInfoConfigResponse DeleteLivePullStreamInfoConfig(String appName, String streamName)  throws  ServerException,ClientException {
		DeleteLivePullStreamInfoConfigRequest request = new DeleteLivePullStreamInfoConfigRequest();
        request.setRegionId(livePullStreamConfig.getApiRegionId());
        request.setAppName(appName);
        request.setStreamName(streamName);
        request.setDomainName(livePullStreamConfig.getDomainName());
        return client.getAcsResponse(request);
	}
	
	public static void main(String[] args) {
		LivePullStreamConfig livePullStreamConfig = new LivePullStreamConfig( "huanghelive.zhongkedongxin.com", "LTAIVvNJzQ3Etp6q","nCu0fyV85yn3jes5lRwqx7Q9qxRhSS","cn-beijing");
		DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(livePullStreamConfig.getApiRegionId(), livePullStreamConfig.getAccessKeyId(), livePullStreamConfig.getAccessKeySecret()));
		AddLivePullStreamInfoConfigRequest request = new AddLivePullStreamInfoConfigRequest();
		request.setDomainName(livePullStreamConfig.getDomainName());
        request.setAppName("Q3KYorao");
        request.setStreamName("7ca5f2b2e51f4ccbb15102fff8c4742c");
        request.setSourceUrl("http://livemonitor.yunshicloud.com/6000001122212370/29d65e3c983b11eab1cbfa163e683eda/index.m3u8");

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
	    TimeZone utcZone = TimeZone.getTimeZone("UTC");
	    sf.setTimeZone(utcZone);

	  //设置创建时间
        Date creatTime = new Date();
        //设置生效时间为一小时后
        Calendar cal = Calendar.getInstance();
        cal.setTime(creatTime);
        cal.add(Calendar.HOUR, 1);// 24小时制

        request.setStartTime(sf.format(new Date()));
        request.setEndTime(sf.format(cal.getTime()));
        request.setRegionId(livePullStreamConfig.getApiRegionId());
        try {
			AddLivePullStreamInfoConfigResponse response = client.getAcsResponse(request);
			System.out.println(new Gson().toJson(response));
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
