/* toplevel.cpp

  Copyright (C) 1998   Andreas W�st (AndreasWuest@gmx.de)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  */

#include "settings.h"
#include "gamewidget.h"
#include "feld.h"
#include "molek.h"

#include <tqscrollbar.h>
#include <tqgroupbox.h>
#include <tqlayout.h>
#include <tqvbox.h>
#include <tqlabel.h>

#include <tdeapplication.h>
#include <highscore/kscoredialog.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdeglobal.h>
#include <ksimpleconfig.h>
#include <tdeglobalsettings.h>

Options settings;

#define MPOSX 480
#define MPOSY 90

// ##########################
// #	class GameWidget    #
// ##########################

int level;

void GameWidget::moveUp()
{
  feld->startAnimation (Feld::MoveUp);
}

void GameWidget::moveDown()
{
  feld->startAnimation (Feld::MoveDown);
}

void GameWidget::moveLeft()
{
  feld->startAnimation (Feld::MoveLeft);
}

void GameWidget::moveRight()
{
  feld->startAnimation (Feld::MoveRight);
}

void GameWidget::nextAtom()
{
  feld->nextAtom();
}

void GameWidget::previousAtom()
{
  feld->previousAtom();
}

void GameWidget::getButton (int button)
{
    feld->startAnimation ((Feld::Direction)button);
}

void GameWidget::doUndo ()
{
  feld->doUndo ();
}

void GameWidget::doRedo ()
{
  feld->doRedo ();
}

void GameWidget::gameOver(int moves) {
    KMessageBox::information(this, i18n("You solved level %1 with %2 moves!").arg(level).arg(moves), i18n("Congratulations"));

    KScoreDialog high(KScoreDialog::Name | KScoreDialog::Score, this);
    high.setCaption(i18n("Level %1 Highscores").arg(level));
    high.setConfigGroup(TQString("Highscores Level %1").arg(level));

    KScoreDialog::FieldInfo scoreInfo;

    if (high.addScore(moves, scoreInfo, true, true))
    {
       high.exec();
    }
    updateLevel(level+1);
}

void GameWidget::getMoves(int moves)
{
    current.setNum(moves);
    ys->setText(current);
}

void GameWidget::mergeHighScores(int l)
{
    TDEConfigGroup oldConfig(kapp->config(), TQString("High Scores Level %1").arg(l).utf8());
    TDEConfigGroup newConfig(kapp->config(), TQString("Highscores Level %1").arg(l).utf8());

    newConfig.writeEntry("LastPlayer", oldConfig.readEntry("LastPlayer"));

    TQString num;
    for (int i = 1; i <= 10; ++i)
    {
	num.setNum(i);
	TQString key = "Pos" + num + "Name";
        newConfig.writeEntry(key, oldConfig.readEntry(key, "-"));
	key = "Pos" + num + "Score";
	newConfig.writeEntry(key, oldConfig.readEntry(key, "-"));
    }
    kapp->config()->sync();
}

void GameWidget::updateLevel (int l)
{
    level=l;
    TQString levelFile = locate("appdata", TQString("levels/level_%1").arg(l));
    if (levelFile.isNull()) {
	return updateLevel(1);
    }

    KSimpleConfig cfg(levelFile, true);
    cfg.setGroup("Level");
    feld->load(cfg);

    if (!kapp->config()->hasGroup(TQString("Highscores Level %1").arg(level)) &&
            kapp->config()->hasGroup(TQString("High Scores Level %1").arg(level)))
	mergeHighScores(level);

    highScore->setConfigGroup(TQString("Highscores Level %1").arg(level));
    highest.setNum(highScore->highScore());

    if (highest != "0" ) hs->setText(highest);
    else hs->setText("-");
    ys->setText("0");
    scrl->setValue(level);

    feld->repaint();
}

void GameWidget::restartLevel()
{
    updateLevel(level);
}

GameWidget::GameWidget ( TQWidget *parent, const char* name )
    : TQWidget( parent, name )
{
    level = 1;
    nlevels = TDEGlobal::dirs()->findAllResources("appdata", "levels/level_*",
				false, true).count();

    TQHBoxLayout *top = new TQHBoxLayout(this, 10);

    // spielfeld
    feld = new Feld (this, "feld");
    feld->setFocus();

    top->addWidget(feld);

    TQVBox *vb = new TQVBox(this);
    vb->setSpacing(20);
    top->addWidget(vb);

    // scrollbar
    scrl = new TQScrollBar(1, nlevels, 1,
			5, 1, TQt::Horizontal, vb, "scrl" );
    connect (scrl, TQ_SIGNAL (valueChanged (int)), TQ_SLOT (updateLevel (int)));

    // molek�l
    molek = new Molek (vb, "molek");
    feld->setMolek(molek);

    connect (feld, TQ_SIGNAL (gameOver(int)), TQ_SLOT(gameOver(int)));
    connect (feld, TQ_SIGNAL (sendMoves(int)), TQ_SLOT(getMoves(int)));
    connect (feld, TQ_SIGNAL (enableRedo(bool)), TQ_SIGNAL(enableRedo(bool)));
    connect (feld, TQ_SIGNAL (enableUndo(bool)), TQ_SIGNAL(enableUndo(bool)));

    highScore = new KScoreDialog(KScoreDialog::Name | KScoreDialog::Score, this);

    // the score group
    TQGroupBox *bg = new TQGroupBox (i18n("Score"), vb, "bg");
    TQBoxLayout *slay = new TQVBoxLayout (bg, 10);

    slay->addSpacing(10);

    slay->addWidget(new TQLabel(i18n("Highscore:"), bg));

    TQFont headerFont = TDEGlobalSettings::generalFont();
    headerFont.setBold(true);

    hs = new TQLabel (highest, bg);
    hs->setAlignment(TQt::AlignRight);
    hs->setFont(headerFont);
    slay->addWidget(hs);

    slay->addSpacing(10);

    slay->addWidget(new TQLabel(i18n("Your score so far:"), bg));

    ys = new TQLabel (current, bg);
    ys->setAlignment(TQt::AlignRight);
    ys->setFont(headerFont);
    slay->addWidget(ys);

    updateLevel(1);

    TDEConfig *config = TDEGlobal::config();
    config->setGroup("Options");
    settings.anim_speed = config->readNumEntry("Animation Speed", 1);
    if (settings.anim_speed < 1 || settings.anim_speed > MAX_SPEED)
	settings.anim_speed = 1;

    settings.changed = false;
}

GameWidget::~GameWidget()
{
}

void GameWidget::showHighscores ()
{
    KScoreDialog high(KScoreDialog::Name | KScoreDialog::Score, this);
    high.setCaption(i18n("Level %1 Highscores").arg(level));
    high.setConfigGroup(TQString("Highscores Level %1").arg(level));
    high.exec();
}

#include "gamewidget.moc"
