/*-
 *  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;

/**
 * Insert a code block into the PDF report. It will have a border and the
 * content has a different font. Supports colored test elements.
 * 
 * @author Matthias Betz
 *
 */
public class CodeBlock {

	private static final String BLOCK = "block";

	private Element codeBlockContainer;
	private Element codeBlockElement;

	private Element currentLine;

	public CodeBlock() {
		codeBlockContainer = new Element("block-container", PdfReport.FO_NS);
		codeBlockContainer.setAttribute("width", "97%");
		codeBlockContainer.setAttribute("margin-left", "9");
		codeBlockContainer.setAttribute("margin-bottom", "5px");

		codeBlockElement = new Element(BLOCK, PdfReport.FO_NS);
		codeBlockElement.setAttribute("padding-after", "10");
		codeBlockElement.setAttribute("font-family", "Courier");
		codeBlockElement.setAttribute("font-size", "10pt");
		codeBlockElement.setAttribute("border-style", "solid");
		codeBlockElement.setAttribute("padding", "5px");
		codeBlockContainer.addContent(codeBlockElement);

		currentLine = new Element(BLOCK, PdfReport.FO_NS);
		codeBlockElement.addContent(currentLine);
	}

	/**
	 * Adds a text with the specified color to the block
	 * 
	 * @param text  the text that is added
	 * @param color the color of the text in HTML code, e.g. #ffffff
	 */
	public void addText(String text, String color) {
		Element inline = new Element("inline", PdfReport.FO_NS);
		currentLine.addContent(inline);
		inline.addContent(text);
		inline.setAttribute("color", color);
	}

	/**
	 * Adds a black colored text to the block.
	 * 
	 * @param text the text that is added
	 */
	public void addText(String text) {
		currentLine.addContent(text);
	}

	/**
	 * Create a new line in the block. The page can break after this.
	 */
	public void newLine() {
		newLine(0);
	}

	/**
	 * Add a new line with a distance from the left border for the next line.
	 * 
	 * @param marginLeft the distance from the left in pixel
	 */
	public void newLine(int marginLeft) {
		currentLine = new Element(BLOCK, PdfReport.FO_NS);
		codeBlockElement.addContent(currentLine);
		if (marginLeft != 0) {
			currentLine.setAttribute("margin-left", "" + marginLeft);
		}
	}

	Element getCodeBlock() {
		return codeBlockContainer;
	}

}
