// Jason Brillante "Damdoshi"
// Hanged Bunny Studio 2014-2015
//
// Sewer Glue

#include		<sstream>
#include		"Ingame.hpp"

bool			hbs::Ingame::Play(uint64_t		delay)
{
  std::list<Element>::iterator	it;
  int			i;

  (void)delay;
  if (arriving)
    {
      if ((arriving -= 1) == 0)
	{
	  static const std::string	people[5] =
	    {
	      "green shirt guys",
	      "teal shirt women",
	      "sexy black wear dude",
	      "hot pink girl",
	      "strange guy with no face"
	    };
	  std::stringstream	ss;

	  ss << "Objective:" << std::endl;
	  if (levels[level].people_to_get != -1)
	    ss << "People to eat:" << levels[level].people_to_get << std::endl;
	  else
	    ss << "Endless mode !";
	  if (levels[level].type_to_get != -1)
	    ss << "Only eat " <<  people[levels[level].type_to_get] << std::endl;
	  ss << "Use arrow key and space" << std::endl;
	  Say(ss.str(), 5, 5, 300, false, false);
	}
      return (false);
    }

  for (it = people.begin(); it != people.end();)
    {
      Element		&el = *it;

      if (el.speed > 0)
	{
	  el.anim += el.speed / 10.0;
	  if ((el.x += el.speed) > (int)ressources[BACKGROUND].getSize().x)
	    it = people.erase(it);
	  else
	    ++it;
	}
      else
	{
	  el.anim += -el.speed / 10.0;
	  if ((el.x += el.speed) < -el.people.getLocalBounds().width)
	    it = people.erase(it);
	  else
	    ++it;
	}
    }

  for (it = car.begin(); it != car.end();)
  {
    Element		&el = *it;

    if (el.speed > 0)
      {
	if ((el.x += el.speed) > (int)ressources[BACKGROUND].getSize().x)
	  it = car.erase(it);
	else
	  ++it;
      }
    else
      {
	if ((el.x += el.speed) < -(int)ressources[CAR].getSize().x)
	  it = car.erase(it);
	else
	  ++it;
      }
  }

  if (miam == 0)
    {
      if (move[LEFT])
	if ((monster_x -= 1) < 0)
	  monster_x = 0;
      if (move[RIGHT])
	if ((monster_x += 1) > (int)ressources[BACKGROUND].getSize().x)
	  monster_x = ressources[BACKGROUND].getSize().x;
      if (move[UP])
	monster_y -= 1;
      if (move[DOWN])
	monster_y += 1;

      if (move[LEFT] || move[RIGHT] || move[UP] || move[DOWN])
	monster_counter += 1;
      monster_hidden = false;
      for (i = 0; i < (int)(sizeof(sewers) / sizeof(sewers[i])); ++i)
	if (monster_x > sewers[i].left && monster_x < sewers[i].left + sewers[i].width)
	  if (monster_y > sewers[i].top && monster_y < sewers[i].top + sewers[i].height)
	    {
	      monster_hidden = true;
	      monster_hidingplace = i;
	    }
      if (monster_hidden == false && monster_y < 56)
	monster_y = 56;
      if (monster_y < 55)
	monster_y = 55;
      if (monster_hidden == false && monster_y > 82)
	monster_y = 82;
      if (monster_y > 83)
	monster_y = 83;

      if (!monster_hidden && outch_delay-- <= 0)
	for (it = car.begin(); it != car.end(); ++it)
	  {
	    if (it->layer == BACK)
	      {
		if (monster_y < 70 &&
		    monster_x > it->x - it->people.getGlobalBounds().width * 0.8 &&
		    monster_x < it->x - it->people.getGlobalBounds().width * 0.2)
		  {
		    if ((life -= 1) == 0)
		      Lost();
		    outch_delay = 50;
		    Say("outch!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
		  }
	      }
	    else
	      {
		if (monster_y >= 66 &&
		    monster_x > it->x + it->people.getGlobalBounds().width * 0.2 &&
		    monster_x < it->x + it->people.getGlobalBounds().width * 0.8)
		  {
		    if ((life -= 1) == 0)
		      Lost();
		    outch_delay = 50;
		    Say("outch!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
		  }
	      }
	  }
    }
  else
    {
      miam -= 1;
      if (monster_y < 70) // Le monstre est a l'arriere plan
	for (it = people.begin(); it != people.end();)
	  {
	    if (it->layer == BACK)
	      {
		if (levels[level].escape)
		  {
		    if (it->x < monster_x && it->speed > 0)
		      it->speed *= -1.2;
		    else if (it->x > monster_x && it->speed < 0)
		      it->speed *= -1.2;
		  }

		if (it->x > sewers[monster_hidingplace].left + sewers[monster_hidingplace].width * 0.3 &&
		    it->x < sewers[monster_hidingplace].left + sewers[monster_hidingplace].width * 0.7)
		  {
		    if (miam > 16)
		      it->speed *= 0.00000001;
		    if (miam == 16)
		      {
			/// die die die
			std::stringstream	ss;
			int			len;

			for (len = rand() % 5 + 1, i = 0; i < len; ++i)
			  ss << 'a';
			ss << "h!";
			Say(ss.str(), it->x - 6 * (i + 2) / 2, 16, 80, false, true);
			if (levels[level].type_to_get != -1)
			  {
			    if (it->subtype == levels[level].type_to_get)
			      {
				if ((people_get += 1) % 10 == 0)
				  switch (rand() % 4)
				    {
				    case 0:
				      Say("yummy!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				      break;
				    case 1:
				      Say("om niom niom...", monster_x - 6 * 8, monster_y - 5, 50, true, true);
				      break;
				    case 2:
				      Say("*burp*", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				      break;
				    case 3:
				      Say("gloup!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				    default:
				      break;
				    }
				else
				  Say("yum!", monster_x - 6 * 2, monster_y - 5, 50, true, true);
			      }
			    else
			      {
				if ((life -= 1) == 0)
				  Lost();
				outch_delay = 50;
				Say("erk!", monster_x - 6 * 2, monster_y - 5, 50, true, true);
			      }
			  }
			else
			  if ((people_get += 1) % 10 == 0)
			    switch (rand() % 4)
			      {
			      case 0:
				Say("yummy!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				break;
			      case 1:
				Say("om niom niom...", monster_x - 6 * 8, monster_y - 5, 50, true, true);
				break;
			      case 2:
				Say("*burp*", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				break;
			      case 3:
				Say("gloup!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
			      default:
				break;
			      }
			
			it = people.erase(it);
		      }
		    else
		      ++it;
		  }
		else
		  ++it;
	      }
	    else
	      ++it;
	  }
      else
	for (it = people.begin(); it != people.end();)
	  if (it->layer == FORE)
	    {
	      if (levels[level].escape)
		{
		  if (it->x < monster_x && it->speed > 0)
		    it->speed *= -1.2;
		  else if (it->x > monster_x && it->speed < 0)
		    it->speed *= -1.2;
		}

	      if (it->x > sewers[monster_hidingplace].left + sewers[monster_hidingplace].width * 0.3 &&
		  it->x < sewers[monster_hidingplace].left + sewers[monster_hidingplace].width * 0.7)
		{
		  if (miam > 16)
		    it->speed *= 0.00000001;
		  if (miam == 16)
		    {
		      /// die die die
		      std::stringstream	ss;
		      int			len;

		      for (len = rand() % 5 + 1, i = 0; i < len; ++i)
			ss << 'a';
		      ss << "h!";
		      Say(ss.str(), it->x - 6 * (i + 2) / 2, 57, 80, false, true);
		      if (levels[level].type_to_get != -1)
			{
			  if (it->subtype == levels[level].type_to_get)
			    {
			      if ((people_get += 1) % 10 == 0)
				switch (rand() % 4)
				  {
				  case 0:
				    Say("yummy!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				    break;
				  case 1:
				    Say("om niom niom...", monster_x - 6 * 8, monster_y - 5, 50, true, true);
				    break;
				  case 2:
				    Say("*burp*", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				    break;
				  case 3:
				    Say("gloup!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
				  default:
				    break;
				  }
			      else
				Say("yum!", monster_x - 6 * 2, monster_y - 5, 50, true, true);
			    }
			  else
			    {
			      if ((life -= 1) == 0)
				Lost();
			      outch_delay = 50;
			      Say("erk!", monster_x - 6 * 2, monster_y - 5, 50, true, true);
			    }
			}
		      else
			if ((people_get += 1) % 10 == 0)
			  switch (rand() % 4)
			    {
			    case 0:
			      Say("yummy!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
			      break;
			    case 1:
			      Say("om niom niom...", monster_x - 6 * 8, monster_y - 5, 50, true, true);
			      break;
			    case 2:
			      Say("*burp*", monster_x - 6 * 3, monster_y - 5, 50, true, true);
			      break;
			    case 3:
			      Say("gloup!", monster_x - 6 * 3, monster_y - 5, 50, true, true);
			    default:
			      break;
			    }
		      it = people.erase(it);
		    }
		  else
		    ++it;
		}
	      else
		++it;
	    }
	  else
	    ++it;
    }

  if ((i = monster_x + (camera_position - sg->GetScreenWidth() / 2)) > 20)
    {
      if ((camera_position -= 1) <
	  -((int)ressources[BACKGROUND].getSize().x) + sg->GetScreenWidth())
	camera_position = -((int)ressources[BACKGROUND].getSize().x) + sg->GetScreenWidth();
    }
  else if (i < -20)
    {
      if ((camera_position += 1) > 0)
	camera_position = 0;
    }

  std::list<Word>::iterator		itw;

  for (itw = words.begin(); itw != words.end(); ++itw)
    {
      if (--itw->delay < (int)itw->str.size())
	itw->fade -= 1;
      else if (itw->fade < (int)itw->str.size())
	itw->fade += 1;
    }

  if ((i = rand()) % 100 == 0)
    NewCar();
  else if (i % 10 == 0)
    NewPeople();

  if (people_get >= levels[level].people_to_get && levels[level].people_to_get != -1)
    {
      if (victory_step == 0)
	{
	  Say("Congratulation!", (sg->GetScreenWidth() - 15 * 6) / 2 - 1,
	      (sg->GetScreenHeight() - 7) / 2 - 1, 200, false, false);
	  Say("Congratulation!", (sg->GetScreenWidth() - 15 * 6) / 2,
	      (sg->GetScreenHeight() - 7) / 2, 200, true, false);
	  victory_step += 1;
	}
      else if (victory_step == 209)
	Win();
      else
	victory_step += 1;
    }


  if (warping && (warp_animation += 1) >= 9)
    sg->klondike = destination;
  return (false);
}
