/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.reporting.pdf;

import org.jdom2.Element;
import org.jdom2.Namespace;

/**
 * Utility class for creating PDF diagrams
 * 
 * @author Matthias Betz
 *
 */
public class PdfUtils {

	private static final String HEIGHT_STRING = "height";

	private static final String WIDTH_STRING = "width";

	private static final Namespace svgNs = Namespace.getNamespace("svg", "http://www.w3.org/2000/svg");

	private static final int WIDTH = 450;

	private PdfUtils() {
		// only static use
	}

	public static Element createBar(int val1, int val2) {
		int max = val1 + val2;
		double val1Percentage = val1 / (double) max;
		int val1Length = (int) (val1Percentage * WIDTH);
		int val2Length = WIDTH - val1Length;
		String val1LengthString = "" + val1Length;
		String val2LengthString = "" + val2Length;

		Element svgElement = new Element("svg", svgNs);
		svgElement.setAttribute(WIDTH_STRING, "450px");
		svgElement.setAttribute(HEIGHT_STRING, "30px");

		if (val1 > 0) {
			Element gVal1Style = new Element("g", svgNs);
			svgElement.addContent(gVal1Style);
			gVal1Style.setAttribute("style", "fill:red");

			Element val1Rect = new Element("rect", svgNs);
			gVal1Style.addContent(val1Rect);
			val1Rect.setAttribute("x", "0");
			val1Rect.setAttribute("y", "0");
			val1Rect.setAttribute(WIDTH_STRING, val1LengthString);
			val1Rect.setAttribute(HEIGHT_STRING, "30");
			Element val1Text = new Element("text", svgNs);
			svgElement.addContent(val1Text);
			val1Text.setAttribute("x", "5");
			val1Text.setAttribute("y", "20");
			val1Text.setAttribute("font-size", "18pt");
			val1Text.addContent("" + val1);
		}

		if (val2 > 0) {
			Element gVal2Style = new Element("g", svgNs);
			svgElement.addContent(gVal2Style);
			gVal2Style.setAttribute("style", "fill:green");
			Element val2Rect = new Element("rect", svgNs);
			gVal2Style.addContent(val2Rect);
			val2Rect.setAttribute("x", val1LengthString);
			val2Rect.setAttribute("y", "0");
			val2Rect.setAttribute(WIDTH_STRING, val2LengthString);
			val2Rect.setAttribute(HEIGHT_STRING, "30");
			Element val2Text = new Element("text", svgNs);
			svgElement.addContent(val2Text);
			String val2String = "" + val2;
			val2Text.setAttribute("x", "" + (WIDTH - val2String.length() * 10));
			val2Text.setAttribute("y", "20");
			val2Text.setAttribute("font-size", "18pt");
			
			val2Text.addContent(val2String);
		}

		return svgElement;
	}

}
