package com.persagy.adm.diagram.core.model.template;

import com.persagy.adm.diagram.core.model.base.Container;
import com.persagy.adm.diagram.core.model.base.XY;

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

/**
 * 容器周边参考点
 * @author zhaoyk
 */
public class ContainerRefPoint {

	public static final String TL = "TL";

	public static final String TR = "TR";

	public static final String BR = "BR";

	public static final String BL = "BL";

	private String containerId;

	private String name;

	private XY location;

	public ContainerRefPoint(String containerId, String name) {
		this.containerId = containerId;
		this.name = name;
	}

	public String getContainerId() {
		return containerId;
	}

	public String getName() {
		return name;
	}

	public XY getLocation() {
		return location;
	}

	public void setLocation(XY location) {
		this.location = location;
	}

	public static List<ContainerRefPoint> getRefPoints(Container container, XY conLocation) {
		ArrayList<ContainerRefPoint> points = new ArrayList<>(16);
		XY size = container.getSize();

		addPoint(container.getId(), TL, new XY(conLocation), points);
		addPoint(container.getId(), TR, new XY(conLocation.x + size.x, conLocation.y), points);
		addPoint(container.getId(), BR, new XY(conLocation.x + size.x, conLocation.y + size.y), points);
		addPoint(container.getId(), BL, new XY(conLocation.x, conLocation.y + size.y), points);

		int len = size.x / 4;
		for (int i = 1; i <= 3; i++) {
			addPoint(container.getId(), "T" + i, new XY(conLocation.x + len * i, conLocation.y), points);
			addPoint(container.getId(), "B" + i, new XY(conLocation.x + len * i, conLocation.y + size.y), points);
		}
		len = size.y / 4;
		for (int i = 1; i <= 3; i++) {
			addPoint(container.getId(), "L" + i, new XY(conLocation.x, conLocation.y + len * i), points);
			addPoint(container.getId(), "R" + i, new XY(conLocation.x + size.x, conLocation.y + len * i), points);
		}

		return points;
	}

	private static void addPoint(String containerId, String name, XY location, ArrayList<ContainerRefPoint> points){
		ContainerRefPoint refPoint = new ContainerRefPoint(containerId, name);
		refPoint.setLocation(location);
		points.add(refPoint);
	}

	public static ContainerRefPoint getRefPoint(Container container, String name, XY conLocation) {
		List<ContainerRefPoint> list = getRefPoints(container, conLocation);
		for(ContainerRefPoint p : list) {
			if(p.getName().equalsIgnoreCase(name)) {
				return p;
			}
		}
		return null;
	}

}
