#include "CMenu.hpp"
#include "CMap.hpp"
#include "CMain.hpp"
#include "CDisplay.hpp"
#include "Interaction.hpp"
#include "CCine.hpp"
#include "CBattle.hpp"
#include "Quests.hpp"

CCine cine;
void Cinematic2(int id);
void CinematicPlant(int id);

void CinematicSummervaleCave(int id)
{
}


void CinematicPlant(int id)
{
bool done = false;
int counter = 0;

   clear(screen);
   textprintf(screen, font, screen->w/2-sizeof("Day 1...")*10, screen->h/2+35, makecol(255,0,0), "The Plant!");
   rest(1200);

   speed_counter = 0;
   do
   {

    while (speed_counter > 0)
    {

    map.RenderScene(200,0);


    if(counter<100)
    {
     player[0].Move(map, EAST);
     counter++;
    }

    else if(counter >= 100)
    {
        Talk("Riku Santorium",
         "Ladies and Gentlemen! We must stand up before the witches and warlocks of this fine country! We must hunt them down "
         "and slaughter them like the pigs they are! That is why I am here to announce that the Emperor has decreed that anyone, "
         "and I mean anyone with anything of magical origin is to be immediately placed under arrest and interrogated!" );
         rest(100);
        Talk("Riku Santorium", "*Pauses to take a drink of water*");
        Talk("Riku Santorium", "*He begins to cough violently and suddenly out of his pocket falls the herb you planted*");
        Talk("", "*The guards begin to circle Riku and one quickly chains him down* Riku yells, 'I know the Emperor you fools you're falling into one of those godless fool's tricks!' ");
        done = true;
        break;
    }




       speed_counter--;
    }

map.RenderScene(200,0);
blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
clear_bitmap(Display.GetSurface());
  }while(!done);

clear(screen);
rest(1000);
player[0].UpdateMovement();
}


CCine::CCine()
{
  seen.resize(20);
  for(int i = 0; i<20; ++i)
   seen[i] = false;
}


/* Cine template
void Cinematic1(int id)
{
   //map.LoadMap("./data/battle.map");
   bool done = false;

  while(!done)
  {
    while (speed_counter > 0)
    {

       speed_counter--;
    }


    map.RenderScene(0,0);
  }
}
*/


void Cinematic1(int id)
{
   map.LoadMap("./data/blackhawk.map", true);
   map.LoadNpcs();
   bool done = false;

   int counter = 0;
   clear(screen);
   textprintf(screen, font, screen->w/2-sizeof("Day 1...")*10, screen->h/2, makecol(255,0,0), "Day 1... The Blackhawks");
//player[0].UpdateMovement();
player[0].x = -1;
player[0].y = -1;
   rest(2000);
   speed_counter = 0;
  do
  {

    while (speed_counter > 0)
    {

    map.RenderScene(0,0);
    
    if(counter<100)
    {
      map.npc[0].Move(map, EAST);
      map.npc[0].UpdateMovement();
     counter++;
    }
    
    else if(counter >= 100)
    {
        Talk(map.npcinfo[map.npc[0].id-1].name, "Javez, you'll be safe here... The Emperor has agreed to keep all sponsored"
        " mercenary houses under his protection...       Now I want you to go outside and practice with Robert... He'll be by"
        " the arena towards the southern part of the field. Have some fun and come back to me when you're done!");
        done = true;
        break;
    }




       speed_counter--;
    }

map.RenderScene(0,0);
blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
clear_bitmap(Display.GetSurface());
  }while(!done);

clear(screen);
rest(1000);
Cinematic2(2);
}


void Cinematic2(int id)
{
   map.LoadMap("./data/eviltemple.map");
   map.LoadNpcs();
   
  bool done = false;
  

  textprintf(screen, font, screen->w/2-sizeof("Citadel de L'Thantil in Thantil")*10+35, screen->h/2, makecol(255,0,0), "Citadel de L'Thantil in Thantil");
  readkey();
  
  int counter = 0;
  speed_counter = -1;
  clear(screen);
  while(!done)
  {
    while (speed_counter > 0)
    {
       if(counter > 60)
       {
         Talk("Thantillian Commander", "I've heard that the pissants have given up Nordor...");
         Talk("Thantillian Soldier", "Aye sir...");
         rest(100);
         Talk("Thantillian Commander", "Good. Send a few of yours to stir up the pot anyway...");
         Talk("Thantillian Soldier", "Milord we've sustained multiple casualties and gauging whether we are up to the task is difficult...");
         Talk("Thantillian Commander", "I've heard about the shortages. Do not worry about it corporal, I've made arrangements to round able-bodied men from the merc camps.");
         Talk("Thantillian Soldier", "The treaty milord?");
         Talk("Thantillian Commander", "The treaty has been abolished, you're free to do as you please in unprotected regions.");
         done = true;
         break;
       }
       counter++;
       speed_counter--;
    }



    map.RenderScene(0,0);
    blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
    clear_bitmap(Display.GetSurface());
  }


/*
  if(  map.CheckBattle() ) battle.Battle(false);
  else
  {
    map.LoadMap("./data/town.map");
    map.LoadNpcs();
    player[0].x = map.player_start_x;
    player[0].y = map.player_start_y;
  }
 */
 
   map.LoadMap("./data/blackhawk.map");
   map.LoadNpcs();
   map.SetPositions();
}


