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

import com.platform.common.utils.R;
import com.platform.modules.live.dto.GenerateLiveUrlDto;
import com.platform.modules.live.service.GenerateLiveUrlService;
import com.platform.modules.live.vo.GenerateLiveUrlVo;
import com.platform.plugs.live.LiveClient;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description:
 * @Author: yikaihong
 * @CreateDate: 2019/1/19 12:24
 * @Version: 1.0
 */
@Service("generateLiveUrlService")
@AllArgsConstructor
@Transactional
public class GenerateLiveUrlServiceImpl implements GenerateLiveUrlService {


    private SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    private LiveClient liveClient;

    @Override
    public R generateLiveUrl(GenerateLiveUrlDto dto) {
        if (dto == null) {
            return R.ok();
        }
        Date timestamp = new Date();
        String[] liveUrls = liveClient.liveStream().generateLiveUrl(dto.getAppName(), dto.getStreamName(), timestamp);
        String pushUrl = liveClient.liveStream().generatePushUrl(dto.getAppName(), dto.getStreamName(), timestamp);
        String liveUrl = liveUrls[0];
        String liveUrlFlv = liveUrls[1];
        String liveUrlM3u8 = liveUrls[2];
        List<String> pushUrlList = new ArrayList<>();
        List<String> liveUrlList = new ArrayList<>();
        pushUrlList.add(pushUrl);
        liveUrlList.add(liveUrl);
        liveUrlList.add(liveUrlFlv);
        liveUrlList.add(liveUrlM3u8);
        GenerateLiveUrlVo vo = new GenerateLiveUrlVo(timestamp, pushUrlList, liveUrlList);
        simpMessagingTemplate.convertAndSend("/queue/subscribeLiveUrl",vo);
        return R.ok().put("data",vo );
    }
}