#include "Application.h"

#include <assert.h>

#include "Core/Core.h"
#include "RHI/RHI.h"
#include "Renderer/MeshRender.h"
//
//static const glm::vec3 kPositions[3] = {
//    glm::vec3{ -0.5f, -0.5f,0.0f},
//    glm::vec3{  0.5f, -0.5f,0.0f},
//    glm::vec3{  0.f,   0.5f,0.0f}
//};
//
//static const glm::vec4 kColors[3] = {
//        glm::vec4{ 1.f, 0.f, 0.f ,1.f},
//        glm::vec4{ 0.f, 1.f, 0.f ,1.f},
//        glm::vec4{ 0.f, 0.f, 1.f ,1.f}
//};


Mesh::Vertex vertices[3] = {
    {{ -0.5f, -0.5f,0.0f}, { 1.f, 0.f, 0.f ,1.f},{ 0.0f, 0.0f }, { 0.0f, 0.0f,0.0f}},
    {{  0.5f, -0.5f,0.0f}, { 0.f, 1.f, 0.f ,1.f},{ 0.0f, 1.0f }, { 0.0f, 0.0f,0.0f}},
    {{  0.f,   0.5f,0.0f}, { 0.f, 0.f, 1.f ,1.f},{ 0.0f, 1.0f }, { 0.0f, 0.0f,0.0f}},
};

Application* s_Application = nullptr;

Application::Application() {
    assert(nullptr == s_Application);
    s_Application = this;

    Logger::Init();
}

Application::~Application() {
    Logger::Shotdown();
    assert(nullptr != s_Application);
    s_Application = nullptr;
}

Application* Application::Get() {
    return s_Application;
}

bool Application::Initialize() {
    if (!RHI::Init()) {
        return false;
    }

    return true;
}

int Application::RunLoop() {
    const DriverSettings& setting = RHI::Get()->GetDriverSettings();
    if (nullptr == setting.isWindowShouldClose) {
        return -1;
    }

    RHI::Get()->StartRender();

    mesh_ = new Mesh;
    mesh_->SetVertices(vertices, 3, false);
    meshRender_ = new MeshRender(mesh_);
    while (!setting.isWindowShouldClose()) {
        RHI::Get()->BeginFrame();

        UpdateScene();

        RHI::Get()->EndFrame();

        RHI::Get()->PollEvents();
    }
    RHI::Get()->StopRender();
    return 0;
}

void Application::Uninitialize() {
    RHI::Shotdown();
}

void Application::UpdateScene() {
    meshRender_->Render();
}
