package com.huimv.video.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.video.video.entity.BaseSpecificVideo;
import com.huimv.video.video.entity.Camera;
import com.huimv.video.video.entity.CameraBrand;
import com.huimv.video.video.entity.vo.CameraVo;
import com.huimv.video.video.mapper.BaseSpecificVideoMapper;
import com.huimv.video.video.mapper.CameraAreaMapper;
import com.huimv.video.video.mapper.CameraBrandMapper;
import com.huimv.video.video.mapper.CameraMapper;
import com.huimv.video.video.service.IBaseSpecificVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-20
 */
@Service
public class BaseSpecificVideoServiceImpl extends ServiceImpl<BaseSpecificVideoMapper, BaseSpecificVideo> implements IBaseSpecificVideoService {
    @Autowired
    private CameraMapper cameraMapper;

    @Autowired
    private CameraAreaMapper cameraAreaMapper;

    @Autowired
    private CameraBrandMapper cameraBrandMapper;

    @Override
    public List listSpecif(Integer farmId, Integer type) {
        List<BaseSpecificVideo> list = this.list(new QueryWrapper<BaseSpecificVideo>().eq("farm_id", farmId).eq("type", type));

        List list1 = new ArrayList();
        for (BaseSpecificVideo baseSpecificVideo : list) {

            Camera camera = cameraMapper.selectById(baseSpecificVideo.getVideoId());

            list1.add(getCameraVo(camera));
        }

        return list1;
    }


    public CameraVo getCameraVo(Camera camera) {


        String cameraAccount = camera.getAccount();
        String cameraPassword = camera.getPassword();
        String camerIp = camera.getCameraIp();
        String cameraName = camera.getCameraName();
        Integer areaId = camera.getAreaId();
        String url = cameraAreaMapper.selectById(areaId).getPublicUrl();

        String rtsp;
        CameraBrand cameraBrand = cameraBrandMapper.selectById(camera.getBrandId());
        Integer type = cameraBrand.getStreamType();
        if (type == 0) {
            rtsp = cameraBrand.getMainStream();
        } else {
            rtsp = cameraBrand.getAssistStream();
        }


        CameraVo cameraVo = new CameraVo();
        cameraVo.setAreaId(areaId);
        if (camera.getId() == 767){
            cameraVo.setWsUrl("ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
            cameraVo.setRtspUrl("rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + "/cam/realmonitor?channel=2&subtype=0");
        }else {
            cameraVo.setWsUrl("ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
            cameraVo.setRtspUrl("rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + rtsp);
        }

        cameraVo.setCameraName(cameraName);
        return cameraVo;
    }
}
