void AvatarController::updateMove(const float elapsedTime)
{
	float timeout = m_avatarModel->getTimeout();
	timeout-=elapsedTime;
	m_avatarModel->setTimeout(timeout);

	float stepX,stepY;
	Vector4 terrainPosition =  m_avatarModel->getTerrainPosition();
	m_avatarModel->getStep(&stepX,&stepY);
	terrainPosition[0]+= stepX*m_avatarModel->getCelerity()*elapsedTime;
	terrainPosition[1]+= stepY*m_avatarModel->getCelerity()*elapsedTime;

	if(timeout > 0)
	{

		m_avatarModel->setTerrainPosition(terrainPosition);
		m_avatarView->setNextAnim(); 
	}
	else
	{
		//go back to idle
		startIdle();

	}
}
void AvatarEntity::updateKick(const float elapsedTime)
{
	//Si estamos golpeando, lo primero que tenemos que mirar es cuanto tiempo
	//llevamos realizando el golpe. Si se ha acabado ya el golpe volvemos al 
	//estado inactivo
	m_timeout -= elapsedTime;

	if(m_timeout < KICK_TIMEOUT/4*3  && m_timeout > KICK_TIMEOUT/4*2)
	{
		setAnim("kick1");
	}
	else if (m_timeout < KICK_TIMEOUT/4*2  && m_timeout > KICK_TIMEOUT/4)
	{
		setAnim("kick2");
	}
	else if (m_timeout < KICK_TIMEOUT/4  && m_timeout > 0)
	{
		setAnim("kick3");
	}
	else if (m_timeout < 0)
	{
		startIdle(); //vuelta a empezar
	}


}
float AvatarEntity::updateHitPoints(AvatarEntity* enemy)
{
	int dircheck = 1;

	//direccion para activar el defense
	if(m_isOnLeft)dircheck = -1; 

	//is hay colission no podemos movernos mas en la direccion equivocada, evita overlaps
	if((m_state == MOVE || m_state == JUMP)&& m_direction == -dircheck)
	{
		startIdle();
	}

	//computo del danyo dependiendo del estado

	if(enemy->getState() == AvatarEntity::KICK 
		|| enemy->getState() == AvatarEntity::JAB 
		|| enemy->getState()  == AvatarEntity::PUNCH)
	{
		if(m_state == DEFENSE)
		{
			//no damage
		}
		else if ((m_state == MOVE && m_direction == dircheck))
		{
			if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
			startDefense();
			//no damage
		}
		else
		{
			if(enemy->getState() == AvatarEntity::KICK)
			{
				m_hitPoints-=1;
			}
			if(enemy->getState() == AvatarEntity::PUNCH)
			{
				m_hitPoints-=0.8;
			}
			if(enemy->getState() == AvatarEntity::JAB)
			{
				m_hitPoints-=0.5;
			}
		}
	}
	return m_hitPoints;
				

}
void AvatarEntity::updateJump(const float elapsedTime)
{
	//Si estamos golpeando, lo primero que tenemos que mirar es cuanto tiempo
	//llevamos realizando el golpe. Si se ha acabado ya el golpe volvemos al 
	//estado inactivo
	m_timeout -= elapsedTime;

	m_deltaX = m_direction * CELERITY * elapsedTime;
	m_terrainPosition[0] += m_deltaX;

	if(m_timeout < JUMP_TIMEOUT/7*6  && m_timeout > JUMP_TIMEOUT/7*5)
	{
		setAnim("jump1");
	}
	else if(m_timeout < JUMP_TIMEOUT/7*5  && m_timeout > JUMP_TIMEOUT/7*4)
	{
		setAnim("jump2");
	}
	else if(m_timeout < JUMP_TIMEOUT/7*4  && m_timeout > JUMP_TIMEOUT/7*3)
	{
		setAnim("jump3");
	}
	else if(m_timeout < JUMP_TIMEOUT/7*3  && m_timeout > JUMP_TIMEOUT/7*2)
	{
		setAnim("jump4");
	}
	else if(m_timeout < JUMP_TIMEOUT/7*2  && m_timeout > JUMP_TIMEOUT/7)
	{
		setAnim("jump5");
	}
	else if(m_timeout < JUMP_TIMEOUT/7  && m_timeout > 0)
	{
		setAnim("jump6");
	}
	else if (m_timeout < 0)
	{
		if(m_direction == 0)
		{
			startIdle(); //vuelta a empezar
		}
		else
		{
			startMove();
		}
	}


}
void AvatarController::updateAttack(const float elapsedTime)
{
	float timeout = m_avatarModel->getTimeout();
	timeout-=elapsedTime;
	m_avatarModel->setTimeout(timeout);
	
	if(timeout > 0)
	{
		m_avatarView->setNextAnim(); 
	}
	else
	{
		//go back to idle
		startIdle();

	}
}
void AvatarEntity::updateJab(const float elapsedTime)
{
	//Si estamos golpeando, lo primero que tenemos que mirar es cuanto tiempo
	//llevamos realizando el golpe. Si se ha acabado ya el golpe volvemos al 
	//estado inactivo
	m_timeout -= elapsedTime;

	if(m_timeout < JAB_TIMEOUT/3*2  && m_timeout > JAB_TIMEOUT/3)
	{
		setAnim("jab1");
	}
	else if (m_timeout < JAB_TIMEOUT/3  && m_timeout > 0)
	{
		setAnim("jab2");
	}
	else if (m_timeout < 0)
	{
		startIdle();
	}

}
AvatarEntity::AvatarEntity(int (*func_playSfxSound)(std::string, Vector4), void (*func_stopSfxSound)(int), bool isOnLeft, bool isExternal, bool isStandAlone, bool automatic, int index)
	: m_curAnim(NULL)
	, m_state(IDLE)
	, m_hitPoints(HIT_POINTS)
	, m_direction(0)
	, m_jump_height(0)
	, m_moveSoundid(-1)
	, m_engaged(false)
	, m_heightCorrection(0)
	, m_automatic(false)
{
	startIdle();

	playSfxSound=func_playSfxSound;
	stopSfxSound=func_stopSfxSound;
	m_isOnLeft = isOnLeft;
	m_isExternal = isExternal;
	m_isStandAlone  = isStandAlone;
	m_action = IDLE_ACT;
	m_automatic = automatic;
	m_index = index;
}
void AvatarEntity::updateIdle(const float elapsedTime)
{
	m_timeout -= elapsedTime;
	
	if(m_timeout < IDLE_TIMEOUT/4*3  && m_timeout > IDLE_TIMEOUT/4*2)
	{
		setAnim("idle1");
	}
	else if (m_timeout < IDLE_TIMEOUT/4*2  && m_timeout > IDLE_TIMEOUT/4)
	{
		setAnim("idle2");
	}
	else if (m_timeout < IDLE_TIMEOUT/4  && m_timeout > 0)
	{
		setAnim("idle3");
	}
	else if (m_timeout < 0)
	{
		startIdle(); //vuelta a empezar
	}

}
void PomodoroTimerApplication::onTimer()
{
    --mMinutesLeft;

    if (mMinutesLeft == 0)
    {
        QSound::play(NotificationSoundPath);
        showEndMessage(mCurrentState);

        switch (mCurrentState)
        {
        case (PomodoroState::Work):
            startRest();
            break;
        case (PomodoroState::Rest):
            startIdle();
            break;
        default:
            Q_ASSERT(false);
        }
    }

    updateSystemTrayIcon();
}
void AvatarEntity::updateExternal(std::string message, float value)
{
	m_terrainPosition[0] = value;

	if(message == "idle#")
	{
		startIdle();
	}
	else if(message == "punch")
	{
		startPunch();
	}
	else if(message == "jab##")
	{
		startJab();
	}
	else if(message == "kick#")
	{
		startKick();
	}
	else if(message == "lower")
	{
		startLower();
	}
	else if(message == "loweS")
	{
		if(m_lowerSoundid != -1) stopSfxSound(m_lowerSoundid);
		startIdle();
	}
	else if(message == "move#")
	{
		startMove();
	}
	else if(message == "move-")
	{
		m_direction = -1;
	}
	else if(message == "move+")
	{
		m_direction = 1;
	}
	else if(message == "moveJ")
	{
		if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
		startJump();
	}
	else if(message == "moveS")
	{
		if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
		startIdle();
	}
	else if(message == "jump#")
	{
		startJump();
	}

	else if(message == "cover")
	{
		if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
		startDefense();
	}

}
void AvatarEntity::updateState()
{
	ActionManager::actions actions = Core::singleton().actionManager().getplayeractions(m_index);
	
	if (actions.up == false
		&& actions.defense == false
		&& actions.down == false
		&& actions.left == false
		&& actions.right == false
		&& actions.punch == false
		&& actions.kick == false
		&& actions.megapunch == false
		&& actions.up == false)
	{
		//si no hay accion debemos detener el movimiento 
		if(m_state == MOVE )
		{
			if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
			startIdle();
		}
		else if(m_state == DEFENSE)
		{
			startIdle();
		}
		else if(m_state == LOWER)
		{
			if(m_lowerSoundid != -1) stopSfxSound(m_lowerSoundid);
			startIdle();
		}
	}
	else
	{

		if(actions.punch == true && m_state == IDLE)
		{
			startPunch();
		}
		else if(actions.megapunch == true && m_state == IDLE)
		{
			startJab();
		}
		else if(actions.kick == true && m_state == IDLE)
		{
			startKick();
		}
		else if(actions.down == true && m_state == IDLE)
		{
			if(m_state != LOWER) startLower();
		}
		else if(actions.left == true && m_state!= JUMP && actions.up == false)
		{
			m_direction = -1;
			if(m_state == IDLE || m_state == DEFENSE) startMove();
		}
		else if(actions.right == true && m_state!= JUMP && actions.up == false)
		{
			m_direction = 1;
			if(m_state == IDLE || m_state == DEFENSE) startMove();
		}
		else if(actions.left == true && actions.up == true && (m_state == IDLE || m_state == MOVE))
		{
			m_direction = -1;
			if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
			startJump();
		}
		else if(actions.right == true && actions.up == true && (m_state == IDLE || m_state == MOVE))
		{
			m_direction = 1;
			if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
			startJump();
		}
		else if(actions.up == true && m_state == IDLE)
		{
			m_direction = 0;
			startJump();
		}

		else if(actions.defense == true && m_state == IDLE)
		{
			if(m_moveSoundid != -1) stopSfxSound(m_moveSoundid);
			startDefense();
		}
	}

	m_action = IDLE_ACT;
}