package com.baselet.element.facet.express;

import java.util.List;

import com.baselet.control.basics.geom.Rectangle;
import com.baselet.control.enums.Priority;
import com.baselet.diagram.draw.DrawHandler;
import com.baselet.element.facet.FirstRunKeyValueFacet;
import com.baselet.element.facet.PropertiesParserState;

/**
 * must be in first-run because it manipulates the left buffer which is used by second-run facets
 * must handle values in parsingFinished when drawer-setup is finished
 */
public class ExpressDataTypeFacet extends FirstRunKeyValueFacet {

	public static final ExpressDataTypeFacet INSTANCE = new ExpressDataTypeFacet();
	private static final Double LINE_RATE = 0.90;

	private ExpressDataTypeFacet() {}

	private static enum ClassTypeEnum {
		DATATYPE
	}

	private static final String[] ExpressModelProperties = {
			"width",
			"isFixed",
			"precision"
	};

	@Override
	public KeyValue getKeyValue() {
		return new KeyValue("type", new ValueInfo(ClassTypeEnum.DATATYPE, "make class active (double left/right border)"));
	}

	private static final int SPACING = 6;

	@Override
	public void handleValue(String value, PropertiesParserState state) {
		// only act if parsing is finished to make sure DrawHandler-Setup is finished
	}

	@Override
	public void parsingFinished(PropertiesParserState state, List<String> handledLines) {

		state.getBuffer().addToLeftAndRight(SPACING);
		DrawHandler drawer = state.getDrawer();
		drawer.drawRectangle(new Rectangle(0, 0, state.getGridElementSize().getWidth(), state.getGridElementSize().getHeight()));
		drawer.drawLine(state.getGridElementSize().getWidth() * LINE_RATE,
				0.0,
				state.getGridElementSize().getWidth() * LINE_RATE,
				1.0 * state.getGridElementSize().getHeight());
	}

	@Override
	public Priority getPriority() {
		return Priority.LOW; // must be after template class to work
	}

	public void draw(DrawHandler handler) {
		handler.drawRectangle(new Rectangle(0, 0, 100, 100));
	}

	@Override
	public boolean checkStart(String line, PropertiesParserState state) {

		if (super.checkStart(line, state)) {
			return true;
		}

		line = line.toLowerCase();
		for (String prefix : ExpressModelProperties) {
			prefix = prefix.toLowerCase();
			if (line.startsWith(prefix)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public void handleLine(String line, PropertiesParserState state) {
		// do nothing
	}

}
