#include "gameprofile.h"
#include "gamewidget.h"
#include "gamesound.h"

GameProfile* gameProfile = 0;


void PlayerInfo::init()
{
  score = localScore = 0;
  level = 1;
  lives = 3;

  time_bonus = speed_bonus = score_bonus = 0;

  next_tool = 2;

  for (int i = 0; i < items.count(); i++)
  {
    items[i] = ItemInfo();
  }
}

GameProfile::GameProfile(QObject *parent) : QObject(parent),
  m_accel(false)
{
    userPath = QDir::homePath();
#ifdef Q_OS_WIN32
  userPath += "/Application Data/XlabSoft/JAG/";
#else
  userPath += "/.xlabsoft/jag/";
#endif

    QDir userdir(userPath);
    if (!userdir.exists())
        userdir.mkpath(userPath);

//    setCurrentPlayer("Player");
//    currentPlayer()->theme = "default";

    m_started = false;
    m_paused = false;

    scanThemes();
}

PlayerInfo *GameProfile::currentPlayer()
{
    if (m_current >= 0 && m_current < m_players.count())
        return m_players.at(m_current);
    else
        return new PlayerInfo("");
}

void GameProfile::setCurrentPlayer(const QString &name)
{
    if (name.isEmpty()) return;

    int index = -1;
    for (int i = 0; i < m_players.count(); i++)
        if (m_players.at(i)->name == name) {
            index = i;
            break;
        }
    if (index == -1) {
        m_players.append(new PlayerInfo(name));
        m_current = m_players.count() - 1;
    }
    else
        m_current = index;

    emit profileChanged();
}

void GameProfile::removePlayer(const QString &name)
{
    int index = -1;
    for (int i = 0; i < m_players.count(); i++)
        if (m_players.at(i)->name == name) {
            index = i;
            break;
        }
    if (index >= 0) {
        m_players.removeAt(index);
        if (m_current >= index)
            m_current--;
        while (m_current >= m_players.count())
            m_current--;
    }
}

void GameProfile::renamePlayer(const QString &before, const QString &after)
{
    int index = -1;
    for (int i = 0; i < m_players.count(); i++)
        if (m_players.at(i)->name == before) {
            index = i;
            break;
        }
    if (index >= 0)
        m_players.at(index)->name = after;
}

void GameProfile::saveProfile()
{
  QFile f(userPath + "users.dat");
  if (f.open(QIODevice::WriteOnly)) {
      QDataStream ds(&f);

      // format signature
      ds << QString("USR1");

      // profiles count
      ds << m_players.count();

      // current profile
      ds << m_current;

      for (int i = 0; i < m_players.count(); i++)
      {
          PlayerInfo *pl = m_players.at(i);
          ds << pl->name;
          ds << pl->theme;
          ds << pl->levelpack;
          ds << pl->score;
          ds << pl->localScore;
          ds << pl->level;
          ds << pl->lives;
          ds << pl->time_bonus;
          ds << pl->speed_bonus;
          ds << pl->score_bonus;
          ds << pl->speed_mag_bonus;
          ds << pl->next_tool;
          ds << pl->diff;
          ds << pl->mode;
          ds << pl->m_fullscreen;
          ds << pl->m_windowed;
          ds << pl->soundVolume;
          ds << pl->musicVolume;
          ds << pl->musicEnabled;

          ds << pl->items.count();
          for (int j = 0; j < pl->items.count(); j++)
          {
              ds << pl->items.at(j).totalCount;
              ds << pl->items.at(j).localCount;
          }
      }

      // acceleration
      ds << m_accel;

      f.close();
  }

//  saveTopTen();
}

