#include <QTextDocument>
#include <QTextDocumentFragment>
#include <QTextCursor>
#include <QTextBlock>
#include <QTextTable>
#include "export_specification.h"
#include "core.h"
#include "designer_view.h"
#include "pibase.h"


QByteArray exportSpecification(SHDesignerView * blockView) {
	QTextDocument doc;
	QTextCursor tc(&doc);
	QTextCharFormat tcf = tc.charFormat();
	tcf.setFontFamily("Times New Roman");
	tcf.setFontPointSize(12);
	tc.setCharFormat(tcf);
	tc.insertText(QString("Project \"%1\" specification").arg(SHCORE->project_properties.propertyValueByName("name").toString()));
	tc.insertText(QString("\n\n"));
	tc.insertText(QString("Devices:"));
	QTextTable * tt = tc.insertTable(1, 5);
	QTextTableFormat tf = tt->format();
	tf.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
	tf.setCellPadding(0.);
	tf.setCellSpacing(0.);
	tt->setFormat(tf);
	tt->setFrameFormat(tf);
	tcf.setFontWeight(75);
	tc = tt->cellAt(0, 0).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::fromUtf8("№"));
	tc = tt->cellAt(0, 1).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Name");
	tc = tt->cellAt(0, 2).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Vendor");
	tc = tt->cellAt(0, 3).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Description");
	tc = tt->cellAt(0, 4).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Count");
	QMap<int, int> bids;
	QList<BlockItem * > bll = blockView->blocks();
	foreach (BlockItem * b, bll)
		bids[b->data(SH_Block_ID).toInt()]++;
	QMapIterator<int, int> it(bids);
	tcf.setFontWeight(50);
	while (it.hasNext()) {
		it.next();
		const BlockType & bt(SHCORE->databaseStorage().block_types[it.key()]);
		tt->appendRows(1);
		int row = tt->rows() - 1;
		tc = tt->cellAt(row, 0).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::number(row));
		tc = tt->cellAt(row, 1).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(bt.name);
		tc = tt->cellAt(row, 2).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(bt.vendor_name);
		tc = tt->cellAt(row, 3).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(bt.description);
		tc = tt->cellAt(row, 4).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::number(it.value()));
	}
	tc.movePosition(QTextCursor::End);
	tc.setCharFormat(tcf);
	tc.insertText(QString("\n\n"));
	tc.insertText(QString("Cables:"));
	tt = tc.insertTable(1, 5);
	tf = tt->format();
	tf.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
	tf.setCellPadding(0.);
	tf.setCellSpacing(0.);
	tt->setFormat(tf);
	tt->setFrameFormat(tf);
	tcf.setFontWeight(75);
	tc = tt->cellAt(0, 0).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::fromUtf8("№"));
	tc = tt->cellAt(0, 1).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Name");
	tc = tt->cellAt(0, 2).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Description");
	tc = tt->cellAt(0, 3).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Count");
	tc = tt->cellAt(0, 4).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText("Overall length, m");
	QList<BlockBusItem * > bsl = blockView->buses();
	QMap<int, double> lmap;
	bids.clear();
	foreach (BlockBusItem * b, bsl) {
		bids[b->busType()]++;
		double cl = 0.;
		if (b->isPropertyExists("length")) cl += b->propertyValueByName("length").toDouble();
		if (b->isPropertyExists("Length")) cl += b->propertyValueByName("Length").toDouble();
		lmap[b->busType()] += cl;
	}
	it = QMapIterator<int, int>(bids);
	tcf.setFontWeight(50);
	while (it.hasNext()) {
		it.next();
		const BusType & bt(SHCORE->databaseStorage().bus_types[it.key()]);
		tt->appendRows(1);
		int row = tt->rows() - 1;
		tc = tt->cellAt(row, 0).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::number(row));
		tc = tt->cellAt(row, 1).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(bt.name);
		tc = tt->cellAt(row, 2).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(bt.description);
		tc = tt->cellAt(row, 3).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::number(it.value()));
		tc = tt->cellAt(row, 4).firstCursorPosition(); tc.setCharFormat(tcf); tc.insertText(QString::number(lmap.value(bt.id, 0.)));
	}
	return doc.toHtml(
#if QT_VERSION_MAJOR <= 5
					 "utf-8"
#endif
							).toUtf8();
}
