#include "MeshViewerView.h"

#include "MeshNode.h"
#include "TransformNode.h"
#include "AbstractMesh.h"
#include "MeshPrimitives.h"

#include "ThemeManager.h"
#include "PaintEvent.h"

#include <iostream>

std::unique_ptr<MeshViewerView> MeshViewerView::Create()
{
    return std::make_unique<MeshViewerView>();
}

MeshViewerView::~MeshViewerView()
{

}

MeshViewerView::MeshViewerView()
{
    mName = "MeshViewerView";
}

void MeshViewerView::doPaint(const PaintEvent* event)
{
    if (!mVisible)
    {
        return;
    }

    if (!mMeshNode)
    {
        mMeshNode = std::make_unique<MeshNode>(Transform(mLocation));
        mMeshNode->setName(mName + "_MeshNode");

        mMeshNode->setWidth(mSize.mWidth);
        mMeshNode->setHeight(mSize.mHeight);

        mMeshNode->setFillColor(event->getThemesManager()->getColor(mBackground));

        mRootNode->addChild(mMeshNode.get());
    }

    if (mTransformDirty)
    {
        mMeshNode->setWidth(mSize.mWidth);
        mMeshNode->setHeight(mSize.mHeight);
    }

    if (mMaterialDirty)
    {
        mMeshNode->setFillColor(event->getThemesManager()->getColor(mBackground));
    }

    if (!mMesh)
    {
        auto mesh = MeshPrimitives::buildRectangleAsTriMesh();
        mMesh = std::move(mesh);

        mMeshNode->setMesh(mMesh.get());
    }
}
