package com.zcloud.service.electronicFence.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.electronicFence.ElectronicFenceMapper;
import com.zcloud.service.electronicFence.ElectronicFenceService;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 说明：TODO
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional
public class ElectronicFenceServiceImpl implements ElectronicFenceService {
    @Autowired
    private ElectronicFenceMapper electronicFenceMapper;

    @Override
    public List<PageData> list(Page page) {
        return electronicFenceMapper.datalistPage(page);
    }

    @Override
    public void save(PageData pd) {
        electronicFenceMapper.save(pd);
    }

    @Override
    public void edit(PageData pd) {
        electronicFenceMapper.edit(pd);
    }

    @Override
    public void removeByIds(PageData pd) {
        electronicFenceMapper.deleteAll(pd);
    }

    @Override
    public PageData findById(PageData pd) {
        return electronicFenceMapper.findById(pd);
    }

    @Override
    public List<Map<String, Object>> listTree(PageData pd) {
        if ("children".equals(pd.getString("childKey"))) {
            // 手机端
            List<Map<String, Object>> dataList = electronicFenceMapper.listTreeApp(pd);
            return buildTreeApp(dataList);
        }
        List<Map<String, Object>> dataList = electronicFenceMapper.listTree(pd);
        return buildTree(dataList);
    }

    @Override
    public boolean hasChild(PageData pd) {
        return electronicFenceMapper.findByPId(pd).size() > 0;
    }

    public static List<Map<String, Object>> buildTree(List<Map<String, Object>> dataList) {
        Map<String, Map<String, Object>> idToNodeMap = new HashMap<>();
        List<Map<String, Object>> rootNodes = new ArrayList<>();

        for (Map<String, Object> data : dataList) {
            String id = (String) data.get("id");
            idToNodeMap.put(id, data);
        }

        for (Map.Entry<String, Map<String, Object>> entry : idToNodeMap.entrySet()) {
            Map<String, Object> node = entry.getValue();
            String pId = (String) node.get("pId");
            String ELECTRONIC_FENCE_ID = (String) node.get("ELECTRONIC_FENCE_ID");
            if (Tools.isEmpty(ELECTRONIC_FENCE_ID)) {
                node.put("disabled", true);
            }
            if ("0".equals(pId)) {
                node.put("disabled", true);
                rootNodes.add(node);
            } else {
                Map<String, Object> parentNode = idToNodeMap.get(pId);
                if (parentNode != null) {
                    List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.getOrDefault("nodes", new ArrayList<>());
                    children.add(node);
                    parentNode.put("nodes", children);
                }
            }
        }

        return rootNodes;
    }

    public static List<Map<String, Object>> buildTreeApp(List<Map<String, Object>> dataList) {
        Map<String, Map<String, Object>> idToNodeMap = new HashMap<>();
        List<Map<String, Object>> rootNodes = new ArrayList<>();

        for (Map<String, Object> data : dataList) {
            String id = (String) data.get("ELECTRONIC_FENCE_AREA_ID");
            idToNodeMap.put(id, data);
        }

        for (Map.Entry<String, Map<String, Object>> entry : idToNodeMap.entrySet()) {
            Map<String, Object> node = entry.getValue();
            String pId = (String) node.get("pId");
            String ELECTRONIC_FENCE_ID = (String) node.get("ELECTRONIC_FENCE_ID");
            if (Tools.isEmpty(ELECTRONIC_FENCE_ID)) {
                node.put("disabled", true);
            }
            if ("0".equals(pId)) {
                node.put("disabled", true);
                rootNodes.add(node);
            } else {
                Map<String, Object> parentNode = idToNodeMap.get(pId);
                if (parentNode != null) {
                    List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.getOrDefault("children", new ArrayList<>());
                    children.add(node);
                    parentNode.put("children", children);
                }
            }
        }

        return rootNodes;
    }
}
