/***********************************************************
 * 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 "SetupState.h"
#include "MenuState.h"

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

SetupState::SetupState(){
    this->_sonido = true;
    this->_idioma = "SP";
    this->_descripcionSonido = "[font='astonish-20'][colour='FFFFFFFF']SONIDO: ACTIVADO";
    this->_descripcionIdioma = "[font='astonish-20'][colour='FFFFFFFF']IDIOMA: CASTELLANO";
}

void SetupState::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
    _buttonBack = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Text", "ButtonBack");
    _buttonSonido = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Image", "ButtonSonido");
    _buttonIdioma = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Image", "ButtonIdioma");
    _descripcion = CEGUI::WindowManager::getSingleton().createWindow("GodModeOn/Text", "Descripcion");

    // Carga los botones de la escena
    _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");

    if(_sonido)
        _buttonSonido->setProperty("Image", "set:GodModeOnGui image:SonidoON");
    else
        _buttonSonido->setProperty("Image", "set:GodModeOnGui image:SonidoOFF");
    _buttonSonido->setPosition(CEGUI::UVector2(CEGUI::UDim(0.30625,0),CEGUI::UDim(0.45,0)));
    _buttonSonido->setSize(CEGUI::UVector2(CEGUI::UDim(0,200),CEGUI::UDim(0,200)));

    if(_idioma == "SP")
        _buttonIdioma->setProperty("Image", "set:GodModeOnGui image:FlagSP");
    else
        _buttonIdioma->setProperty("Image", "set:GodModeOnGui image:FlagEN");
    _buttonIdioma->setPosition(CEGUI::UVector2(CEGUI::UDim(0.5375,0),CEGUI::UDim(0.45,0)));
    _buttonIdioma->setSize(CEGUI::UVector2(CEGUI::UDim(0,200),CEGUI::UDim(0,200)));

    _descripcion->setPosition(CEGUI::UVector2(CEGUI::UDim(0.0,0),CEGUI::UDim(0.80,0)));
    _descripcion->setSize(CEGUI::UVector2(CEGUI::UDim(1.0,0),CEGUI::UDim(0,37)));
    _descripcion->setProperty("HorzFormatting", "HorzCentred");
    _descripcion->setText("[font='astonish-20'][colour='FFFFFFFF']");

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

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

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

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

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

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

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

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

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

void SetupState::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();

    // Gestión de mouse sobre opciones
    if(_buttonSonido->isHit(vec2Mouse)) handle_MouseEnter_WindowSonido();
    else if(_buttonIdioma->isHit(vec2Mouse)) handle_MouseEnter_WindowIdioma();
    else handle_MouseLeaves_WindowOpciones();

    // Muestra el mouse al terminar
    _mouse->setImage("GodModeOnGui", "MouseCursor");
}

void SetupState::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();
            else if(_buttonSonido->isHit(vec2Mouse)){ handle_MouseClick_WindowSonido(); handle_MouseEnter_WindowSonido(); }
            else if(_buttonIdioma->isHit(vec2Mouse)){ handle_MouseClick_WindowIdioma(); handle_MouseEnter_WindowIdioma(); }

            break;

        default:
            break;
    }
}

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

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

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

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

bool SetupState::handle_MouseClick_WindowSonido()
{
    if(_sonido){
        _buttonSonido->setProperty("Image", "set:GodModeOnGui image:SonidoOFF");
        _descripcionSonido = "[font='astonish-20'][colour='FFFFFFFF']SONIDO: DESACTIVADO";
        _sonido = false;
    }
    else{
        _buttonSonido->setProperty("Image", "set:GodModeOnGui image:SonidoON");
        _descripcionSonido = "[font='astonish-20'][colour='FFFFFFFF']SONIDO: ACTIVADO";
        _sonido = true;
    }
    return true;
}

bool SetupState::handle_MouseClick_WindowIdioma()
{
    // Solo idioma SP de momento
    /*if(_idioma == "SP"){
        _buttonIdioma->setProperty("Image", "set:GodModeOnGui image:FlagEN");
        _descripcionIdioma = "[font='astonish-20'][colour='FFFFFFFF']LANGUAGE: ENGLISH";
        _idioma = "EN";
    }
    else{
        _buttonIdioma->setProperty("Image", "set:GodModeOnGui image:FlagSP");
        _descripcionIdioma = "[font='astonish-20'][colour='FFFFFFFF']IDIOMA: CASTELLANO";
        _idioma = "SP";
    }*/
    return true;
}

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

bool SetupState::handle_MouseEnter_WindowSonido()
{
    _descripcion->setText(_descripcionSonido);
    return true;
}

bool SetupState::handle_MouseEnter_WindowIdioma()
{
    _descripcion->setText(_descripcionIdioma);
    return true;
}

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

bool SetupState::handle_MouseLeaves_WindowOpciones()
{
    _descripcion->setText("");
    return true;
}
