#include "config.h"

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"

#include "Engine.h"
#include "Timer.h"
#include "Player.h"
#include "TextBox.h"

typedef struct room Room;
typedef struct kaos Kaos;
typedef struct scene Scene;
typedef struct hyperKaos HyperKaos;

#include "extern.h"

TextBox* newTextBox()
{
  TextBox* self = malloc(sizeof(TextBox));
  self->portrait = NULL;
  self->message = malloc(4*sizeof(SDL_Surface*));
  self->lines = 0;
  self->cursor = 0;
  self->scroll = 0;
  self->scrollFrom = 0;
  return self;
}

TextBox* newGTextBox(SDL_Surface* image)
{
  TextBox* self = malloc(sizeof(TextBox));
  self->portrait = image;
  self->message = malloc(4*sizeof(SDL_Surface*));
  self->lines = 0;
  self->cursor = 0;
  self->scroll = 0;
  self->scrollFrom = 0;
  return self;
}

void deleteTextBox(TextBox* target)
{
  int i;
  if (target->portrait) SDL_FreeSurface(target->portrait);
  for (i = 0; i < target->lines; i++)
  {
    SDL_FreeSurface(target->message[i]);
  }
  free(target->message);
}

void addText(TextBox* self, char* text)
{
  int i;
  if ( self->lines != 0 && self->lines%4 == 0)
  {
    SDL_Surface** templines = (SDL_Surface**)malloc((self->lines+4)*sizeof(SDL_Surface*));
    for (i = 0; i < self->lines; i++)
      templines[i] = self->message[i];
    free(self->message);
    self->message = templines;
    templines = NULL;
  }
  self->message[self->lines] = TTF_RenderText_Solid(font, text, textColor);
  self->lines++;
}

void textBoxInput(TextBox* self, int* textIsRelevent)
{
    if (self->cursor != 17)
    {
      self->cursor++;
      while (SDL_PollEvent(&event))
      {
        switch (event.type)
        {
          case SDL_QUIT: quit=1; playing = 0; *textIsRelevent = 0; break;
          case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
            {
              case A_BUTTON:
                if (self->cursor < 15) self->cursor += 3;
                break;
              case FS_BUTTON:
                toggleFullscreen();
                break;
              default: break;
            }
        }
      }
    }


    if (self->cursor == 17 && self->scroll+1 != self->lines && (self->scroll+1)%4 != 0)
    {
      self->cursor = 0;
      self->scroll++;
    }
    if (self->cursor == 17 && ((self->scroll+1)%4 == 0 || self->scroll+1 == self->lines))
    {
      applySurface(274, 120, nextArrow, screen, NULL);
      while (SDL_PollEvent(&event))
      {
        switch (event.type)
        {
          case SDL_QUIT: quit = 1; playing = 0; *textIsRelevent = 0; break;
          case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
            {
              case A_BUTTON:
                if (self->scroll + 1 != self->lines)
                {
                  self->cursor = 0;
                  self->scroll++;
                  self->scrollFrom += 4;
                }
                else *textIsRelevent = 0;
                break;
              case FS_BUTTON:
                  toggleFullscreen();
                break;

            default: break;
          }
        }
      }
    }
}

void displayTextBox(TextBox* self)
{
  int textIsRelevent = 1;
  int offset = -32;
  int i;
  SDL_Rect textScroller = {0, 0, 0, 14};
  actionbutton = 0;
  while (textIsRelevent)
  {
    timeStart(fps);
    renderBackground();
    renderForeground();

    applySurface(22, 51, textBoxBG, screen, NULL);
    if (self->portrait)
    {
      applySurface(32, 58, self->portrait, screen, NULL);
      offset = 0;
    }

    for (i = self->scrollFrom; i < self->scroll; i++)
    {
      applySurface(106 + offset, 64+(14*(i%4)), self->message[i], screen, NULL);
    }

    textScroller.w = self->cursor*10;
    applySurface(106 + offset, 64+(14*(self->scroll%4)), self->message[self->scroll], screen, &textScroller);

    textBoxInput(self, &textIsRelevent);

    SDL_Flip(screen);
    timeDilation();

  }
  self->scroll = 0;
  self->cursor = 0;
  self->scrollFrom = 0;
}