package com.huimv.video.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.video.domain.CameraInfo;
import com.huimv.video.domain.PastureArea;
import com.huimv.video.repo.CameraInfoRepo;
import com.huimv.video.repo.PastureAreaRepo;
import com.huimv.video.repo.PublicNetworkRepo;
import com.huimv.video.service.CameraInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.video.service.impl
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/1/7 0007 13:56
 **/
@Service
public class CameraInfoServiceImpl implements CameraInfoService {
    @Autowired
    private CameraInfoRepo cameraInfoRepo;

    @Autowired
    private PublicNetworkRepo publicNetworkRepo;

    @Autowired
    private PastureAreaRepo pastureAreaRepo;

    @Override
    @DS("master")
    public void save(CameraInfo cameraInfo) {
        cameraInfoRepo.save(cameraInfo);
    }

    @Override
    @DS("master")
    public void update(CameraInfo cameraInfo) {
        cameraInfoRepo.save(cameraInfo);
    }

    @Override
    @DS("master")
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            cameraInfoRepo.deleteById(id);
        }
    }

    @Override
    @DS("master")
    public List findAll() {
        List list = new ArrayList();
        List<CameraInfo> all = cameraInfoRepo.findAll();
        for (CameraInfo cameraInfo : all) {
            Map map = new HashMap();

            PastureArea pastureArea = pastureAreaRepo.findAllById( Integer.parseInt(cameraInfo.getAreaId()));

            if (pastureArea.getParent() != null){
                map.put("id",cameraInfo.getId());
                map.put("name",cameraInfo.getName());
                map.put("account",cameraInfo.getAccount());
                map.put("password",cameraInfo.getPassword());
                map.put("areaName",cameraInfo.getAreaName());
                map.put("ip",cameraInfo.getIp());
                map.put("areaId",cameraInfo.getAreaId());
                map.put("remark",cameraInfo.getRemark());
                map.put("areaParentId",pastureArea.getParent());
                map.put("LQID",cameraInfo.getLQID());
                list.add(map);
            }else {
                return list;
            }

        }


        return list;
    }

    @Override
    @DS("master")
    public List<CameraInfo> searchAll(String name) {

        return cameraInfoRepo.findByNameLike(name);
    }

    @Override
    @DS("master")
    public List getCameraDetails(List<Integer> areaIds) {
        List cameraDetailsList = new ArrayList();
        for (Integer areaId : areaIds) {
            List<CameraInfo> cameraList = cameraInfoRepo.findAllByAreaId(areaId);
            String publicIp = publicNetworkRepo.findTopByIdDesc().getUrl();
            for (CameraInfo cameraInfo : cameraList) {
                PastureArea pastureArea = pastureAreaRepo.findAllById( Integer.parseInt(cameraInfo.getAreaId()));
                Map map = new HashMap();
                String cameraAccount =  cameraInfo.getAccount();
                String cameraPassword =  cameraInfo.getPassword();
                String camerIp = cameraInfo.getIp();
                map.put("wsUrl","ws://" + publicIp + ":10080/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword+"%40"+camerIp);
                map.put("rtspUrl","rtsp://"+cameraAccount+":"+cameraPassword+"@"+camerIp);
                map.put("areaId",areaId);
                map.put("areaParentId",pastureArea.getParent());
                cameraDetailsList.add(map);
            }
        }

        return cameraDetailsList;
    }
}
