package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.LineInterviewDAO;
import com.cool.store.dto.trtc.callback.VideoCallBackDTO;
import com.cool.store.entity.LineInterviewDO;
import com.cool.store.service.TRTCVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class TRTCVideoServiceImpl implements TRTCVideoService {

    @Resource
    private LineInterviewDAO lineInterviewDAO;

    @Override
    public void handleVideoCallBack(VideoCallBackDTO videoCallBackDTO) {
        //将视频播放地址拼接到对应的面试信息字段中
        String videoUrl = videoCallBackDTO.getEventInfo().getPayload().getTencentVod().getVideoUrl();
        String roomId = videoCallBackDTO.getEventInfo().getRoomId();
        LineInterviewDO interviewInfo = lineInterviewDAO.getInterviewInfoByRoomId(roomId);
        if(Objects.isNull(interviewInfo)){
            return;
        }
        String[] split = videoUrl.split(",");
        List<String> videoList = JSONObject.parseArray(interviewInfo.getVideoUrl(), String.class);
        if(Objects.isNull(videoList)){
            videoList = new ArrayList<>();
        }
        videoList.addAll(Stream.of(split).collect(Collectors.toList()));
        interviewInfo.setVideoUrl(JSONObject.toJSONString(videoList.stream().distinct().collect(Collectors.toList())));
        lineInterviewDAO.updateInterviewInfo(interviewInfo);
    }
}
