//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Job/BatchInfo.cpp
//! @brief     Implements class BatchInfo.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Job/BatchInfo.h"
#include "GUI/Model/Job/JobStatus.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString BeginTime("Begin");
const QString Comments("Comments");
const QString Duration("Duration");
const QString EndTime("End");
const QString Identifier("Id");
const QString Name("Name");
const QString Progress("Progress");
const QString Status("Status");

} // namespace Tag
} // namespace

BatchInfo::BatchInfo()
    : m_status(JobStatus::Idle)
{
}

void BatchInfo::setJobName(const QString& name)
{
    m_name = name;
    emit jobNameChanged(name);
}

void BatchInfo::setBeginTime(const QDateTime& begin_time)
{
    m_begin_time = begin_time;
    emit jobBeginTimeChanged(begin_time);
}

void BatchInfo::setEndTime(const QDateTime& end_time)
{
    m_end_time = end_time;
    emit jobEndTimeChanged(end_time);
}

std::optional<size_t> BatchInfo::duration() const
{
    QDateTime begin_time = beginTime();
    QDateTime end_time = endTime();
    if (begin_time.isValid() && end_time.isValid() && begin_time < end_time)
        return begin_time.msecsTo(end_time);
    return std::nullopt;
}

void BatchInfo::setComments(const QString& comments)
{
    m_comments = comments;
    emit jobCommentsChanged(comments);
}

void BatchInfo::setProgress(int progress)
{
    m_progress = progress;
    emit jobProgressChanged(progress);
}

void BatchInfo::setStatus(JobStatus status)
{
    m_status = status;
    emit jobStatusChanged(status);
}

void BatchInfo::writeTo(QXmlStreamWriter* w) const
{
    XML::writeAttribute(w, XML::Attrib::version, uint(2));

    // name
    w->writeStartElement(Tag::Name);
    XML::writeAttribute(w, XML::Attrib::value, m_name);
    w->writeEndElement();

    // identifier
    w->writeStartElement(Tag::Identifier);
    XML::writeAttribute(w, XML::Attrib::value, m_identifier);
    w->writeEndElement();

    // begin time
    w->writeStartElement(Tag::BeginTime);
    XML::writeAttribute(w, XML::Attrib::value, m_begin_time.toString(Qt::ISODateWithMs));
    w->writeEndElement();

    // end time
    w->writeStartElement(Tag::EndTime);
    XML::writeAttribute(w, XML::Attrib::value, m_end_time.toString(Qt::ISODateWithMs));
    w->writeEndElement();

    // progress
    w->writeStartElement(Tag::Progress);
    XML::writeAttribute(w, XML::Attrib::value, m_progress);
    w->writeEndElement();

    // comments
    w->writeStartElement(Tag::Comments);
    XML::writeAttribute(w, XML::Attrib::value, m_comments);
    w->writeEndElement();

    // status
    w->writeStartElement(Tag::Status);
    XML::writeAttribute(w, XML::Attrib::value, jobStatusToString(m_status));
    w->writeEndElement();
}

void BatchInfo::readFrom(QXmlStreamReader* r)
{
    const uint version = XML::readUIntAttribute(r, XML::Attrib::version);
    Q_UNUSED(version)

    while (r->readNextStartElement()) {
        QString tag = r->name().toString();

        if (tag == Tag::Comments) {
            XML::readAttribute(r, XML::Attrib::value, &m_comments);
            XML::gotoEndElementOfTag(r, tag);

            // progress
        } else if (tag == Tag::Progress) {
            QString progress_str;
            XML::readAttribute(r, XML::Attrib::value, &progress_str);
            m_progress = progress_str.toInt();
            XML::gotoEndElementOfTag(r, tag);

            // begin time
        } else if (tag == Tag::BeginTime) {
            QString begin_t_str;
            XML::readAttribute(r, XML::Attrib::value, &begin_t_str);
            m_begin_time = QDateTime::fromString(begin_t_str, Qt::ISODateWithMs);
            XML::gotoEndElementOfTag(r, tag);

            // end time
        } else if (tag == Tag::EndTime) {
            QString end_t_str;
            XML::readAttribute(r, XML::Attrib::value, &end_t_str);
            m_end_time = QDateTime::fromString(end_t_str, Qt::ISODateWithMs);
            XML::gotoEndElementOfTag(r, tag);

            // identifier
        } else if (tag == Tag::Identifier) {
            XML::readAttribute(r, XML::Attrib::value, &m_identifier);
            XML::gotoEndElementOfTag(r, tag);

            // name
        } else if (tag == Tag::Name) {
            XML::readAttribute(r, XML::Attrib::value, &m_name);
            XML::gotoEndElementOfTag(r, tag);

            // status
        } else if (tag == Tag::Status) {
            QString status_str;
            XML::readAttribute(r, XML::Attrib::value, &status_str);
            m_status = jobStatusFromString(status_str);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
