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

/**
 * Construct a table for the pdf report.
 * 
 * @author Matthias Betz
 *
 */
public class Table {

	private static final String TABLE_HEADER_COLOR = "#58ACFA";

	private Element tableBlock;

	private Element tableHeader;
	private Element tableBody;
	private Element table;

	private int columns;

	public Table(int columns) {
		this.columns = columns;
		tableBlock = new Element("block", PdfReport.FO_NS);
		tableBlock.setAttribute("padding-after", "10");
		tableBlock.setAttribute("keep-together", "always");

		table = new Element("table", PdfReport.FO_NS);
		tableBlock.addContent(table);
		PdfReport.applySolidBorder(table);
		table.setAttribute("table-layout", "fixed");
		table.setAttribute("width", "100%");

		tableHeader = new Element("table-header", PdfReport.FO_NS);
		table.addContent(tableHeader);
		tableHeader.setAttribute("background-color", TABLE_HEADER_COLOR);

		tableBody = new Element("table-body", PdfReport.FO_NS);
		table.addContent(tableBody);
	}

	public void setTableColumnWidth(int... width) {
		int sum = 0;
		for (int w : width) {
			sum += w;
		}
		if (sum != 100 || width.length != columns) {
			throw new IllegalArgumentException("column width must result in a sum of 100");
		}
		for (int i = 1; i <= width.length; i++) {
			int w = width[i - 1];
			Element tableColumn = new Element("table-column", PdfReport.FO_NS);
			table.addContent(i - 1, tableColumn);
			tableColumn.setAttribute("column-number", "" + i);
			tableColumn.setAttribute("column-width", w + "%");
		}
	}

	public void setTitle(String... content) {
		if (content.length != columns) {
			throw new IllegalArgumentException(
					"Column length is: " + columns + " but only " + content.length + " values given");
		}
		if (tableHeader.getContentSize() > 0) {
			tableHeader.removeContent();
		}
		tableHeader.addContent(createTableRow(content));
	}

	public void addRow(String... content) {
		if (content.length != columns) {
			throw new IllegalArgumentException(
					"Column length is: " + columns + " but only " + content.length + " values given");
		}
		tableBody.addContent(createTableRow(content));
	}

	Element getTableBlock() {
		return tableBlock;
	}

	private Element createTableRow(String... content) {
		Element tableRow = new Element("table-row", PdfReport.FO_NS);

		for (int i = 0; i < content.length; i++) {
			Element tableCell = new Element("table-cell", PdfReport.FO_NS);
			tableRow.addContent(tableCell);
			tableCell.addContent(PdfReport.createTextElement(content[i]));
			tableCell.setAttribute("padding", "3pt 3pt 3pt 3pt");
			PdfReport.applySolidBorder(tableCell);
		}
		return tableRow;
	}

}
