package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.management.dao.*;
import com.huimv.management.entity.CameraBrandEntity;
import com.huimv.management.entity.PublicNetworkEntity;
import com.huimv.management.entity.UserAttentionEntity;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
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;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;

import com.huimv.management.entity.CameraInfoEntity;
import com.huimv.management.service.CameraInfoService;


@Service("cameraInfoService")
public class CameraInfoServiceImpl extends ServiceImpl<CameraInfoDao, CameraInfoEntity> implements CameraInfoService {
    @Autowired
    private CameraInfoDao cameraInfoDao;

    @Autowired
    private PublicNetworkDao publicNetworkDao;

    @Autowired
    private PastureAreaDao pastureAreaDao;

    @Autowired
    private CameraBrandDao cameraBrandDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params,Long userId) {

        //根据牧场查询
        String farmId = (String) params.get("farmId");

        QueryWrapper<CameraInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(farmId), "farm_id",farmId);

        //关键词
        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like("name", keyword)
                    .or().like("area_name", keyword)
                    .or().like("ip", keyword)
                    .or().like("camera_brand", keyword);
        }

        String areaId = (String) params.get("areaId");
        if (StringUtils.isNotBlank(areaId)) {
            wrapper.eq("area_id", Integer.parseInt(areaId));
        }


        IPage<CameraInfoEntity> page = this.page(
                new Query<CameraInfoEntity>().getPage(params),
                wrapper
        );
        List<CameraInfoEntity> records = page.getRecords();
        addAttention(records,userId);

        return new PageUtils(page);
    }

    @Override
    public List getCameraDetails(Integer cameraId) {
        List cameraDetailsList = new ArrayList();
        CameraInfoEntity cameraInfo = cameraInfoDao.selectById(cameraId);
        Integer farmId = cameraInfo.getFarmId();
        QueryWrapper<PublicNetworkEntity> urlWrapper = new QueryWrapper<PublicNetworkEntity>();
        urlWrapper.eq("farm_id", farmId);
        String url = publicNetworkDao.selectOne(urlWrapper).getUrl();

        Map map = new HashMap();
        String cameraAccount = cameraInfo.getAccount();
        String cameraPassword = cameraInfo.getPassword();
        String camerIp = cameraInfo.getIp();
        Integer areaId = cameraInfo.getAreaId();


        String rtsp ;
        CameraBrandEntity cameraBrandEntity = cameraBrandDao.selectById(cameraInfo.getCameraBrandId());
        Boolean type = cameraBrandEntity.getType();
        if (type){
            rtsp = cameraBrandEntity.getMainStream();
        }else {
            rtsp = cameraBrandEntity.getAssistStream();
        }

        map.put("wsUrl", "ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
        map.put("rtspUrl", "rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + rtsp);
        map.put("areaId", areaId);

        cameraDetailsList.add(map);


        return cameraDetailsList;
    }

    @Override
    public List getByAreaId(Integer areaId,Long userId) {
        List cameraDetailsList = new ArrayList();

        LambdaQueryWrapper<CameraInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CameraInfoEntity::getAreaId, areaId);
        List<CameraInfoEntity> cameraInfoEntities = cameraInfoDao.selectList(wrapper);

        QueryWrapper<PublicNetworkEntity> urlWrapper = new QueryWrapper<PublicNetworkEntity>();

        List<CameraInfoEntity> cameraInfoEntities1 = addAttention(cameraInfoEntities, userId);

        for (CameraInfoEntity cameraInfo : cameraInfoEntities) {
            String rtsp ;

            Map map = new HashMap();
            Integer farmId = cameraInfo.getFarmId();
            urlWrapper.eq("farm_id", farmId);
            String url = publicNetworkDao.selectOne(urlWrapper).getUrl();
            urlWrapper.clear();

            String cameraAccount = cameraInfo.getAccount();
            String cameraPassword = cameraInfo.getPassword();
            String camerIp = cameraInfo.getIp();
            CameraBrandEntity cameraBrandEntity = cameraBrandDao.selectById(cameraInfo.getCameraBrandId());
            Boolean type = cameraBrandEntity.getType();
            if (type){
                rtsp = cameraBrandEntity.getMainStream();
            }else {
                rtsp = cameraBrandEntity.getAssistStream();
            }
            map.put("wsUrl", "ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
            map.put("rtspUrl", "rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + rtsp);
            map.put("areaId", areaId);
            map.put("name",cameraInfo.getName());
            map.put("id",cameraInfo.getId());
            map.put("attention",cameraInfo.getAttention());
            map.put("areaName",cameraInfo.getAreaName());


            cameraDetailsList.add(map);
        }


        return cameraDetailsList;
    }

    @Override
    public List findAttention(Integer farmId,Long userId) {
        List cameraDetailsList = new ArrayList();
        LambdaQueryWrapper<CameraInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CameraInfoEntity::getFarmId,farmId);
        List<CameraInfoEntity> cameraInfoEntities = baseMapper.selectList(wrapper);
        QueryWrapper<PublicNetworkEntity> urlWrapper = new QueryWrapper<PublicNetworkEntity>();

        List<CameraInfoEntity> cameraInfoEntities1 = addAttention(cameraInfoEntities, userId);
        List<CameraInfoEntity> collect = cameraInfoEntities1.stream()
                .filter(o -> o.getAttention() == 1)
                .collect(Collectors.toList());

        for (CameraInfoEntity cameraInfo : collect) {
            String rtsp ;
            Map map = new HashMap();
            urlWrapper.eq("farm_id", farmId);
            String url = publicNetworkDao.selectOne(urlWrapper).getUrl();
            urlWrapper.clear();

            String cameraAccount = cameraInfo.getAccount();
            String cameraPassword = cameraInfo.getPassword();
            String camerIp = cameraInfo.getIp();
            Integer areaId = cameraInfo.getAreaId();
            CameraBrandEntity cameraBrandEntity = cameraBrandDao.selectById(cameraInfo.getCameraBrandId());
            Boolean type = cameraBrandEntity.getType();
            if (type){
                rtsp = cameraBrandEntity.getMainStream();
            }else {
                rtsp = cameraBrandEntity.getAssistStream();
            }
            map.put("wsUrl", "ws://" + url + "/camera_relay?tcpaddr=" + cameraAccount + "%3A" + cameraPassword + "%40" + camerIp);
            map.put("rtspUrl", "rtsp://" + cameraAccount + ":" + cameraPassword + "@" + camerIp + rtsp);
            map.put("areaId", areaId);
            map.put("name",cameraInfo.getName());
            map.put("id",cameraInfo.getId());
            map.put("attention",cameraInfo.getAttention());
            map.put("areaName",cameraInfo.getAreaName());

            cameraDetailsList.add(map);
        }
        return  cameraDetailsList;
    }

    @Autowired
    private UserAttentionDao userAttentionDao;
    //是否关注
    public List<CameraInfoEntity> addAttention(List<CameraInfoEntity> cameraInfoEntities,Long userId){
        LambdaQueryWrapper<UserAttentionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserAttentionEntity::getUserId,userId);
        List<UserAttentionEntity> userAttentionEntities = userAttentionDao.selectList(wrapper);

        for (UserAttentionEntity userAttentionEntity : userAttentionEntities) {
            for (CameraInfoEntity cameraInfoEntity : cameraInfoEntities) {
                if (cameraInfoEntity.getId().equals(userAttentionEntity.getCameraId())){
                    cameraInfoEntity.setAttention(1);
                }
            }
        }
        return cameraInfoEntities;
    }


}