#include <stdio.h>

#include "config.h"

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>

#include "Scene.h"
#include "Timer.h"
#include "Engine.h"

typedef struct kaos Kaos;
typedef struct textBox TextBox;
typedef struct room Room;
typedef struct player Player;
typedef struct hyperKaos HyperKaos;
#include "extern.h"

SLayer* newSLayer(SDL_Surface* img, int x, int y, int h, int v, int alpha)
{
  SLayer* self = malloc(sizeof(SLayer));

  self->sprite = img;
  self->x = x;
  self->y = y;
  self->h = h;
  self->v = v;

  self->oX = x;
  self->oY = y;

  if (alpha != 255)
    SDL_SetAlpha(self->sprite, SDL_SRCALPHA|SDL_RLEACCEL, alpha);

  return self;
}

void deleteSLayer(SLayer* target)
{
  SDL_FreeSurface(target->sprite);
  free(target);
}

Scene* newScene(int in, int out, int time, SDL_Color incolor, SDL_Color outcolor)
{
  Scene* self = malloc(sizeof(Scene));

  self->sprites = NULL;
  self->time = time;
  self->nSprites = 0;
  self->fade = (Transition){in, incolor, out, outcolor};

  return self;
}

void buildScene(Scene* self, SLayer* sprite)
{
  int i;
  SLayer** temp;
  if (self->nSprites)
    temp = self->sprites;
  self->sprites = malloc((self->nSprites+1)*sizeof(SLayer*));
  if (self->nSprites)
  {
    for (i = 0; i < self->nSprites; i++)
      self->sprites[i] = temp[i];
  }
  self->sprites[self->nSprites] = sprite;
  self->nSprites++;
}

void sceneInput(Scene* self, int* counter)
{
  while (SDL_PollEvent(&event))
  {
    switch (event.type)
    {
      case SDL_KEYDOWN:
        switch (event.key.keysym.sym)
        {
          case A_BUTTON:
            *counter = self->time - 1;
            break;
          case FS_BUTTON:
            toggleFullscreen();
            break;
          default: break;
        }
        break;
      case SDL_QUIT:
        if (playing)
        {
          playing = 0;
          quit = 1;
        }
        else
        {
          *counter = self->time - 1;
          playing = 0;
          quit = 1;
        }
        break;
    }
  }
}

void playScene(Scene* self)
{
  int i, j;
  int alphamod;
  Uint32 icolor, ocolor;
  SDL_Surface* fadeIn, *fadeOut;

  fadeIn = loadImage("assets/img/backgrounds/blueroom.png");
  fadeOut = fadeIn;

  icolor = SDL_MapRGB(fadeIn->format, self->fade.incolor.r, self->fade.incolor.g, self->fade.incolor.b);
  ocolor = SDL_MapRGB(fadeIn->format, self->fade.outcolor.r, self->fade.outcolor.g, self->fade.outcolor.b);

  SDL_FillRect(fadeIn, NULL, icolor);
  SDL_FillRect(fadeOut, NULL, ocolor);


  for (i = 0; i < self->time; i++)
  {
    timeStart(fps);
    sceneInput(self, &i);
    for (j = 0; j < self->nSprites; j++)
    {
      applySurface(self->sprites[j]->x, self->sprites[j]->y, self->sprites[j]->sprite, screen, NULL);
      self->sprites[j]->x += self->sprites[j]->h;
      self->sprites[j]->y += self->sprites[j]->v;
    }
    if (i < self->fade.in)
    {
      alphamod = 255 - (int)(255*((float)i/self->fade.in));
      SDL_SetAlpha(fadeIn, SDL_SRCALPHA|SDL_RLEACCEL, alphamod);
      applySurface(0,0, fadeIn, screen, NULL);
    }
    if (self->time - i < self->fade.out)
    {
      alphamod =  255 - (int)(255*((float)(self->time - i)/self->fade.out));
      SDL_SetAlpha(fadeOut, SDL_SRCALPHA|SDL_RLEACCEL, alphamod);
      applySurface(0,0, fadeOut, screen, NULL);
    }
    SDL_Flip(screen);
    timeDilation();
  }
  for (j = 0; j < self->nSprites; j++)
  {
    self->sprites[j]->x = self->sprites[j]->oX;
    self->sprites[j]->y = self->sprites[j]->oY;
  }
}

void deleteScene(Scene* target)
{
  int i;
  for (i = 0; i < target->nSprites; i++)
  {
    SDL_FreeSurface(target->sprites[i]->sprite);
    free(target->sprites[i]);
  }
  free(target->sprites);
  free(target);
}