package com.lutao.cloud.oil.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.oil.mapper.VideoEquipmentInfoMapper;
import com.lutao.cloud.oil.pojo.entity.VideoInfo;
import com.lutao.cloud.oil.pojo.vo.*;
import com.lutao.cloud.oil.service.VideoEquipmentInfoService;
import com.lutao.cloud.oil.service.VideoInfoService;
import com.lutao.entity.VideoEquipmentInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class VideoEquipmentInfoServiceImpl extends ServiceImpl<VideoEquipmentInfoMapper, VideoEquipmentInfo> implements VideoEquipmentInfoService {

    @Resource
    private VideoInfoService videoInfoService;

    @Override
    public MessageInfo<List<VideoEquipmentInfoTreeVO>> queryVideoEquipmentInfoTree() {

        List<VideoEquipmentInfoTreeVO> videoEquipmentInfoTreeVOS = new ArrayList<>();

        List<VideoEquipmentInfoBaseVO> videoEquipmentInfoBaseVOS = this.baseMapper.queryVideoEquipmentInfoTree();
        //构造树
        videoEquipmentInfoBaseVOS.stream().collect(Collectors.groupingBy(VideoEquipmentInfoBaseVO::getAppName)).forEach((k, v) -> {
            List<PlatformVideoVO> platformVideoVOS = new ArrayList<>();
            VideoEquipmentInfoTreeVO videoEquipmentInfoTreeVO = new VideoEquipmentInfoTreeVO();
            videoEquipmentInfoTreeVO.setAppName(k);


            v.stream().filter(item -> item.getPlatformName() != null).collect(Collectors.groupingBy(VideoEquipmentInfoBaseVO::getPlatformName)).forEach((k1, v1) -> {
                List<CameraVO> cameraNames = new ArrayList<>();
                PlatformVideoVO platformVideoVO = new PlatformVideoVO();
                platformVideoVO.setPlatformName(k1);
                v1.forEach(videoEquipmentInfoBaseVO -> {
                    CameraVO cameraVO = new CameraVO();
                    cameraVO.setId(videoEquipmentInfoBaseVO.getId());
                    cameraVO.setCameraName(videoEquipmentInfoBaseVO.getCameraName());
                    cameraVO.setCameraIndex(videoEquipmentInfoBaseVO.getCameraIndex());
                    cameraVO.setOnlineStatus(videoEquipmentInfoBaseVO.getOnlineStatus());
                    cameraNames.add(cameraVO);
                });
                platformVideoVO.setCameraNames(cameraNames);
                platformVideoVOS.add(platformVideoVO);
            });

            videoEquipmentInfoTreeVO.setPlatformVideoVOS(platformVideoVOS);
            videoEquipmentInfoTreeVOS.add(videoEquipmentInfoTreeVO);
        });

        return MessageInfo.ok(videoEquipmentInfoTreeVOS);
    }

    @Override
    public MessageInfo<List<VideoEquipmentInfoTreeVO>> queryVideoEquipmentInfoTreeV2() {
        List<VideoEquipmentInfoBaseVO> videoEquipmentInfoBaseVOS = this.baseMapper.queryVideoEquipmentInfoTree();
        return null;
    }

    @Override
    public MessageInfo<List<VideoInfoNode>> queryVideoEquipmentInfoTreeNew() {

        //所有视频信息
        List<VideoInfo> videoInfos = videoInfoService.list();

        Map<String, VideoInfoNode> nodeMap = new HashMap<>();
        List<VideoInfoNode> rootNodes = new ArrayList<>();
        Map<String, List<VideoInfo>> cameraMap = new HashMap<>();

        // 创建所有的节点并存入 map
        for (VideoInfo videoInfo : videoInfos) {
            if (videoInfo.getIndexCode() != null) {
                VideoInfoNode node = new VideoInfoNode();
                node.setVideoInfo(videoInfo);
                node.setChildren(new ArrayList<>());
                nodeMap.put(videoInfo.getIndexCode(), node);
            } else if (videoInfo.getCameraIndexCode() != null) {
                String parentIndexCode = videoInfo.getParentIndexCode();
                if (!cameraMap.containsKey(parentIndexCode)) {
                    cameraMap.put(parentIndexCode, new ArrayList<>());
                }
                cameraMap.get(parentIndexCode).add(videoInfo);
            }
        }

        // 将视频信息添加到对应的平台节点下
        for (Map.Entry<String, List<VideoInfo>> entry : cameraMap.entrySet()) {
            String parentIndexCode = entry.getKey();
            List<VideoInfo> cameras = entry.getValue();
            VideoInfoNode parentNode = nodeMap.get(parentIndexCode);
            if (parentNode != null) {
                for (VideoInfo camera : cameras) {
                    VideoInfoNode cameraNode = new VideoInfoNode();
                    cameraNode.setVideoInfo(camera);
                    parentNode.getChildren().add(cameraNode);
                }
            }
        }

        // 构建树形结构
        for (VideoInfoNode node : nodeMap.values()) {
            VideoInfoNode parentNode = nodeMap.get(node.getVideoInfo().getParentIndexCode());
            if (parentNode == null) {
                rootNodes.add(node);
            } else {
                parentNode.getChildren().add(node);
            }
        }

        return MessageInfo.ok(rootNodes);
    }
}
