/***********************************************************
 * God Mode On, Und[0]ne Devs < http://undonedevs.tk >
 * 2013 Pepe Romero < wizardpepe@gmail.com >
 * < https://bitbucket.org/undone_devs/god-mode-on >
 ***********************************************************/

#include "CreditsState.h"
#include "MenuState.h"

template<> CreditsState* Ogre::Singleton<CreditsState>::msSingleton = 0;

void CreditsState::enter()
{
    _root = Ogre::Root::getSingletonPtr();

    _sceneMgr = _root->getSceneManager("SceneManager");
    _camera = _sceneMgr->getCamera("PjCamera");
    _viewport = _root->getAutoCreatedWindow()->addViewport(_camera);

    _exitGame = false;

    // Crea un window con un background
    _sheet = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "Sheet");
    _background = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Image", "MenuBackground");
    _background->setProperty("Image", "set:GodModeOnMenus image:MainGenerico");
    _sheet->addChildWindow(_background);

    // Carga mouse
    _mouse = CEGUI::MouseCursor::getSingletonPtr();
    _mouse->setImage("GodModeOnGui", "MouseCursor");
    _mouse->setVisible(true);
    _mouse->show();

    // Crea los botones
    _credits = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Text", "Credits");
    _buttonBack = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Text", "ButtonBack");

    // Carga los botones de la escena
    _credits->setPosition(CEGUI::UVector2(CEGUI::UDim(0.03,0),CEGUI::UDim(0.10,0)));
    _credits->setSize(CEGUI::UVector2(CEGUI::UDim(0.9,0),CEGUI::UDim(0.7,0)));
    _credits->setProperty("HorzFormatting", "HorzCentred");
    _credits->setText("\
    [font='astonish-27'][colour='FFFFFFFF']PROGRAMACION\n\
    [font='astonish-20'][colour='FFA2A1A1']PEPE ROMERO [colour='FFA9D0F5']UNDONEDEVS.TK\n\
    \n\
    \n\
    [font='astonish-27'][colour='FFFFFFFF']DESAROLLO GRAFICO\n\
    [font='astonish-20'][colour='FFFFFFFF']GENERAL: [colour='FFA2A1A1']PEPE ROMERO [colour='FFA9D0F5']UNDONEDEVS.TK\n\
    [colour='FFFFFFFF']LIBRO: [colour='FFA2A1A1']OOLE [colour='FFA9D0F5']BLENDSWAP.COM\n\
    [colour='FFFFFFFF']TEXTURAS: [colour='FFA2A1A1']NOBIAX [colour='FFA9D0F5']NOBIAX.DEVIANTART.COM\n\
    [colour='FFFFFFFF']FUENTE ASTONISHED: [colour='FFA9D0F5']MISPRINTEDTYPE.COM");

    _buttonBack->setPosition(CEGUI::UVector2(CEGUI::UDim(0.03,0),CEGUI::UDim(0.90,0)));
    _buttonBack->setSize(CEGUI::UVector2(CEGUI::UDim(0,138),CEGUI::UDim(0,37)));
    _buttonBack->setText("[font='astonish-30'][colour='FFA2A1A1']VOLVER");

    // Añade los botones a la escena
    _background->addChildWindow(_credits);
    _background->addChildWindow(_buttonBack);

    // Establece el sheet
    CEGUI::System::getSingleton().setGUISheet(_sheet);
}

void CreditsState::exit()
{
    _sceneMgr->clearScene();
    _root->getAutoCreatedWindow()->removeAllViewports();
    CEGUI::WindowManager::getSingletonPtr()->destroyAllWindows();
}

void CreditsState::pause(){}
void CreditsState::resume(){}

bool CreditsState::frameStarted(const Ogre::FrameEvent& evt)
{
    return true;
}

bool CreditsState::frameEnded(const Ogre::FrameEvent& evt)
{
    if (_exitGame)
        return false;
    return true;
}

void CreditsState::keyPressed(const OIS::KeyEvent &e)
{
    switch(e.key){

    case OIS::KC_ESCAPE:
        handle_MouseClick_WindowBack();
        break;
    default:
        break;

    }
}

void CreditsState::keyReleased(const OIS::KeyEvent &e){}

void CreditsState::mouseMoved(const OIS::MouseEvent &e){

    // Actualiza el mouse
    CEGUI::Vector2 vec2Mouse = CEGUI::Vector2(e.state.X.abs, e.state.Y.abs);
    _mouse->setPosition(vec2Mouse);

    // Gestión de mouse sobre botones
    if(_buttonBack->isHit(vec2Mouse)) handle_MouseEnter_WindowBack();
    else handle_MouseLeaves_WindowBack();
}

void CreditsState::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id){

    // Posición del mouse
    CEGUI::Vector2 vec2Mouse = CEGUI::Vector2(e.state.X.abs, e.state.Y.abs);

    switch(id){
        case 0: // Botón izquierdo

            // Gestión de clicks de botones
            if(_buttonBack->isHit(vec2Mouse)) handle_MouseClick_WindowBack();

            break;

        default:
            break;
    }
}

void CreditsState::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id){}

CreditsState* CreditsState::getSingletonPtr()
{
    return msSingleton;
}

CreditsState& CreditsState::getSingleton()
{
    assert(msSingleton);
    return *msSingleton;
}

bool CreditsState::handle_MouseClick_WindowBack()
{
    changeState(MenuState::getSingletonPtr());
    return true;
}

bool CreditsState::handle_MouseEnter_WindowBack()
{
    _buttonBack->setText("[font='astonish-30'][colour='FFFFFFFF']VOLVER");
    return true;
}

bool CreditsState::handle_MouseLeaves_WindowBack()
{
    _buttonBack->setText("[font='astonish-30'][colour='FFA2A1A1']VOLVER");
    return true;
}
