#include "CMap.hpp"
#include "CBattle.hpp"
#include "Interaction.hpp"
#include "Quests.hpp"
#include "CCine.hpp"

CQuests quest;

CQuests::CQuests()
{
  quest_bit = 0;
}

CEvent event;

void CQuests::ProcessQuest(int qb)
{
  bool flag1 = 0;
  bool found = false;
  int store = 0;
  bool flag2 = 0;

  switch(qb)
  {
    case QUEST_FOUGHT_ROBERT:
            SET_BIT(quest_bit, QUEST_FOUGHT_ROBERT);
            map.LoadMap("./data/blackhawk_battle.map");
            battle.Battle(false);
//            map.LoadMap("./data/blackhawk_outside.map");
            // start battle here!
            break;
            
    case QUEST_GET_PACKAGE:
          Talk("Wise old man", "Don't you worry lad, nobody has ever won against Robert, the first time at least. Now I've got an important task for you: I need you to go to the village south of here, SouthGlade and retrieve a package for me, you'll have to serach for the OLD HAG that has the package but it shouldn't be too hard... Go ahead and explore!");
          SET_BIT(quest.quest_bit, QUEST_GET_PACKAGE);
          map.LoadMap("./data/worldmap.map");
          map.SetPositions();
          player[0].UpdateMovement();
          break;

    case QUEST_GOT_PACKAGE:
       if(!CHECK_BIT(quest_bit, QUEST_KILLED_OLDMAN))
       {
           SET_BIT(map.tile[1][65][83].flags, FLAG_EVENT);
           map.tile[1][65][83].gfx = TILE_ITEM;
           event.x = 83;
           event.y = 65;
           event.flags = EVENT_ITEM;
           event.id = 2;
           map.event.push_back(event);
           map.max_event++;
           
           SET_BIT(quest_bit, QUEST_KILLED_OLDMAN);
           SET_BIT(quest_bit, QUEST_HAS_BOX);
      }
          break;
          
    case QUEST_CLEARED_SOUTHGLADE_CAVE:
    if( !CHECK_BIT(quest_bit, QUEST_CLEARED_SOUTHGLADE_CAVE ) )
    {
         for(int i = 0; i<player[0].max_battles; ++i)
         {
             if( player[0].fought_battles[i] == "cave_battle.map")
                 flag1 = true;
             if( player[0].fought_battles[i] == "cave_battle2.map")
                 flag2 = true;
         }
         
         if(flag1 == true && flag2 == true)
         {
          Talk("", "You cleared the goblins out of the cave! You gain 15 exp and 2 gold!");
          player[0].gold += 2;
          player[0].exp += 15;

          if(CHECK_BIT(quest_bit, QUEST_RECRUIT_FEMALE) )
            player[1].exp +=15;
            
          SET_BIT(quest_bit, QUEST_CLEARED_SOUTHGLADE_CAVE );
         }
    }
         break;
         
    case QUEST_PLANTED_HERB:
     if(CHECK_BIT(quest_bit, QUEST_NEED_TO_PLANT_HERB) )
     {
         cine.BeginCine(3);
         Talk("Riku Santorium","You terrorists will not get away with this! I know the Emperor!");
         SET_BIT(quest_bit, QUEST_PLANTED_HERB);
         REMOVE_BIT(quest_bit, QUEST_NEED_TO_PLANT_HERB);
         player[0].exp+=8;
         Talk("", "You gained 8 experience points!");
     }
         break;
         
    case QUEST_RECEIVED_HELM:
      if(!CHECK_BIT(quest_bit, QUEST_RECEIVED_HELM))
      {
         Talk("", "You received an item!");

         SET_BIT(map.tile[1][player[0].y/map.sizeh][player[0].x/map.sizew].flags, FLAG_EVENT);
         map.tile[1][player[0].y/map.sizeh][player[0].x/map.sizew].gfx = TILE_ITEM;
         event.x = player[0].x/map.sizew;
         event.y = player[0].y/map.sizeh;
         event.flags = EVENT_ITEM;
         event.id = 3;
         map.event.push_back(event);
         map.max_event++;

         SET_BIT(quest_bit, QUEST_RECEIVED_HELM);
      }
         break;
         
    case QUEST_NORDORCAVE_OPEN:
         if(map.event[0].read == true && map.event[1].read == false  && map.event[2].read == true)
         {
           Talk("", "Towards the south you hear a loud grinding noise.");
           map.tile[0][73][24].gfx = map.tile[0][72][24].gfx;
           map.tile[0][73][25].gfx = map.tile[0][72][25].gfx;
           map.tile[0][73][24].flags = 0;
           map.tile[0][73][25].flags = 0;
           SET_BIT(quest_bit, QUEST_NORDORCAVE_OPEN);
         }
         break;
         
    case QUEST_TALKED_ABOUT_HERB:
         if(!CHECK_BIT(quest_bit, QUEST_TALKED_ABOUT_HERB))
         {
           Talk("Vlad Jatoole", "*As you approach this man a group of other men surround you* Greetings friend, I am Vlad Jatoole, one of the terrorists if you will, I have some information that might be of use to you, friend. But before I do bestow this knowledge upon thee, I need you to perform a task for me - to make sure you are not one of the Imperalists... I need you to go to Nordor and plant that lovely little herb you have from the Blackhawk package on a certain Imperalist preparing to give a speech... When you complete this task come back to me... and don't waste your time telling the Imperalists, for they will slice your little throat open as soon as we are discovered...");
           SET_BIT(quest_bit, QUEST_TALKED_ABOUT_HERB);
           SET_BIT(quest_bit, QUEST_NEED_TO_PLANT_HERB);
           SET_BIT(quest_bit, QUEST_ENTERED_SUMMERVALE_CAVE);
         }
         
         else if(CHECK_BIT(quest_bit, QUEST_TALKED_ABOUT_HERB) && CHECK_BIT(quest_bit, QUEST_NEED_TO_PLANT_HERB) )
         {
              Talk("Vlad Jatoole", "Friend, we need you to plant that herb.. it's the only way. Go to Nordor and find the man giving the speech.");
         }
         
         else if(CHECK_BIT(quest_bit, QUEST_TALKED_ABOUT_HERB) && CHECK_BIT(quest_bit, QUEST_PLANTED_HERB) )
         {
              Talk("Vlad Jatoole", "My thanks friend, now you may pass into the underground and discover what it is that you seek... BUT be careful for there are several unfriendly creatures there, be prepared for a fight... Find Tridor and all shall be revealed.");
         }
         break;
         
    case HANDLE_SUMMERVALE_CAVE:
         /* I changed my mind.. don't show the NPCs just make it seem random to the player!
         for(int i = 0; i<player[0].max_battles; ++i)
         {
             if(player[0].fought_battles[i] == "sum_battle1.map")
             {
                map.npc[0].x = 99*64;
                map.npc[0].y = 0*64;
             }
             
             if(player[0].fought_battles[i] == "sum_battle2.map")
             {
                map.npc[1].x = 99*64;
                map.npc[1].y = 0*64;
             }
             
             if(player[0].fought_battles[i] == "sum_extrabattle.map")
             {
                map.npc[2].x = 99*64;
                map.npc[2].y = 0*64;
             }

         }
         */
         break;
         
    case TALKED_TO_ORC_LEADER:
         SET_BIT(quest_bit, TALKED_TO_ORC_LEADER);
         SET_BIT(quest_bit, QUEST_RECRUIT_FEMALE);
         break;
         
    case PORTAL_NEARORC:
         map.LoadMap("./data/tanya_battle.map");
         map.LoadNpcs();
         player[0].cam.x = 0;
         player[0].cam.y = 0;
         map.SetPositions();

        found = false;
        for(int i = 0; i<player[0].max_battles; ++i)
        {
          if(player[0].fought_battles[i] == "tanya_battle.map")
           found = true;
           
          if(player[0].fought_battles[i] == "empty")
          {
           store = i;
           break;
          }
        }


        if(!found)
        {
         battle.Battle(true);
         player[0].fought_battles[store] = "tanya_battle.map";
        }


         map.LoadMap("./data/tanya.map");
         map.LoadNpcs();
         player[0].cam.x = 0;
         player[0].cam.y = 0;
         map.SetPositions();
         player[0].UpdateMovement();
         
         SET_BIT(quest_bit, CAN_ENTER_ORC_VILLAGE);
         break;
         
    case TELEPORT_TO_TRIDOR:
         map.LoadMap("./data/worldmap.map");
         map.LoadNpcs();
         player[0].cam.x = 0;
         player[0].cam.y = 0;
         map.SetPositions();
         player[0].x = 72*16;
         player[0].y = 7*16;
         player[0].UpdateMovement();
         break;
         
         
    case TELEPORT_TO_CLOUD_CITY:
         map.LoadMap("./data/cloudcity.map");
         map.LoadNpcs();
         player[0].cam.x = 0;
         player[0].cam.y = 0;
         map.SetPositions();
         player[0].UpdateMovement();
         break;
         
    case HEAL_PLAYERS:
         player[0].hp=player[0].max_hp;
         player[1].hp=player[1].max_hp;
         
         break;
         
    case TELEPORT_TO_ORC_VILLAGE:
         map.LoadMap("./data/orcvillage.map");
         map.LoadNpcs();
         player[0].cam.x = 0;
         player[0].cam.y = 0;
         map.SetPositions();
         player[0].UpdateMovement();
         break;
         
    case KILLED_HARPY:
         SET_BIT(quest_bit, KILLED_HARPY);
         
         
    default:
            break;
  }
}
