/**
 *  The main ingame process cycle when keen galaxy is up and running
 */
void CPlayGameGalaxy::process()
{
    if(g_pSound->pauseGamePlay() )
        return;

    CEventContainer &eventContainer = g_pBehaviorEngine->m_EventList;

    if( !gpMenuController->active() )
    {
        processInput();

        const bool msgboxactive = !mMessageBoxes.empty();

        // Trigger the Status screen here
        if(m_Inventory.showStatus())
        {
            if( g_pInput->getPressedAnyButtonCommand(0) )
            {
                g_pSound->playSound(SOUND_STATUS_SLIDE_OUT);
                m_Inventory.toggleStatusScreen();
            }
        }
        else
        {
            if(!msgboxactive && g_pInput->getPressedCommand(IC_STATUS))
            {
                g_pSound->playSound(SOUND_STATUS_SLIDE_IN);
                m_Inventory.toggleStatusScreen();
            }
        }



        /*if( !msgboxactive && g_pInput->getPressedCommand(IC_STATUS)  )
        {
        	m_Inventory.toggleStatusScreen();
        }*/

        // process Page if one is open. Could be one of the finale scenes
        //if(m_Page.isActive())
        //m_Page.process();

        // process World Map if active. At the start it's enabled
        if(m_WorldMap.isActive())
        {
            m_WorldMap.process(msgboxactive);
        }

        // process World Map if active. At the start it's enabled
        if(m_LevelPlay.isActive())
        {
            m_LevelPlay.process(msgboxactive);
        }

        // We have to show the status screen, do so...
        if( m_Inventory.showStatus() )
        {
            m_Inventory.drawStatus();
        }

        // Draw some Textboxes with Messages only if one of those is open and needs to be drawn
        if(msgboxactive)
        {
            CMessageBoxGalaxy *pMB = mMessageBoxes.front().get();
            pMB->process();

            if(pMB->isFinished())
                mMessageBoxes.pop_front();

            return;
        }

        //// Special Keyboard Input
        /// Cheat Codes
        if( g_pInput->getHoldedKey(KF10) )
        {
            if(g_pInput->getHoldedKey(KJ))
            {
                m_Cheatmode.jump = !m_Cheatmode.jump;
                std::string jumpstring = "Jump-Cheat has been ";
                jumpstring += ((m_Cheatmode.jump) ? "enabled" : "disabled");
                eventContainer.add( new EventSendDialog(jumpstring) );
            }
            else if(g_pInput->getHoldedKey(KG))
            {
                m_Cheatmode.god = !m_Cheatmode.god;
                std::string godstring = "God-Mode has been ";
                godstring += ((m_Cheatmode.god) ? "enabled" : "disabled");
                eventContainer.add( new EventSendDialog(godstring) );
            }
            else if(g_pInput->getHoldedKey(KI))
            {
                m_Cheatmode.items = true;
                eventContainer.add( new EventSendDialog("Get all Items!") );
                m_Inventory.Item.triggerAllItemsCheat();
                m_Cheatmode.items = true;
            }
            else if(g_pInput->getHoldedKey(KN))
            {
                m_Cheatmode.noclipping = true;
                eventContainer.add( new EventSendDialog("No clipping toggle!") );
            }
        }

    }

    // In this part we will poll all the relevant Events that are important for the
    // Galaxy Main Engine itself. For example, load map, setup world map, show Highscore
    // are some of those events.

    if( EventSendBitmapDialogMsg *ev = eventContainer.occurredEvent<EventSendBitmapDialogMsg>() )
    {
        CMessageBoxBitmapGalaxy *pMsgBox = new CMessageBoxBitmapGalaxy( ev->Msg, ev->BitmapRef, ev->Direction );
        pMsgBox->init();

        mMessageBoxes.push_back( pMsgBox );
        eventContainer.pop_Event();
    }
    else if( EventSendBitmapDialogMessages *ev = eventContainer.occurredEvent<EventSendBitmapDialogMessages>() )
    {
        std::vector< SmartPointer<EventSendBitmapDialogMsg> >::iterator it = ev->msgs.begin();
        for( ; it != ev->msgs.end() ; it++ )
        {
            CMessageBoxBitmapGalaxy *pMsgBox = new CMessageBoxBitmapGalaxy( (*it)->Msg, (*it)->BitmapRef, (*it)->Direction );
            pMsgBox->init();

            mMessageBoxes.push_back( pMsgBox );
        }
        eventContainer.pop_Event();
    }
    else if( EventSendDialog *ev = eventContainer.occurredEvent<EventSendDialog>() )
    {
        CMessageBoxGalaxy *pMsgBox = new CMessageBoxGalaxy( ev->Msg );
        pMsgBox->init();

        mMessageBoxes.push_back( pMsgBox );
        eventContainer.pop_Event();
    }
    /*else if( EventSendSelectionDialogMsg* ev = EventContainer.occurredEvent<EventSendSelectionDialogMsg>() )
    {
    	g_pMusicPlayer->stop();
    	mMessageBoxes.push_back( new CMessageBoxSelection( ev->Message, ev->Options ) );
    	EventContainer.pop_Event();
    }*/


    if(mMessageBoxes.empty())
    {
        if( EventEnterLevel *ev = eventContainer.occurredEvent<EventEnterLevel>() )
        {
            // Start a new level!
            if(ev->data > 0xC000)
            {
                const Uint16 NewLevel = ev->data - 0xC000;
                if(NewLevel < 50)
                {
                    g_pMusicPlayer->stop();
                    m_WorldMap.setActive(false);
                    m_LevelPlay.loadLevel(NewLevel);
                    m_LevelName = m_LevelPlay.getLevelName();
                    g_pSound->playSound( SOUND_ENTER_LEVEL );
                    m_LevelPlay.setActive(true);
                }
            }
            eventContainer.pop_Event();
        }
        else if( eventContainer.occurredEvent<EventRestartLevel>() )
        {
            g_pMusicPlayer->stop();
            m_LevelPlay.reloadLevel();
            eventContainer.pop_Event();
        }
        else if( EventExitLevel *ev = eventContainer.occurredEvent<EventExitLevel>() )
        {
            g_pMusicPlayer->stop();
            m_LevelPlay.setActive(false);
            m_WorldMap.setActive(true);
            m_LevelName = m_WorldMap.getLevelName();
            m_WorldMap.loadAndPlayMusic();
            eventContainer.add( new EventPlayerEndLevel(*ev) );
            eventContainer.pop_Event();
        }
        else if( EventPlayTrack *ev =  eventContainer.occurredEvent<EventPlayTrack>() )
        {
            g_pMusicPlayer->stop();
            if( g_pMusicPlayer->loadTrack(m_ExeFile, ev->track) )
                g_pMusicPlayer->play();
            eventContainer.pop_Event();
        }
    }
}
/**
 *  The main ingame process cycle when keen galaxy is up and running
 */
