#include "templatedao.h"
#include "../../data/sqltable.h"

bool TemplateDAO::create(Template &tpl)
{
    QSqlQuery query;
    query.prepare(
        "INSERT INTO TEMPLATE ("
        "NAME, DESCRIPTION, DOCUMENT_TYPE, WIDTH_MM, HEIGHT_MM, "
        "MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT, CONTENT, IS_DEFAULT, CREATEDBY"
        ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    );
    query.addBindValue(tpl.name);
    query.addBindValue(tpl.description);
    query.addBindValue(tpl.documentType);
    query.addBindValue(tpl.widthMM);
    query.addBindValue(tpl.heightMM);
    query.addBindValue(tpl.marginTop);
    query.addBindValue(tpl.marginBottom);
    query.addBindValue(tpl.marginLeft);
    query.addBindValue(tpl.marginRight);
    query.addBindValue(tpl.content);
    query.addBindValue(tpl.isDefault ? 1 : 0);
    query.addBindValue(tpl.createdBy);

    if (!query.exec()) {
        qWarning() << "TemplateDAO::create - Error:" << query.lastError().text();
        return false;
    }
    tpl.id = query.lastInsertId().toInt();
    return true;
}

QVector<Template> TemplateDAO::getAll()
{
    QVector<Template> result;
    QSqlQuery query("SELECT ID, NAME, DESCRIPTION, DOCUMENT_TYPE, WIDTH_MM, HEIGHT_MM, "
                    "MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT, CONTENT, IS_DEFAULT, CREATEDBY "
                    "FROM TEMPLATE ORDER BY NAME");
    while (query.next()) {
        Template tpl;
        tpl.id = query.value(0).toInt();
        tpl.name = query.value(1).toString();
        tpl.description = query.value(2).toString();
        tpl.documentType = query.value(3).toString();
        tpl.widthMM = query.value(4).toDouble();
        tpl.heightMM = query.value(5).toDouble();
        tpl.marginTop = query.value(6).toDouble();
        tpl.marginBottom = query.value(7).toDouble();
        tpl.marginLeft = query.value(8).toDouble();
        tpl.marginRight = query.value(9).toDouble();
        tpl.content = query.value(10).toString();
        tpl.isDefault = query.value(11).toBool();
        tpl.createdBy = query.value(12).toString();
        result.append(tpl);
    }
    return result;
}

Template TemplateDAO::getById(int id)
{
    Template tpl;
    QSqlQuery query;
    query.prepare("SELECT ID, NAME, DESCRIPTION, DOCUMENT_TYPE, WIDTH_MM, HEIGHT_MM, "
                  "MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT, CONTENT, IS_DEFAULT, CREATEDBY "
                  "FROM TEMPLATE WHERE ID = ?");
    query.addBindValue(id);
    if (query.exec() && query.next()) {
        tpl.id = query.value(0).toInt();
        tpl.name = query.value(1).toString();
        tpl.description = query.value(2).toString();
        tpl.documentType = query.value(3).toString();
        tpl.widthMM = query.value(4).toDouble();
        tpl.heightMM = query.value(5).toDouble();
        tpl.marginTop = query.value(6).toDouble();
        tpl.marginBottom = query.value(7).toDouble();
        tpl.marginLeft = query.value(8).toDouble();
        tpl.marginRight = query.value(9).toDouble();
        tpl.content = query.value(10).toString();
        tpl.isDefault = query.value(11).toBool();
        tpl.createdBy = query.value(12).toString();
    }
    return tpl;
}

bool TemplateDAO::update(const Template &tpl)
{
    QSqlQuery query;
    query.prepare(
        "UPDATE TEMPLATE SET "
        "NAME=?, DESCRIPTION=?, DOCUMENT_TYPE=?, WIDTH_MM=?, HEIGHT_MM=?, "
        "MARGIN_TOP=?, MARGIN_BOTTOM=?, MARGIN_LEFT=?, MARGIN_RIGHT=?, "
        "CONTENT=?, IS_DEFAULT=? "
        "WHERE ID=?"
    );
    query.addBindValue(tpl.name);
    query.addBindValue(tpl.description);
    query.addBindValue(tpl.documentType);
    query.addBindValue(tpl.widthMM);
    query.addBindValue(tpl.heightMM);
    query.addBindValue(tpl.marginTop);
    query.addBindValue(tpl.marginBottom);
    query.addBindValue(tpl.marginLeft);
    query.addBindValue(tpl.marginRight);
    query.addBindValue(tpl.content);
    query.addBindValue(tpl.isDefault ? 1 : 0);
    query.addBindValue(tpl.id);

    if (!query.exec()) {
        qWarning() << "TemplateDAO::update - Error:" << query.lastError().text();
        return false;
    }
    return true;
}

bool TemplateDAO::remove(int id)
{
    QSqlQuery query;
    query.prepare("DELETE FROM TEMPLATE WHERE ID = ?");
    query.addBindValue(id);
    if (!query.exec()) {
        qWarning() << "TemplateDAO::remove - Error:" << query.lastError().text();
        return false;
    }
    return true;
}

Template TemplateDAO::getDefaultForType(const QString &documentType)
{
    Template tpl;
    QSqlQuery query;
    query.prepare("SELECT ID, NAME, DESCRIPTION, DOCUMENT_TYPE, WIDTH_MM, HEIGHT_MM, "
                  "MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT, CONTENT, IS_DEFAULT, CREATEDBY "
                  "FROM TEMPLATE WHERE DOCUMENT_TYPE = ? AND IS_DEFAULT = 1 LIMIT 1");
    query.addBindValue(documentType);
    if (query.exec() && query.next()) {
        tpl.id = query.value(0).toInt();
        tpl.name = query.value(1).toString();
        tpl.description = query.value(2).toString();
        tpl.documentType = query.value(3).toString();
        tpl.widthMM = query.value(4).toDouble();
        tpl.heightMM = query.value(5).toDouble();
        tpl.marginTop = query.value(6).toDouble();
        tpl.marginBottom = query.value(7).toDouble();
        tpl.marginLeft = query.value(8).toDouble();
        tpl.marginRight = query.value(9).toDouble();
        tpl.content = query.value(10).toString();
        tpl.isDefault = query.value(11).toBool();
        tpl.createdBy = query.value(12).toString();
    }
    return tpl;
}