/* Backend item
*/

#include "backenditem.h"

#include <QPen>
#include <QRect>
#include <QBrush>
#include <QPoint>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
#include <QString>
#include <QPainterPath>
#include <QColor>

#include "objectitem.h"

BackendItem::BackendItem(const QString &xmlStructure)
    : QGraphicsItem(),
      m_xmlStructure(xmlStructure)
{
    updateStructure(xmlStructure);
}

void BackendItem::updateStructure(const QString &xmlStructure)
{
    m_xmlStructure = xmlStructure;

    QDomDocument doc;
    doc.setContent(m_xmlStructure);
    QDomElement backendElement = doc.documentElement();
    QDomNodeList nodelist = backendElement.elementsByTagName("Definitions");
    QDomElement e = nodelist.at(0).toElement();
    QDomNode n = e.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(e.tagName() == "MediaObject") {
            if(!e.isNull()) {
                ObjectItem *o = new ObjectItem(this, e);
                o->setPos(20,50); // the node really is an element.
            }
        }
        n = n.nextSibling();
    }

    //update(boundingRect());
}

QString BackendItem::name() const
{
    QDomDocument doc;
    doc.setContent(m_xmlStructure);
    QDomElement backendElement = doc.documentElement();
    QDomNodeList nodelist = backendElement.elementsByTagName("Backend");
    QDomElement e = nodelist.at(0).toElement();

    return e.attribute("Name");
}

QRectF BackendItem::boundingRect() const
{
    if(children().count() > 0) {
        QRectF c = childrenBoundingRect();
        c.setLeft(c.left() - 20);
        c.setTop(c.top() - 50);
        c.setRight(c.right() + 20);
        c.setBottom(c.bottom() + 20);
        return c;
    }
    else
        return QRectF(0,0,300,100);
}

void BackendItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)

    QRect r = boundingRect().toRect();

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);

    int rad = 20;

    // Draw the title bar
    QPainterPath titlePath;
    titlePath.moveTo(r.width(), r.top() + rad);
    titlePath.arcTo(r.width() - rad, r.top(), rad, rad, 0.0, 90.0);
    titlePath.lineTo(r.left() + rad, r.top());
    titlePath.arcTo(r.left(), r.top(), rad, rad, 90.0, 90.0);
    titlePath.lineTo(r.left(), 20);
    titlePath.lineTo(r.width(), 20);
    titlePath.closeSubpath();

    QPen pen(QBrush(qRgb(126,140,169)), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter->setPen(pen);
    painter->setBrush(QBrush(qRgb(126,140,169)));
    painter->drawPath(titlePath);

    // Draw the border
    QPainterPath borderPath;
    borderPath.moveTo(r.width(), r.top() + rad);
    borderPath.arcTo(r.width() - rad, r.top(), rad, rad, 0.0, 90.0);
    borderPath.lineTo(r.left() + rad, r.top());
    borderPath.arcTo(r.left(), r.top(), rad, rad, 90.0, 90.0);
    borderPath.lineTo(r.left(), r.height() - 2 * rad);
    borderPath.arcTo(r.left(), r.height() - rad, rad, rad, 180.0, 90.0);
    borderPath.lineTo(r.width() - 2 * rad, r.height());
    borderPath.arcTo(r.width() - rad, r.height() - rad, rad, rad, 270.0, 90.0);
    borderPath.closeSubpath();

    QPen pen2(QBrush(qRgb(50,50,50)), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter->setPen(pen2);
    painter->setBrush(QBrush(QColor(250,250,250,30)));
    painter->drawPath(borderPath);

    // Draw the title of the backend

    QDomDocument doc;
    doc.setContent(m_xmlStructure);
    QDomElement backendElement = doc.documentElement();
    QDomNodeList nodelist = backendElement.elementsByTagName("Backend");
    QDomElement e = nodelist.at(0).toElement();

    QString pid = e.attribute("PID");
    QString name = e.attribute("Name");
    QString prog = e.attribute("ParentProgram");
    
    QPen txtPen(Qt::black);
    QFont txtFont("Arial", 8);

    painter->setPen(txtPen);
    painter->setFont(txtFont);
    painter->drawText(QPoint(5,15), name + " by " + prog + " [" + pid + "]");

    if(children().count() < 1) {
        painter->drawPixmap(QPoint(25, (boundingRect().height() / 2)), QPixmap(":/icons/warning.png"));
        painter->drawText(QPoint(45,(boundingRect().height() / 2) + 10), "This backend doesn't have any structure!");
        painter->drawText(QPoint(45,(boundingRect().height() / 2) + 25), "It's probably being used for information only.");
    }

    painter->restore();
}