void ThantilCine(int id)
{
   map.LoadMap("./data/castle.map");
   map.LoadNpcs();
   map.SetPositions();
   REMOVE_BIT(quest.quest_bit, QUEST_RECRUIT_FEMALE);
int cam_x = 25;
int cam_y = 12;
int cam_y_offset = 0;

int killed_teliana = 0;

  int done = false;
  int counter = 0;
  speed_counter = -1;
  clear(screen);
  while(!done)
  {
    while (speed_counter > 0)
    {
       counter ++;
       if(counter == 60)
       {
        Talk("Commander Hildoft", "The southern patrols have been damaged badly, although, the northern patrols are doing quite well...");
        Talk("Emperor Thantil", "I still want the goblins sent from Thizhil as soon as possible, I don't want to take any risks...");

       }
       if(counter > 65)
       {
         if(cam_y_offset<=800) cam_y_offset++;
         if(cam_y_offset>=800)
         {

          if(killed_teliana)
          {
             rest(500);
             Talk("", "As you see Teliana lying on the floor charred, you begin to chant the spell that Dolfgan taught you, a bunch of tiny fireballs lash out at the nearby Imperalists, killing many of them! You must kill Thantil!");
             map.LoadMap("./data/castlebattle.map");
             done = true;
          }

          if(!killed_teliana)
          {
             Talk("Emperor Thantil", "**Booming from the garden** It would seem that we have company commander, allow me to take care of them! **An electrical charge seems to build in the room and a huge fireball comes flaming towards Teliana, instantly killing her!** Ah, it would seem that this lad has a bit of magic in him, kill him boys!");
             clear(Display.GetSurface());
             rest(200);
             map.LoadMap("./data/castleteliana.map");
             killed_teliana = true;
          }
          

         }
       }
       
       speed_counter--;
    }



    map.RenderScene(cam_x*16,cam_y*16+cam_y_offset);
    blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
    clear_bitmap(Display.GetSurface());
  }


  if(  map.CheckBattle() ) battle.Battle(false);
  player[0].x = map.player_start_x;
  player[0].y = map.player_start_y;
  
  map.LoadMap("./data/castleend.MAP", false);
      map.RenderScene(cam_x*16,cam_y*16);
      blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
  Talk("Thantil", "Heh, you really thought you could kill me? You've much to learn pissant... You've wasted far too much of my time! You have little magical skill, boy... Perhaps your wizardry can get you out of the wastelands... Have fun getting fucked by the sandworms...");
  
  map.LoadMap("./data/wastelands.MAP", false);
      map.RenderScene(0,0);
      blit(Display.GetSurface(), screen, 0, 0, 0, 0, screen->w, screen->h);
  Talk("", "And so it were, stranded in the middle of nothingness...");
  player[0].x = 16*6;
  player[0].y = 16*6;
  BITMAP *endbitmap = load_bitmap("./data/theend.tga", NULL);

  blit( endbitmap, screen, 0, 0,
          0, 0, endbitmap->w, endbitmap->h);
          
  readkey();
  readkey();
  
  allegro_exit();
  exit(0);
  
  
/*
  if(  map.CheckBattle() ) battle.Battle(false);
  else
  {
    map.LoadMap("./data/town.map");
    map.LoadNpcs();
    player[0].x = map.player_start_x;
    player[0].y = map.player_start_y;
  }
 */

}


void CCine::BeginCine(int id)
{

if(seen[id] == false)
{
  switch(id)
  {
    case 1:
         Cinematic1(id);
         seen[1] = true;
         break;
    case 2:
         Cinematic2(id);
         seen[2] = true;
         break;
    case 3:
         CinematicPlant(id);
         seen[3] = true;
         break;
    case 4:
        REMOVE_BIT(quest.quest_bit, QUEST_ENTERED_SUMMERVALE_CAVE);
        CinematicSummervaleCave(id);
        break;
    case 5:
         ThantilCine(id);
    default:
            break;
  }
}



}

void FinalizeCine()
{

}
