#include "workspace/WorkSpaceXMLParse.h"

#include "workspace/WorkSpace.h"
#include "entities/EntitiesManager.h"

#include "common/SpdLogger.h"
#include "WorkSpaceManager.h"

WorkSpaceXMLParse::WorkSpaceXMLParse(WorkSpace* workspace, QObject* parent) noexcept
    : QObject(parent)
    , workSpace_(workspace) {
}

bool WorkSpaceXMLParse::Save(const QString& path) {
    path_ = path;
    return false;
}

bool WorkSpaceXMLParse::ParseScene(const tinyxml2::XMLElement* element) {
    if (nullptr == element) {
        LOG_WARN("element is nullptr");
        return false;
    }

    const char* eleName = element->Name();
    if (0 == strcmp(eleName, "scene")) {
        const char* name = element->Attribute("name");

        return workSpace_->CreateScene(name);
    } else if (0 == strcmp(eleName, "describe")) {
        const char* value = element->Attribute("describe");

        workSpace_->SetDescribe(value);
        return true;
    } else if (0 == strcmp(eleName, "uuid")) {
        const char* value = element->Attribute("uuid");

        workSpace_->SetUUid(value);
        return true;
    }
    LOG_WARN("element not has name");
    return false;
}

bool WorkSpaceXMLParse::ParseTimestep(const tinyxml2::XMLElement* element) {
    if (nullptr == element) {
        LOG_WARN("element is nullptr");
        return false;
    }

    const char* path = element->Attribute("path");
    if (nullptr == path) {
        LOG_WARN("element not has path");
        return false;
    }
    
    return workSpace_->SetTimestepPath(path);
}

bool WorkSpaceXMLParse::ParseLamp(const tinyxml2::XMLElement* element) {
    if (nullptr == element) {
        LOG_WARN("element is nullptr");
        return false;
    }

    const char* path = element->Attribute("path");
    if (nullptr == path) {
        LOG_WARN("element not has path");
        return false;
    }

    return workSpace_->SetLampPath(path);
}

bool WorkSpaceXMLParse::ParseEntities(const tinyxml2::XMLElement* element) {
    if (nullptr == element) {
        LOG_WARN("element is nullptr");
        return false;
    }

    const char* eleName = element->Name();
    if (0 == strcmp(eleName, "entities")) {
        return EntitiesManager::Get().Parse(element, workSpace_);
    }
    LOG_WARN("element not has entities");
    return false;
}

bool WorkSpaceXMLParse::ParseChart(const tinyxml2::XMLElement* element)
{
    if (nullptr == element) {
        LOG_WARN("element is nullptr");
        return false;
    }

    const tinyxml2::XMLElement* xmlElement = element->FirstChildElement();
    while (nullptr != xmlElement) {
        const char* name = xmlElement->Name();
        if (0 == strcmp(name, "Wave"))
        {
            QVariantMap varChart;
            const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute();
            while (nullptr != attribute) {

                WorkSpaceManager::Get().SetDYTWaveFile(QString::fromLocal8Bit(attribute->Value()));
                attribute = attribute->Next();
            }
        }
        else if (0 == strcmp(name, "Report"))
        {
            QVariantMap varChart;
            const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute();
            while (nullptr != attribute) {

                WorkSpaceManager::Get().SetDYTReportFile(QString::fromLocal8Bit(attribute->Value()));
                attribute = attribute->Next();
            }
        }
        else if (0 == strcmp(name, "RD"))
        {
            QVariantMap varChart;
            const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute();
            while (nullptr != attribute) {

                WorkSpaceManager::Get().SetDYTRDFile(QString::fromLocal8Bit(attribute->Value()));
                attribute = attribute->Next();
            }
        }

        xmlElement = xmlElement->NextSiblingElement();
    }

    return true;
}

bool WorkSpaceXMLParse::ParseReport(const tinyxml2::XMLElement* element)
{
    QString strFile = "";
    int iBatch = 0;

    const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
    while (nullptr != attribute) {
        if (0 == strcmp(attribute->Name(), "file"))
        {
            strFile = QString::fromLocal8Bit(attribute->Value());
        }
        else if (0 == strcmp(attribute->Name(), "batch"))
        {
            iBatch = atoi(attribute->Value());
        }

        attribute = attribute->Next();
    }

    return true;
}

bool WorkSpaceXMLParse::Load(const QString& dyt) {
    std::string path = dyt.toStdString();
    LOG_INFO("load path:{}", path);
    tinyxml2::XMLDocument xmlDocument;
    tinyxml2::XMLError error = xmlDocument.LoadFile(path.c_str());
    if (tinyxml2::XMLError::XML_SUCCESS != error) {
        LOG_INFO("load feiled:{}", static_cast<int>(error));
        return false;
    }

    const tinyxml2::XMLElement* root = xmlDocument.RootElement();
    if (!ParseScene(root)) {
        return false;
    }

    const tinyxml2::XMLElement* xmlElement = root->FirstChildElement();
    while (nullptr != xmlElement) {
        const char* name = xmlElement->Name();
        if (0 == strcmp(name, "entities")) {
            ParseEntities(xmlElement);
        } else if (0 == strcmp(name, "timestep")) {
            ParseTimestep(xmlElement);
        } else if (0 == strcmp(name, "lamp")) {
            ParseLamp(xmlElement);
        }
        else if (0 == strcmp(name, "charts")) {
            ParseChart(xmlElement);
        }
        else if (0 == strcmp(name, "ReportInfo")) {
            ParseReport(xmlElement);
        }
        else {
           /* Control* control = CreateControl(name);
            if (nullptr == control) {
                continue;
            }

            if (!control_) {
                Attach(control);
            }

            control->Serialize(xmlElement);*/
        }
        xmlElement = xmlElement->NextSiblingElement();
    }

    return true;
}