
#include "../include/WFXTest.h"


using namespace WFX;
using namespace WFX::scene;
using namespace WFX::kernel;
using namespace WFX::renderer;
using namespace WFX::input;
using namespace WFX::io;
using namespace WFX::math;
using namespace std;


WFXTest::WFXTest() : WFXGUIApplication() {
    LOG("WFXTest -> create");
    setup(800, 600, 16, "WFXTest");
    //scene...
    MainGraph = new ListSceneGraph();
    MainGraph->getRenderContext().Lighting = true;
    MainGraph->getRenderContext().TextureMapping = true;
    MainPort = new Viewport(0.0f, 0.0f, 1.0f, 1.0f);
    MainCamera = new FirstPersonCamera(Window);
    MainJob = new RenderJob(MainGraph, MainCamera, MainPort);
    Window->addRenderJob(MainJob);
    //Node* n = GeometryLoader::load(File("untitled.3ds"));
    //Node* n = GeometryLoader::load(File("HanSolo.3ds"));
    //Node* n = GeometryLoader::load(File("face.3ds"), true);
    //Node* n = GeometryLoader::load(File("alien.3ds"), true);
    Node* n = GeometryLoader::load(File("park.3ds"), true);

    SceneGraphTransformer t;
    n->accept(t);

    //Vector3 tr(0,0,5);
    //n->translate(tr);
    //n->scale(2.0f);
    //Node* n2 = GeometryLoader::load(File("face.3ds"), true);
    //Node* n3 = GeometryLoader::load(File("foce.3ds"), true);
    //n3->translate(tr);

    OctreeBuilder* b = new OctreeBuilder();
    b->addSource(n);
    //b->addSource(n2);
    //b->addSource(n3);
    b->build();
    MainGraph->add(b->getOctree());
    //delete b;
    
    run();
}

WFXTest::~WFXTest() {
    LOG("WFXTest -> destroy");
    delete MainGraph;
    delete MainCamera;
    delete MainJob;
    delete MainPort;
}

bool WFXTest::start() {
    LOG("WFXTest -> start");
    return true;
}

void WFXTest::stop() {
    LOG("WFXTest -> stop");
}

void WFXTest::update() {
    OctreeNode::Drawn = 0;
    Window->centerCursor();
    MainCamera->update();
    MainGraph->update();
    Window->render();
    Window->swapBuffers();
}

void WFXTest::keyPressed(WFX::input::KeyEvent k) {
    if (k.Key == WFXK_ESC) {
        exit();
    }
    if (k.Key == WFXK_p) {
        saveScreenShot();
    }
    if (k.Key == WFXK_o) {
        SceneGraphPrinter p;
        MainGraph->accept(p);
    }
    if (k.Key == WFXK_f) {
        LOG("FPS: " << TimeTask->FPS);
        LOG("ON Drawn " << OctreeNode::Drawn);
    }
    
}

void WFXTest::keyReleased(WFX::input::KeyEvent k) {
}

void WFXTest::timerEvent(int id) {
    LOG("TimerEvent " << id);
    exit();
}

