package com.persagy.adm.diagram.core.line;

import com.persagy.adm.diagram.core.DiagramBuilder;
import com.persagy.adm.diagram.core.model.ConnectPoint;
import com.persagy.adm.diagram.core.model.DiagramNode;
import com.persagy.adm.diagram.core.model.EquipmentNode;
import com.persagy.adm.diagram.core.model.Line;
import com.persagy.adm.diagram.core.model.base.XY;
import com.persagy.adm.diagram.core.model.template.MainPipe;

import java.util.ArrayList;
import java.util.List;

/**
 * 连线布局计算
 * @author zhaoyk
 * @date 2022-01-12
 */
public class LineLayoutManager {

	private DiagramBuilder diagramBuilder;

	private List<Block> blocks = new ArrayList<>();

	public LineLayoutManager(DiagramBuilder diagramBuilder) {
		this.diagramBuilder = diagramBuilder;
		initBlocks();
	}

	private void initBlocks(){
		for(Object o : diagramBuilder.getEquipMap().values()){
			if (o instanceof DiagramNode) {
				blocks.add(new Block((DiagramNode)o));
			}
		}
	}

	public void layout(Line line){
		ConnectPoint p1 = line.getFrom();
		ConnectPoint p2 = line.getTo();

		boolean exchange = false;
		//先计算点模型的终点
		if(p1.getEquipmentNode() == null) {
			if(p2.getEquipmentNode() == null) {
				throw new RuntimeException("do not support 2 main pipe connect");
			}

			ConnectPoint tmp = p2;
			p2 = p1;
			p1 = tmp;

			exchange = true;
		}

		LineEnd[] arr = buildLineEnd(p1, p2);

	}

	private LineEnd[] buildLineEnd(ConnectPoint p1, ConnectPoint p2) {
		LineEnd[] arr = new LineEnd[2];

		arr[0] = new LineEnd();
		arr[0].setPoint(anchorEmerge(p1));

		if(p2.getHostObj() instanceof EquipmentNode) {
			arr[1] = new LineEnd();
			arr[1].setPoint(anchorEmerge(p2));
		} else if (p2.getHostObj() instanceof MainPipe) {
			MainPipe mp = (MainPipe) p2.getHostObj();
			XY[] nearLine = null;
			double min = Double.MAX_VALUE;
			for(List<XY> list : mp.getLocationPath()) {
				XY pre = null;
				for(XY xy : list) {
					if(pre != null) {
						XY[] line = new XY[]{pre, xy};
						double distance = distance(line, p1.getLocation());
						if(distance < min){
							min = distance;
							nearLine = line;
						}
					}
					pre = xy;
				}
			}
			arr[1] = new LineEnd();
			arr[1].setLine(nearLine);
		}

		return arr;
	}

	/**
	 * 就算点和线段的最短距离
	 */
	private double distance(XY[] line, XY xy){
		//TODO
		return 0;
	}

	/**
	 * 锚点出线
	 */
	private XY anchorEmerge(ConnectPoint point){
		if(point.getLocation() == null) {
			point.layout();
		}

		XY xy = new XY(point.getLocation());
		switch (point.getAnchorCode().charAt(0)) {
			case 'T':
				xy.y -= LineEnd.offset;
				break;
			case 'B':
				xy.y += LineEnd.offset;
				break;
			case 'L':
				xy.x -= LineEnd.offset;
				break;
			case 'R':
				xy.x += LineEnd.offset;
				break;
			default:
		}
		return xy;
	}

}
