package com.fdkankan.ucenter.util;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.ucenter.common.constants.ConstantFilePath;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class RoamingPointUtil {

    public static String createRoamingPoint(String num, String visionTxtPath){

        if(!FileUtil.exist(visionTxtPath)){
            return null;
        }
        JSONObject visionJson = JSON.parseObject(FileUtil.readUtf8String(visionTxtPath));

        JSONArray sweepArray = visionJson.getJSONArray("sweepLocations");
        List<JSONObject> roamingPoint = new ArrayList<>();
        for (int i = 0; i < sweepArray.size(); i++) {
            JSONObject objectJson = sweepArray.getJSONObject(i);
            JSONObject newJson = new JSONObject();
            newJson.put("uuid", objectJson.getString("uuid"));
            JSONObject puck = objectJson.getJSONObject("puck");
            puck.put("y", puck.getDoubleValue("y"));
            puck.remove("z");
            newJson.put("puck", puck);
            newJson.put("subgroup", objectJson.getString("subgroup"));
            roamingPoint.add(newJson);
        }
        Map<String, List<JSONObject>> subgroup = roamingPoint.stream().collect(
                Collectors.groupingBy(x -> x.getString("subgroup"))).entrySet().stream()
                .collect(Collectors.toMap(e -> "floor" + e.getKey(), Map.Entry::getValue));
        return JSON.toJSONString(subgroup);
    }

}