void CPlayGameGalaxy::ponder(const float deltaT)
{
	if(g_pSound->pauseGamePlay() )
		return;
	
	if( !gMenuController.active() )
	{
        processInput();

        const bool msgboxactive = !mMessageBoxes.empty();

        int playerCount = 0;
        for( auto &inv : mInventoryVec )
        {
            // Trigger the Status screen here
            if(inv.showStatus())
            {
                if( gInput.getPressedAnyButtonCommand(playerCount) )
                {
                    g_pSound->playSound(SOUND_STATUS_SLIDE_OUT);
                    inv.toggleStatusScreen();
                }
            }
            else
            {
                if(!msgboxactive && gInput.getPressedCommand(playerCount, IC_STATUS))
                {
                    g_pSound->playSound(SOUND_STATUS_SLIDE_IN);
                    inv.toggleStatusScreen();
                }
            }

            playerCount++;
        }


		// process World Map if active. At the start it's enabled
		if(m_WorldMap.isActive())
		{
            m_WorldMap.setMsgBoxOpen(msgboxactive);
            m_WorldMap.ponder(deltaT);
		}

        // process inlevel play if active. At the start it's disabled, m_WorldMap turns it on.
		if(m_LevelPlay.isActive())
		{
            m_LevelPlay.setMsgBoxOpen(msgboxactive);
            m_LevelPlay.ponder(deltaT);
		}                

		// Draw some Textboxes with Messages only if one of those is open and needs to be drawn
		if(msgboxactive)
		{
			CMessageBoxGalaxy *pMB = mMessageBoxes.front().get();
            pMB->ponder();

			if(pMB->isFinished())
				mMessageBoxes.pop_front();

			return;
		}

		//// Special Keyboard Input
		/// Cheat Codes
		if( gInput.getHoldedKey(KF10) )
		{
			if(gInput.getHoldedKey(KJ))
			{
				m_Cheatmode.jump = !m_Cheatmode.jump;
				std::string jumpstring = "Jump-Cheat has been ";
				jumpstring += ((m_Cheatmode.jump) ? "enabled" : "disabled");
                showMsg(jumpstring);
			}
			else if(gInput.getHoldedKey(KG))
			{
				m_Cheatmode.god = !m_Cheatmode.god;
				std::string godstring = "God-Mode has been ";
				godstring += ((m_Cheatmode.god) ? "enabled" : "disabled");
                showMsg(godstring);
			}
			else if(gInput.getHoldedKey(KI))
			{
                showMsg("Get all Items!");

                for( auto &inv : mInventoryVec )
                    inv.Item.triggerAllItemsCheat();

				m_Cheatmode.items = true;
			}
			else if(gInput.getHoldedKey(KN))
			{
				m_Cheatmode.noclipping = true;
                showMsg("No clipping toggle!");
			}
			else if(gInput.getHoldedKey(KS))
			{
		for( auto &inv : mInventoryVec )
		    inv.Item.triggerAllItemsCheat();

				m_Cheatmode.items = true;
				m_Cheatmode.god = true;
				m_Cheatmode.jump = true;
                showMsg("Super Cheat!");
			}
		}

	}

}