void GameProfile::loadProfile()
{
    QFile f(userPath + "users.dat");
    if (f.open(QIODevice::ReadOnly))
    {
        QDataStream ds(&f);

        QString sig;
        // format signature
        ds >> sig;
        if (sig == "USR1") {
            int cnt, index;
            // profiles count
            ds >> cnt;

            // current profile
            ds >> m_current;

            for (int i = 0; i < cnt; i++) {
                m_players.append(new PlayerInfo(""));
                index = m_players.count() - 1;
                PlayerInfo *pl = m_players[index];
                ds >> pl->name;
                ds >> pl->theme;
                ds >> pl->levelpack;
                ds >> pl->score;
                ds >> pl->localScore;
                ds >> pl->level;
                ds >> pl->lives;
                ds >> pl->time_bonus;
                ds >> pl->speed_bonus;
                ds >> pl->score_bonus;
                ds >> pl->speed_mag_bonus;
                ds >> pl->next_tool;
                ds >> pl->diff;
                ds >> pl->mode;
                ds >> pl->m_fullscreen;
                ds >> pl->m_windowed;
                ds >> pl->soundVolume;
                ds >> pl->musicVolume;
                ds >> pl->musicEnabled;

                int itemscnt;
                ds >> itemscnt;
                for (int j = 0; j < itemscnt; j++) {
                    ItemInfo iteminfo;
                    ds >> iteminfo.totalCount;
                    ds >> iteminfo.localCount;
                    pl->items.append(iteminfo);
                }
            }
        }

        // acceleration
        ds >> m_accel;

        f.close();
    }

    if (m_current >= 0 && m_current < m_players.count()) {
        sndEngine->setChannelVolume(currentPlayer()->soundVolume);
        sndEngine->setMusicVolume(currentPlayer()->musicVolume);
        sndEngine->enableMusic(currentPlayer()->musicEnabled);
    }

    if (m_players.count() == 0)
        setCurrentPlayer("Player");

//    loadTopTen();
}

void GameProfile::setPlayerNames(const QStringList &sl)
{
    m_players.clear();
    for (int i = 0; i < sl.count(); i++)
        setCurrentPlayer(sl.at(i));
}

QStringList GameProfile::playerNames()
{
    QStringList sl;
    for (int i = 0; i < m_players.count(); i++) {
        sl.append(m_players.at(i)->name);
    }
    return sl;
}

void GameProfile::scanThemes()
{
    m_themes.clear();
    QString path = GameWidget::getResourcePath() + "schemes/";

    QDir dir(path);
    QFileInfoList lst = dir.entryInfoList(QStringList("*"), QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
    for (int i = 0; i < lst.count(); i++) {
        QString name = lst.at(i).canonicalFilePath();
        if (!name.startsWith(".") && lst.at(i).isDir())
            m_themes.append(name.section(path, 1, 1));
    }
}

int GameProfile::levelPackCount(const QString &lp)
{
    QString fname = GameWidget::getResourcePath() + "levels/" + lp + ".lpk";
    int levels = 0;
    QFile f(fname);
    if (f.open(QIODevice::ReadOnly)) {
        QDataStream ds(&f);

        // signature
        QString sig;
        ds >> sig;
        if (sig == "LPK1") {
            // read levels count
            ds >> levels;
        }
        f.close();
    }
    return levels;
}

/*
void GameProfile::addTopTenPlayer(PlayerScore ps)
{
    bool ins = false;
    for (int i = 0; i < m_topten.count(); i++) {
        if (ps.score > m_topten.at(i).score) {
            m_topten.insert(i, ps);
            ins = true;
            break;
        }
    }
    if (!ins)
        m_topten.append(ps);
}

void GameProfile::saveTopTen()
{
    QFile f(userPath + "hall.dat");
    if (f.open(QIODevice::WriteOnly)) {
        QDataStream ds(&f);
        // format signature
        ds << QString("HAL1");

        // count
        int cnt = qMin(m_topten.count(), 10);
        ds << cnt;

        for (int i = 0; i < cnt; i++) {
            ds << m_topten.at(i).name;
            ds << m_topten.at(i).score;
        }
    }
}

void GameProfile::loadTopTen()
{
    QFile f(userPath + "hall.dat");
    if (f.open(QIODevice::ReadOnly)) {
        QDataStream ds(&f);

        QString sig;
        // format signature
        ds >> sig;
        if (sig == "HAL1") {
            int cnt;
            // count
            ds >> cnt;

            for (int i = 0; i < cnt; i++) {
                QString name;
                int score;
                ds >> name;
                ds >> score;
                m_topten.append(PlayerScore(name, score));
            }
        }
    }
}
*/
