#include "mainengine.h"
#include <SDL/SDL.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "sound_commands.h"

/* TODO (Ne0v001#2#): Finish sound FX in graphic stuff. */
/* TODO (Ne0v001#3#): Create engine for interface. */

using std::ifstream;

CEMain::CEMain(SDL_Surface * scre, int videox, int videoy, int bitdepth)
{
  back = scre;
  //make sure SDL inits, else exit
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  
  //open up video mode, to being with all running
  back = SDL_SetVideoMode(videox,videoy,bitdepth,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);
  if ( back == NULL )
  {
    printf("Unable to set %ix%i video: %s\n", videox, videoy,SDL_GetError());
    exit(1);
  }
  
  //disable cursor (might remove later, if cursor is used)
  SDL_ShowCursor(0);

  //comment back in when needed
  //"Loading" screen <.< >.>
  //background = ImageLoad("data\\loading.bmp");
  //DrawIMG(background, 0, 0);  
  
  //<<testing>>
  LoadMap("data/credits.lmao");
  //<<end testing>>
  
  //pause for a moment
  SDL_Delay(1000);
  
  //start up main loop
  Loop();
}

//main loop of the program
void CEMain::Loop()
{
  int done = 0;
  while(done == 0)
  {
  //keep track of any event made by SDL
    SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }

      if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
      }
    }
    
    //used for determining if a key is pressed
    keys = SDL_GetKeyState(NULL);
    
    //make scene drawing part here
    BackGround();
    
    //any key functions, put here
    BlockDraw();
    
    //flip the screen
    SDL_Flip(back);
  }
  //free surfaces
  SDL_FreeSurface(background);
  SDL_FreeSurface(back);
  //shut off all music
  MusicOff();
}

//for loading image files
SDL_Surface * CEMain::ImageLoad(char * file)
{
  SDL_Surface *temp1, *temp2;
  temp1 = SDL_LoadBMP(file);
  temp2 = SDL_DisplayFormat(temp1);
  SDL_FreeSurface(temp1);
  return temp2;
}

//drawing the image onto the screen
void CEMain::DrawIMG(SDL_Surface *img, int x, int y)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, back, &dest);
}

//loading up the map file
void CEMain::LoadMap(char * filename)
{
  //input stream from file
  file.open(filename);
  
  //acquire background
  char bg[512];
  file >> bg;
  //put as background <.<
  background = ImageLoad(bg);
  
  //getting name of sprite file
  file >> bg;
  ParseSpriteFile(bg);
  
  //getting name of music file
  file >> bg;
  ParseMusicFile(bg);
  
  //parse the coordinates for where the sprites go
  file >> bg;
  ParseMapCoord(bg);
  
  //start ambient music
  PlayMusic(SFX.ambient);
}

//draws the background
void CEMain::BackGround()
{
  DrawIMG(background, 0, 0);
}

//parses and loads sprite file list
void CEMain::ParseSpriteFile(char * filename)
{
  printf("Parsing sprite file...\n");
  //open up file for reading
  sprfile.open(filename);
  //initialize all bools to false
  for (int i=0; i < MAXSPRITE; i++) { SPRITE.spriteb[i][0] = false; }
  
  int loop = 0;
  //read file up O_o o_O
  while(!sprfile.eof())
  {
    sprfile >> SPRITE.sprites[loop];
    //debugging stuff
    printf("Value of SPRITE.sprites[%i] is: %s\n",loop,SPRITE.sprites[loop]);
    SPRITE.spriteb[loop][0] = true;
    loop++;
  }
  //write loop amount
  printf("variable loop in function CEMain::ParseSpriteFile looped %i time(s)...\n", loop);
  
  //make space for all of the textures
  SPRITE.SPIC = new SpriteP[loop];
  
  //load up all of the textures
  for (int i=0; i < loop; i++)
  {
    SPRITE.SPIC[i].sprite = ImageLoad(SPRITE.sprites[i]);
  }
  
  sprfile.close(); //close file
}

//parses and loads music file list
void CEMain::ParseMusicFile(char * filename)
{
  printf("Parsing music file...\n");
  //open up file for reading
  muzfile.open(filename);
  
  //initialize to false
  for (int i = 0; i < 5; i++) { SFX.music[i][0] = false; } 
  
  while(true)
  {
    while(!muzfile.eof())
    {
      muzfile >> bg;
      printf("variable _bg_: %s\n",bg);
        //ambient music
        if (SFX.music[0][0] == false)
        {
          muzfile >> bg;
          strcpy(SFX.ambient,bg);
          printf("Ambient: %s\n",bg);
          SFX.music[0][0] = true; //set file variable to true
          break; //head back to beginning
        }
        //press up in menu
        if (SFX.music[1][0] == false)
        {
          muzfile >> bg;
          strcpy(SFX.menup,bg);
          printf("Menu Up: %s\n",bg);
          SFX.music[1][0] = true;
          break;
        }
        //press down in menu
        if (SFX.music[2][0] == false)
        {
          muzfile >> bg;
          strcpy(SFX.mendown,bg);
          printf("Menu Down: %s\n",bg);
          SFX.music[2][0] = true;
          break;
        }
        //select something in menu
        if (SFX.music[3][0] == false)
        {
          muzfile >> bg;
          strcpy(SFX.mensel,bg);
          printf("Menu Select: %s\n",bg);
          SFX.music[3][0] = true;
          break;
        }
        //cancel/go up a level in menu
        if (SFX.music[4][0] == false)
        {
          muzfile >> bg;
          strcpy(SFX.mencan,bg);
          printf("Menu Cancel: %s\n",bg);
          SFX.music[4][0] = true;
        }
        break; //finish inner loop
     }
     
     if(SFX.music[4][0] == true)
     {
       break; //finish outer loop
     }
  }
  //close file stream
  muzfile.close();
  printf("Music file parsed...\n");
}

//parsing the locations for sprites
void CEMain::ParseMapCoord(char * filename)
{
  //coordinate file format:
  //# of blocks
  //(texture used) (X pos) (Y pos) (collide or not [background/foreground])
  //...

  //open coordinate file stream
  coorfile.open(filename);
  
  //debugging
  printf("Starting coordinate parsing...\n");
  
  //check for how many "blocks" there are
  coorfile >> BLOCK.BlockNum;
  
  //make storage space for blockinfo
  BLOCK.blockinfo = new BlockInfo[BLOCK.BlockNum];
  
  //run a loop to see what block uses what texture,
  //and check if collision detection should be used
  for (int i = 0; i < BLOCK.BlockNum; i++)
  {
    //read variables in
    coorfile >> BLOCK.blockinfo[i].tex >> BLOCK.blockinfo[i].x >>
      BLOCK.blockinfo[i].y >> BLOCK.blockinfo[i].collision >>
      BLOCK.blockinfo[i].w >> BLOCK.blockinfo[i].h;
  }
  
  //close file stream
  coorfile.close();
  
  //debugging
  printf("Finished coordinate parsing...\n");
}

//draws all blocks onscreen
void CEMain::BlockDraw()
{     
  //iterate through all files and display
  for (int i = 0; i < BLOCK.BlockNum; i++)
  {
    //draw the block
    DrawIMG(SPRITE.SPIC[i].sprite, BLOCK.blockinfo[i].x, BLOCK.blockinfo[i].y);
  }
}