#include "gamescene.h"
#include "gameprofile.h"
#include "gamestock.h"
#include "gamestat.h"
#include "gamebonus.h"
#include "gametools.h"
#include "gamesound.h"

StatInfo::StatInfo()
{
  buttonOk = QRect(450,600,300,50);
  buttonCancel = QRect(100,600,300,50);

  buttons << buttonOk << buttonCancel;
}

void GameScene::showStatictics(STAT_CODE code)
{
  stat->code = code;
  stat_active = true;

  advanceTimer->start();
  enableInput();

  if (code == STAT_LEVEL_WON || code == STAT_GAME_WON)
  {
    QTimer::singleShot(1000, this, SLOT(bonusTimeLeft()));

    gameProfile->currentPlayer()->localScore += time * BONUS_FOR_SECOND;
    gameProfile->currentPlayer()->score += time * BONUS_FOR_SECOND;
    gameProfile->currentPlayer()->next_tool =
        toolset->activeToolIndex(gameProfile->currentPlayer()->score);
    return;
  }
}

void GameScene::bonusTimeLeft()
{
  if (!stat_active)
    return;

  score += BONUS_FOR_SECOND;
  toolset->addScore(BONUS_FOR_SECOND);

  sndEngine->playSound(GameSound::sndBonus);

  if (time > 0) {
    --time;
    QTimer::singleShot(10, this, SLOT(bonusTimeLeft()));
  }
}

void GameScene::checkStatMouseActions(const QPoint &pos)
{
  if (stat->buttonCancel.contains(pos))
  {
    exitToMainMenu();
  }
  else
  if (stat->buttonOk.contains(pos))
  {
    runNextLevel();
  }
}

void GameScene::drawStatInfo(QPainter &p)
{
  p.setOpacity(0.9);
  p.setPen(QPen(Qt::white, 2));
  p.setBrush(Qt::black);
#if QT_VERSION >= 0x040400
  p.drawRoundedRect(0+1,0+1,860-1,700-1, 5,5);
#else
  p.drawRect(0+1,0+1,860-1,700-1);
#endif

  p.setOpacity(1);

  p.setFont(gameStock->Font40);

  if (stat->code == STAT_LEVEL_WON)
  {
    p.drawText(0,0,860,80, Qt::AlignHCenter | Qt::AlignBottom,
               QString("Level %1 complete!").arg(level));

    p.setFont(gameStock->Font20);

    const int x1 = 200, x2 = 300, x3 = 600;
    const int y1 = 200, y2 = 300, y3 = 400;

    p.drawPixmap(x1,y1, gameStock->Clock);
    p.drawText(x3,y1+30, QString("%1:%2").arg(time/60, 2, 10, QChar('0')).arg(time%60, 2, 10, QChar('0')));

    p.drawPixmap(x1,y2, gameStock->Score);
    p.drawText(x3,y2+30, QString::number(/* stat->level_score */ score));

    p.drawPixmap(x1,y3, gameStock->Items.at(0));
    p.drawText(x3,y3+30, QString::number(gameBonus->collectedOnLevel()));

    p.setPen(QPen(Qt::yellow));
    p.drawText(x2,y1+30, "Time left:");
    p.drawText(x2,y2+30, "Score:");
    p.drawText(x2,y3+30, "Items collected:");
  }
  else if (stat->code == STAT_LEVEL_FAIL) // level failed
  {
    p.drawText(0,0,860,80, Qt::AlignHCenter | Qt::AlignBottom,
               QString("Level %1 failed!").arg(level));

    int percent = 0;
    if (stat->level_targets_all > 0)
        percent = (stat->level_targets_all - stat->level_targets_left)*100/stat->level_targets_all;
    p.setFont(gameStock->Font20);
    p.drawText(0,300,860,50, Qt::AlignCenter,
               QString("You have completed %1% of level %2.").arg(percent).arg(level));
    p.drawText(0,350,860,50, Qt::AlignCenter,
               QString("Next time you'll be more lucky!"));
  }
  else { // game won
    p.drawText(0,0,860,80, Qt::AlignHCenter | Qt::AlignBottom,
               QString("Congratulations!"));

    p.setFont(gameStock->Font20);

    p.drawText(0,100,860,50, Qt::AlignCenter,
               QString("You have completed all the levels and won the game!"));

    const int x1 = 200, x2 = 300, x3 = 600;
    const int y2 = 300, y3 = 400;

    p.drawPixmap(x1,y2, gameStock->Score);
    p.drawText(x3,y2+30, QString::number(score));

    p.drawPixmap(x1,y3, gameStock->Items.at(0));
    p.drawText(x3,y3+30, QString::number(gameBonus->collectedTotal()));

    p.setPen(QPen(Qt::yellow));
    p.drawText(x2,y2+30, "Total score:");
    p.drawText(x2,y3+30, "Items collected:");
  }

  // ok button
  if (stat->code != STAT_GAME_WON) {
    p.setOpacity(stat->buttonOk.contains(cursorPosition()) ? 0.3 : 0.1);
    p.setPen(QPen(Qt::white, 2));
    p.setBrush(Qt::darkGray);

    #if QT_VERSION >= 0x040400
      p.drawRoundedRect(stat->buttonOk, 5,5);
    #else
      p.drawRect(stat->buttonOk);
    #endif

    p.setOpacity(1);
    p.setPen(QPen(Qt::white));
    p.setFont(gameStock->Font20);
    p.drawText(stat->buttonOk, Qt::AlignCenter, stat->code == STAT_LEVEL_WON ? "Continue" : "Try Again");
  }

  // cancel button
  stat->buttonCancel = (stat->code == STAT_GAME_WON) ? QRect(280,600,300,50) : QRect(100,600,300,50);

  p.setOpacity(stat->buttonCancel.contains(cursorPosition()) ? 0.3 : 0.1);
  p.setPen(QPen(Qt::white, 2));
  p.setBrush(Qt::darkGray);

  #if QT_VERSION >= 0x040400
    p.drawRoundedRect(stat->buttonCancel, 5,5);
  #else
    p.drawRect(stat->buttonCancel);
  #endif

  p.setOpacity(1);
  p.setPen(QPen(Qt::white));
  p.setFont(gameStock->Font20);
  p.drawText(stat->buttonCancel, Qt::AlignCenter, "Exit to Menu");
}

void GameScene::drawFinalInfo(QPainter &p)
{
  static int logo_x = 100, logo_y = 100;
  static int logo_dx = 5, logo_dy = 5;

  p.setOpacity(1);

  p.drawPixmap(0,0, gameStock->FinalBackground);

  p.setOpacity(0.3);

  p.drawPixmap(logo_x, logo_y, gameStock->GameLogoBig);

  if (logo_x <= 0 || logo_x >= 720) logo_dx = -logo_dx;
  if (logo_y <= 0 || logo_y >= 660) logo_dy = -logo_dy;

  logo_x += logo_dx;
  logo_y += logo_dy;

  p.setOpacity(1);

  p.setPen(QPen(Qt::darkRed, 2));
  p.setFont(gameStock->Font40);

  p.drawText(0,0,1024,768, Qt::AlignCenter,
             "CONGRATULATIONS"
             "\n\n"
             "You are Winner!"
             );
}
