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

import cn.hutool.core.map.MapUtil;
import com.platform.common.utils.R;
import com.platform.modules.live.constants.LiveConstants;
import com.platform.modules.live.dto.AddCasterVideoResourceDto;
import com.platform.modules.live.dto.AliRequestDto;
import com.platform.modules.live.service.CasterService;
import com.platform.modules.live.utils.AliRequestUtils;
import com.platform.plugs.common.rpc.RpcResponse;
import com.platform.plugs.live.LiveClient;
import com.platform.plugs.live.responseEntity.CasterVideoResource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description:
 * @Author: yikaihong
 * @CreateDate: 2019/1/19 15:09
 * @Version: 1.0
 */
@Service("casterService")
@Transactional
public class CasterServiceImpl implements CasterService {

    @Autowired
    private LiveClient liveClient;

    @Override
    public R addCasterVideoResource(CasterVideoResource resource) {

        RpcResponse response = liveClient.liveCaster().addCasterVideoLiveStreamResource(
                resource.getCasterId(),resource.getResourceName(),resource.getLocationId(),resource.getLiveStreamUrl());
        if (RpcResponse.ERROR_STATUS.equals(response.getStatus())) {
            return R.error().put("errorCode", response.getErrorCode()).put("errorMsg", response.getErrorMsg());
        }
        CasterVideoResource responseEntity = (CasterVideoResource)response.getResponse();
        return R.ok();
    }

    @Override
    public R deleteCasterVideoResource(String casterId, String resourceId) {

        RpcResponse response = liveClient.liveCaster().deleteCasterVideoResource(casterId, resourceId);
        if (RpcResponse.ERROR_STATUS.equals(response.getStatus())) {
            return R.error().put("errorCode", response.getErrorCode()).put("errorMsg", response.getErrorMsg());
        }
        return R.ok();
    }


    @Override
    public R describeCasterVideoResources(String casterId) {

        RpcResponse response = liveClient.liveCaster().describeCasterVideoResources(casterId);
        if (RpcResponse.ERROR_STATUS.equals(response.getStatus())) {
            return R.error().put("errorCode", response.getErrorCode()).put("errorMsg", response.getErrorMsg());
        }
        List<CasterVideoResource> resources = (List<CasterVideoResource>)response.getResponse();
        return R.ok().put("data", resources);
    }


}