package com.fdkankan.model.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.model.bean.PointBean;
import com.fdkankan.model.bean.SegmentBean;
import com.fdkankan.model.bean.VertexBean;
import com.fdkankan.model.bean.WallBean;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class FloorPlanUserUtil {

	public static void main(String[] args) {
		JSONObject floorPlanUserJson = FloorPlanUserUtil.createFloorPlanUserJson("D:\\test\\新建文件夹\\floorplan_cad.json");
		FileUtil.writeUtf8String(floorPlanUserJson.toJSONString(), "D:\\test\\新建文件夹\\floorplan.json");

	}

	/**
	 * 根据floorplan_cad.json文件生成houseType.json
	 * @param filePath
	 * @return
	 */
	public static JSONObject createFloorPlanUserJson(String filePath) {

		JSONArray floors = readFloorJson(filePath);
		if(CollUtil.isEmpty(floors)){
			return null;
		}

		JSONObject house = new JSONObject();
		house.put("unit", "m");
		house.put("angle", 0);
		house.put("type", "cad");
		house.put("version", "v4.0");

		JSONArray targetFloors = new JSONArray();
		house.put("floors", targetFloors);

		int currentId = 0;

		for(int i = 0; i < floors.size(); i++){
			JSONObject floor = (JSONObject)floors.get(i);
			Map<String, Object> result = createFloorHandler(floor, currentId);
			JSONArray points = (JSONArray)result.get("pointArr");
			JSONArray walls = (JSONArray)result.get("wallArr");
			currentId = (int)result.get("currentId");
			JSONObject targetFloor = new JSONObject();
			targetFloor.put("points", convertArrToMap(points));
			targetFloor.put("walls", convertArrToMap(walls));
			targetFloor.put("id", floor.getInteger("id"));
			targetFloor.put("name",floor.getString("name"));
			targetFloor.put("subgroup", floor.getInteger("subgroup"));
			targetFloors.add(targetFloor);
		}

		house.put("currentId", currentId);

		return house;
	}

	private static Map<String, JSONObject> convertArrToMap(JSONArray arr){
		Map<String, JSONObject> result = new HashMap<>();
		if(CollUtil.isEmpty(arr)){
			return result;
		}

		arr.stream().forEach(item->{
			JSONObject object = (JSONObject)item;
			String vectorId = object.getString("vectorId");
			result.put(vectorId, object);
		});

		return result;
	}

	private static Map<String, Object> createFloorHandler(JSONObject floor, int currentId){

		Map<String, Object> result = new HashMap<>();
		//处理点
		Map<Integer, VertexBean> vertexMap = new HashMap<>();
		Map<String, PointBean> pointMap = new HashMap<>();
		Map<Integer, String> vpMap = new HashMap<>();
		JSONArray vertexArr = floor.getJSONArray("vertex-xy");
		for(int i = 0; i < vertexArr.size(); i++){
			Object o = vertexArr.get(i);

			VertexBean vertexBean = JSON.parseObject(JSON.toJSONString(o), VertexBean.class);
			Integer vertexId = vertexBean.getId();
			vertexMap.put(vertexId, vertexBean);

			String pointId = "Point" + currentId;
			pointMap.put(pointId, PointBean.builder().geoType("Point").vectorId(pointId).x(vertexBean.getX()).y(vertexBean.getY()).build());
			++currentId;

			vpMap.put(vertexId, pointId);
		}

		//处理墙
		Map<Integer, SegmentBean> segmentMap = new HashMap<>();
		Map<String, WallBean> wallMap = new HashMap<>();
		Map<Integer, String> swMap = new HashMap<>();
		JSONArray segmentArr = floor.getJSONArray("segment");
		Map<String, String> startMap = new HashMap<>();
		Map<String, String> endMap = new HashMap<>();
		for(int i = 0; i < segmentArr.size(); i++){
			Object o = segmentArr.get(i);

			SegmentBean segmentBean = JSON.parseObject(JSON.toJSONString(o), SegmentBean.class);
			String startPointId = vpMap.get(segmentBean.getA());
			String endPointId = vpMap.get(segmentBean.getB());
			segmentBean.setStartPointId(startPointId);
			segmentBean.setEndPointId(endPointId);

			Integer segmentId = segmentBean.getId();
			segmentMap.put(segmentId, segmentBean);

			String wallId = "Wall" + currentId;
			WallBean wallBean = WallBean.builder()
				.geoType("Wall")
				.vectorId(wallId)
				.start(segmentBean.getStartPointId())
				.end(segmentBean.getEndPointId())
				.children(new String[]{})
				.width(0.2d)
				.build();
			wallMap.put(wallId, wallBean);
			++currentId;

			startMap.put(wallBean.getStart(), wallBean.getVectorId());
			endMap.put(wallBean.getEnd(), wallBean.getVectorId());

			swMap.put(segmentId, wallId);
		}

		Collection<PointBean> pointBeans = pointMap.values();
		for (PointBean pointBean : pointBeans) {
			Map<String, String> parent = new HashMap<>();
			String startParent = startMap.get(pointBean.getVectorId());
			String endParent = endMap.get(pointBean.getVectorId());
			if(StrUtil.isNotEmpty(startParent)){
				parent.put(startParent, "start");
			}
			if(StrUtil.isNotEmpty(endParent)){
				parent.put(endParent, "end");
			}
			pointBean.setParent(parent);
		}
		JSONArray pointArr = JSON.parseArray(JSON.toJSONString(pointBeans));
		result.put("pointArr", pointArr);

		Collection<WallBean> wallBeans = wallMap.values();
		JSONArray wallArr = JSON.parseArray(JSON.toJSONString(wallBeans));
		result.put("wallArr", wallArr);

		result.put("currentId", currentId);

		return result;

	}

	private static JSONArray readFloorJson(String filePath) {
		try {
			JSONObject floorplan = FileUtils.readJson(filePath);
			JSONArray floors = floorplan.getJSONArray("floors");
			return floors;
		} catch (IOException e) {

			return null;
		}
	}

}








