package com.platform.plugs.live.handlers;

import com.aliyuncs.vod.model.v20170321.*;
import com.platform.plugs.common.rpc.CommonRpc;
import com.platform.plugs.common.rpc.ResponseWrapper;
import com.platform.plugs.common.rpc.RpcResponse;
import com.platform.plugs.live.config.LiveTransCodeConfig;
import com.platform.plugs.live.responseEntity.TranscodeInfo;
import com.platform.plugs.live.responseEntity.TranscodeJob;
import com.platform.plugs.live.responseEntity.TranscodeTask;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author MXY
 * 2019/12/3 10:10
 */
public class LiveTransCodeHandler extends LiveHandler implements ILiveTransCodeHandler {

    private LiveTransCodeConfig config;

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

    @Override
    public void configLiveTransCode(LiveTransCodeConfig config) {
        this.config = config;
    }

    /**
     * 查询视频转码摘要
     */
    @Override
    public RpcResponse<List<TranscodeTask>> transCodeTaskSummary(String videoIds) {

        GetTranscodeSummaryRequest request = new GetTranscodeSummaryRequest();

        request.setVideoIds(videoIds);

        return rpc(request, new ResponseWrapper<List<TranscodeTask>, GetTranscodeSummaryResponse>() {
            @Override
            public List<TranscodeTask> responseWrap(GetTranscodeSummaryResponse response) {

                List<GetTranscodeSummaryResponse.TranscodeSummary> dataList = response.getTranscodeSummaryList();
                List<TranscodeTask> result = new ArrayList<>();

                if (null != dataList && dataList.size() > 0) {
                    for (GetTranscodeSummaryResponse.TranscodeSummary data : dataList) {
                        result.add(TranscodeTask.parse(data));
                    }
                }

                return result;
            }
        });
    }

    /**
     * 查询转码任务列表
     */
    @Override
    public RpcResponse<List<TranscodeTask>> transCodeTaskList(String videoId, Integer pageSize, Integer pageNo) throws Exception {

        ListTranscodeTaskRequest request = new ListTranscodeTaskRequest();

        // 设置请求参数
        request.setVideoId(videoId);
        if (null != pageSize)
            request.setPageSize(pageSize);
        if (null != pageNo)
            request.setPageNo(pageNo);

        return rpc(request, new ResponseWrapper<List<TranscodeTask>, ListTranscodeTaskResponse>() {
            @Override
            public List<TranscodeTask> responseWrap(ListTranscodeTaskResponse response) {

                List<ListTranscodeTaskResponse.TranscodeTask> dataList = response.getTranscodeTaskList();
                List<TranscodeTask> result = new ArrayList<>();
                if (null != dataList && dataList.size() > 0) {
                    for (ListTranscodeTaskResponse.TranscodeTask data : dataList) {
                        TranscodeTask transcodeTask = TranscodeTask.parse(data);
                        result.add(transcodeTask);
                    }
                }
                return result;
            }
        });
    }

    /**
     * 查询转码任务详情
     */
    @Override
    public RpcResponse<TranscodeInfo> transCodeTaskInfo(String taskId) throws Exception {

        GetTranscodeTaskRequest request = new GetTranscodeTaskRequest();

        // 设置请求参数
        request.setTranscodeTaskId(taskId);

        return rpc(request, new ResponseWrapper<TranscodeInfo, GetTranscodeTaskResponse>() {
            @Override
            public TranscodeInfo responseWrap(GetTranscodeTaskResponse response) {

                GetTranscodeTaskResponse.TranscodeTask data = response.getTranscodeTask();
                TranscodeInfo info = new TranscodeInfo();
                List<TranscodeJob> transcodeJobList = data.getTranscodeJobInfoList().stream().map(TranscodeJob::parse).collect(Collectors.toList());

                info.setTranscodeTaskId(data.getTranscodeTaskId());
                info.setVideoId(data.getVideoId());
                info.setTranscodeTemplateGroupId(data.getTranscodeTemplateGroupId());
                info.setTaskStatus(data.getTaskStatus());
                info.setCreationTime(data.getCreationTime());
                info.setCompleteTime(data.getCompleteTime());
                info.setTrigger(data.getTrigger());
                info.setTranscodeJobs(transcodeJobList);

                return info;
            }
        });
    }

    @Override
    public RpcResponse<SubmitTranscodeJobsResponse> submitTransCodeJobs(String videoId, String templateGroupId) {

        SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();

        // 设置需要转码的视频id
        request.setVideoId(videoId);
        // 设置需要使用的模板组id
        request.setTemplateGroupId(templateGroupId);

        return rpc(request, new ResponseWrapper<SubmitTranscodeJobsResponse, SubmitTranscodeJobsResponse>() {
            @Override
            public SubmitTranscodeJobsResponse responseWrap(SubmitTranscodeJobsResponse response) {
                return response;
            }
        });
    }
}
