
#include <WFX/SDL/System.h>
#include <WFX/SDL/SDLTimeTask.h>
#include <WFX/SDL/SDLInputHandler.h>
#include <WFX/SDL/SDLRenderWindow.h>
#include <WFX/SDL/SDLRenderer.h>
#include <WFX/SDL/SDLTextureManager.h>

using namespace WFX;
using namespace WFX::lang;
using namespace WFX::renderer;
using namespace WFX::scene;
using namespace WFX::SDL;

System::System() {
    LOGN("Initiating SDL System... ");

    LOG("ok!");
}

System::~System() {
}

WFX::kernel::TimeTask* System::getTimeTask() {
    try {
        if (!TimeTask)  
            TimeTask = new SDLTimeTask(); 
    } catch (WFXException& e) {
        RETHROW(e, "unable to create TimeTask");
    }
    return TimeTask;
}

WFX::input::InputHandler* System::getInputHandler() {
    try {
        if (!InputHandler)
            InputHandler = new SDLInputHandler();
    } catch (WFXException& e) {
        RETHROW(e, "unable to create InputHandler");
    }
    return InputHandler;
}

WFX::renderer::RenderWindow* System::getRenderWindow(Int w, Int h, Int d, std::string title, bool fs) {
    WFX::renderer::RenderWindow* rw;
    try {
        rw = new SDLRenderWindow(w,h,d,title,fs);
        RenderWindows.push_back(rw);
    } catch (WFXException& e) {
        RETHROW(e, "unable to create RenderWindow");
    }
    return rw;
}

WFX::renderer::Renderer* System::getRenderer() {
    try {
        if (!Renderer)
            Renderer = new SDLRenderer();
    } catch (WFXException& e) {
        RETHROW(e, "unable to create Renderer");
    }
    return Renderer;
}

WFX::scene::TextureManager* System::getTextureManager(bool def) {
    try {
        if (!TextureManager)
            if (def)
                TextureManager = new WFX::scene::TextureManager();
            else
                TextureManager = new SDLTextureManager();
    } catch (WFXException& e) {
        RETHROW(e, "unable to create TextureManager");
    }
    return TextureManager;
}
 
WFX::util::EndianConvert* System::getEndianConvert() {
    //return new WFX::util::SwapEndian();
    return new WFX::util::EndianConvert();
}

std::string System::getCurrentPath() {
    //TODO: make this work in windows...
    
    //1k should be enough for everyone...
    Char tmpPath[1024];
    getcwd(tmpPath,1024);

    std::string r(tmpPath);

    return r;
}
