package com.platform.modules.transcode.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.platform.common.utils.Query;
import com.platform.common.utils.R;
import com.platform.modules.live.entity.LiveTranscodeInfoEntity;
import com.platform.modules.live.entity.LiveTranscodeJobEntity;
import com.platform.modules.live.service.LiveTranscodeInfoService;
import com.platform.modules.live.service.LiveTranscodeJobService;
import com.platform.modules.transcode.service.TranscodeService;
import com.platform.modules.transcode.vo.TranscodeInfoVo;
import com.platform.plugs.common.rpc.ResponseResult;
import com.platform.plugs.common.rpc.RpcResponse;
import com.platform.plugs.live.LiveClient;
import com.platform.plugs.live.responseEntity.TranscodeTask;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author MXY
 * 2020/1/7 18:30
 */
@Service
public class TranscodeServiceImpl implements TranscodeService {

    private LiveClient liveClient;

    private LiveTranscodeInfoService liveTranscodeInfoService;

    private LiveTranscodeJobService liveTranscodeJobService;

    public TranscodeServiceImpl(LiveClient liveClient, LiveTranscodeInfoService liveTranscodeInfoService, LiveTranscodeJobService liveTranscodeJobService) {
        this.liveClient = liveClient;
        this.liveTranscodeInfoService = liveTranscodeInfoService;
        this.liveTranscodeJobService = liveTranscodeJobService;
    }

    @Override
    public R list(String videoId, Integer pageSize, Integer pageNo) {

        List<TranscodeTask> response = null;
        try {
            final RpcResponse<List<TranscodeTask>> rpcResponse = liveClient.liveTransCode().transCodeTaskList(videoId, pageSize, pageNo);

            if (ResponseResult.ERROR.equals(rpcResponse.getResult()))
                return R.error("获取转码列表失败");

            response = rpcResponse.getResponse();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("list", response);
    }

    @Override
    public R info(String videoId) {

        // 根据taskId获取
        LiveTranscodeInfoEntity infoEntity = liveTranscodeInfoService.getByVideoId(videoId);

        if (null == infoEntity)
            return R.error("获取详情失败");

        List<LiveTranscodeJobEntity> jobEntityList = liveTranscodeJobService.getByTaskId(infoEntity.getTranscodeTaskId());

        TranscodeInfoVo infoVo = new TranscodeInfoVo();

        BeanUtils.copyProperties(infoEntity, infoVo);
        infoVo.setJobEntityList(jobEntityList);

        return R.ok().put("info", infoVo);
    }

    @Override
    public Page page(Map<String, Object> params) {

        int current = 1;
        int size = 10;

        String strPage = (String) params.get("page");
        String strLimit = (String) params.get("limit");

        if (StringUtils.isNotBlank(strPage))
            current = Integer.valueOf(strPage);
        if (StringUtils.isNotBlank(strLimit))
            size = Integer.valueOf(strLimit);

        EntityWrapper<LiveTranscodeInfoEntity> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id", false);

        Page<LiveTranscodeInfoEntity> page = liveTranscodeInfoService.selectPage(
                new Query<LiveTranscodeInfoEntity>(params).getPage(),
                wrapper
        );

        page.setCurrent(current);
        page.setSize(size);

        return page;
    }
}
