#include "MeshViewerView.h"

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

#include <iostream>

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

MeshViewerView::~MeshViewerView()
{

}

MeshViewerView::MeshViewerView()
{
    mName = "MeshViewerView";
    mBackgroundColor = {204, 204, 255};
}

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

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

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

        mMeshNode->setFillColor(mBackgroundColor);

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

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

    if (mMaterialDirty)
    {
        mMeshNode->setFillColor(mBackgroundColor);
    }

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

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