#include "BarinkEngine.h"
#include "imgui.h"
#include "GUI.h"
#include "Util.h"

/*
* Define globals
*/
Camera* cam;


Shader* shader;

Renderable* Cube;
Material* matCube;
Texture* textureCube;

Renderable* Cube2;
Material* matCube2;

char* code = new char[254];


const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";



/*
* Runs once at startup 
* - USe to initialize the game/sandbox/demo
*/
void Start() {

    /*
        Building a very basic scene graph
    */
    SceneNode MyCube = SceneNode();
    MyCube.name = "MyCube";

    SceneNode MyBaby = SceneNode();
    MyBaby.name = "Baby";

    SceneNode MySecondCube = SceneNode();
    MySecondCube.name = "MySecondCube";


    MyCube.addChild(MyBaby);

    
    Scene scene = Scene("My awesome Game Scene");
    scene.GetRoot().addChild(MyCube);
    scene.GetRoot().addChild(MySecondCube);


   // Walk scene graph
    PrintSceneTree(scene.GetRoot(),0);

    shader = new Shader(vertexShaderSource, fragmentShaderSource);

    textureCube = new Texture("build/SandboxApplication/Debug/Textures/wall.jpg");

    matCube = new Material(*shader);
    matCube->Color = glm::vec3(1.0, 0.0, 0.0);

    matCube2 = new Material(*shader);
    matCube2->Color = glm::vec3(0.0, 1.0f, 0.0);

    std::string cubePath = "build/SandboxApplication/Debug/Models/Cube.obj";
    std::string lanternPath = "build/SandboxApplication/Debug/Models/Latern.gltf";
    /*
    * load meshes 
    */
    Cube = Renderable::Load(lanternPath);
    Cube2 = Renderable::Load(cubePath);
    Cube->addChild(*Cube2);

    Cube->shader = shader;
    Cube2->shader = shader;

    Cube->texture = textureCube;
    Cube2->texture = textureCube;

    Cube2->transform.Position = glm::vec3(-9.0f, 0.0f, 0.0f);

    Cube->transform.Position = glm::vec3(-8.0f, 0.0f, -2.0f);

    cam = new Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);

    memset(code, '\0', 254);

}


/*
* Runs every frame
* - Use to draw Immediate mode graphics (Not meant for HUD's )
*/
void ImmediateGraphicsDraw() {
    ImGui::NewFrame();

    // Show ImGui demo such that I can easily look
    // at possible GUI elements to use
    ImGui::ShowDemoWindow();


    // Show internal BarinkEngine stats
    ShowStats();


    // Show different tooling for this specific sandbox
    CameraTool(cam);
    ScriptingTool(code);

    transformWindow(Cube->transform, "Transform (Cube)");

    transformWindow(Cube2->transform, "Transform (Cube2)");

    materialWindow(*matCube, "Material Cube");
    materialWindow(*matCube2, "Material Cube2");



}

/*
* Runs every frame
* - Meant for game logic ( non-physics related) 
*/
void Update()
{


    /*
    * NOTE: this needs to move to the renderer
    * Render code should not appear in the sandbox file
    */
    glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    shader->Use();
    shader->setUniformMat4("P", projection);
    shader->setUniformMat4("M", CalculateModelMat(Cube->transform));
    shader->setUniformMat4("V", cam->GetViewMatrix());
    matCube->Apply();

    Cube->Draw();

    shader->setUniformMat4("M", CalculateModelMat(Cube2->transform));
    matCube2->Apply();

    Cube2->Draw();

}

/*
* Runs at the end of the program
* - Meant for cleanup
*/
void Stop() {
    // Cleanup
    Cube->VAO.Delete();
    Cube->elementBuffer.Delete();

    Cube2->VAO.Delete();
    Cube2->elementBuffer.Delete();

    delete Cube2;
    delete Cube;

    delete matCube;
    delete matCube2;

    delete shader;
}