package com.platform.plugs.live.handlers;

import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsOnlineListRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsOnlineListResponse;
import com.platform.plugs.common.rpc.CommonRpc;
import com.platform.plugs.common.rpc.ResponseResult;
import com.platform.plugs.common.rpc.ResponseWrapper;
import com.platform.plugs.common.rpc.RpcResponse;
import com.platform.plugs.live.annotation.RpcParam;
import com.platform.plugs.live.config.LiveStreamConfig;
import com.platform.plugs.live.responseEntity.LiveStreamOnline;
import com.platform.plugs.live.responseEntity.LiveStreams;
import com.platform.plugs.live.utils.StreamAddressUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author TONG
 * @Date 2019/2/19 14:24
 **/
public class LiveStreamHandler extends LiveHandler implements ILiveStreamHandler {

    private LiveStreamConfig liveStreamConfig;

    public LiveStreamHandler(CommonRpc commonRpc) {
        super(commonRpc);
    }

    public void configLiveStream(LiveStreamConfig liveStreamConfig) {
        this.liveStreamConfig = liveStreamConfig;
    }

    public LiveStreamConfig getLiveStream() {
        return liveStreamConfig;
    }

    /**
     * 生成流地址
     * @param appName
     * @param streamName
     * @param endDate
     * @return
     */
    public LiveStreams generateStreamUrls(String appName, String streamName, Date endDate) {

        Date startDate = new Date();
        LiveStreams streams = new LiveStreams(
                StreamAddressUtils.generatePushUrl(startDate, endDate, appName, streamName, liveStreamConfig),
                StreamAddressUtils.generateLiveUrl(startDate, endDate, appName, streamName, liveStreamConfig),
                startDate.getTime()
        );
        return streams;
    }

    /**
     * 生成推流地址
     * @param appName
     * @param streamName
     * @param endDate
     * @return
     */
    public String generatePushUrl(String appName, String streamName, Date endDate) {
        return StreamAddressUtils.generatePushUrl(new Date(), endDate, appName, streamName, liveStreamConfig);
    }

    /**
     * 生成播流地址
     * @param appName
     * @param streamName
     * @param endDate
     * @return
     */
    public String[] generateLiveUrl(String appName, String streamName, Date endDate) {
        return StreamAddressUtils.generateLiveUrl(new Date(), endDate, appName, streamName, liveStreamConfig);
    }

    /**
     * 查询在线推流的列表，每分钟最多1000次,参数不支持通配符“*”，若无传null
     * @param appName
     * @param streamName
     */
    public List<LiveStreamOnline> describeLiveStreamsOnlineList(String appName, String streamName) {

        if(StringUtils.isBlank(appName)) return null;

        DescribeLiveStreamsOnlineListRequest request = new DescribeLiveStreamsOnlineListRequest();

        request.setDomainName(liveStreamConfig.getLiveDomain());
        request.setAppName(appName);
        if(StringUtils.isBlank(appName))
            request.setStreamName(streamName);

        RpcResponse<List<LiveStreamOnline>> response = rpc(request, new ResponseWrapper<List<LiveStreamOnline>, DescribeLiveStreamsOnlineListResponse>() {

            @Override
            public List<LiveStreamOnline> responseWrap(DescribeLiveStreamsOnlineListResponse response) {
                List<LiveStreamOnline> liveStreamOnlines = new ArrayList<>();
                List<DescribeLiveStreamsOnlineListResponse.LiveStreamOnlineInfo> onlineInfos = response.getOnlineInfo();

                if(onlineInfos != null && onlineInfos.size() > 0) {

                    for (DescribeLiveStreamsOnlineListResponse.LiveStreamOnlineInfo onlineInfo : onlineInfos) {
                        LiveStreamOnline streamOnline = new LiveStreamOnline();
                        streamOnline.setTranscoded(onlineInfo.getTranscoded());
                        streamOnline.setPublishTime(onlineInfo.getPublishTime());
                        streamOnline.setStreamName(onlineInfo.getStreamName());
                        streamOnline.setPublishUrl(onlineInfo.getPublishUrl());
                        streamOnline.setDomainName(onlineInfo.getDomainName());
                        streamOnline.setAppName(onlineInfo.getAppName());
                        streamOnline.setPublishType(onlineInfo.getPublishType());
                        streamOnline.setPublishDomain(onlineInfo.getPublishDomain());

                        liveStreamOnlines.add(streamOnline);
                    }
                }
                return liveStreamOnlines;
            }
        });
        if(response.getResult() != ResponseResult.SUCCESS) return null;

        return response.getResponse();
    }

    /**
     * 历史在线人数，8小时左右延时，endDate在当前时间之内8小时，报错
     * @param appName
     * @param streams
     * @param startDate
     * @param endDate
     * @return
     */
    public int describeLiveStreamHistoryUserNum(String appName, String[] streams, Date startDate, Date endDate) {

//        if(StringUtils.isBlank(appName) || streams)
//        if(endDate == null)
//            endDate = new Date();
        return 0;
    }
    /**
     * 生成直播时移时间轴查询地址
     * @param startDate
     * @param endDate
     * @param appName
     * @param streamName
     * 20200305 weiyan
     * @return
     */
    public String generateQueryTimeLineUrl(int startDate, int endDate, String appName, String streamName){
    	return StreamAddressUtils.generateQueryTimeLineUrl(startDate, endDate, appName, streamName, liveStreamConfig);
    }
    
    /**
     * 生成直播时移地址
     *
     * @param startDate
     * @param appName
     * @param streamName
     * @return
     */
    public String generateTimeShiftUrl(Date startDate,String appName, String streamName){
    	return StreamAddressUtils.generateTimeShiftUrl(startDate, appName, streamName, liveStreamConfig);
    }
}
