#include "select.h"
#include "playground.h"
#include "playcanvas.h"
#include "resource.h"
#include "rules.h"
#include "rule.h"

#include <qlayout.h>
#include <qframe.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include <qcheckbox.h>
#include <qdir.h>

#include <kapplication.h>
#include <klocale.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>

#include "config.h"

Select::Select(QWidget *parent, const char *name)
: QDialog(parent, name)
{
	QVBoxLayout *vbox;
	QHBoxLayout *hbox, *hbox2, *hbox3;
	QPushButton *ok, *cancel, *editcustom;
	QLabel *desclevel, *desctheme, *descplayers;
	QString lastlevel;
	int lastbots;

	m_playground = NULL;
	m_playcanvas = NULL;
	m_rules = NULL;

	kapp->config()->setGroup("Preferences");
	lastlevel = kapp->config()->readEntry("Level");
	lastbots = kapp->config()->readNumEntry("Bots");

	desclevel = new QLabel(i18n("Level to play"), this);
	desctheme = new QLabel(i18n("Theme to use"), this);
	descplayers = new QLabel(i18n("Number of AI players"), this);

	combolevel = new QComboBox(this);

	KSimpleConfig conf(DATADIR "/levels/levels.list");
	conf.setGroup("levels");
	QStringList l = conf.readListEntry("levels");
	for(QStringList::iterator it = l.begin(); it != l.end(); it++)
	{
		conf.setGroup((*it));
		QString name = conf.readEntry("Name", i18n("(unknown)"));
		name = name + " (" + conf.readEntry("width", "0") + "x" + conf.readEntry("height", "0") + ")";
		combolevel->insertItem(name);
		m_map[name] = (*it);
		if(lastlevel == (*it)) combolevel->setCurrentText(name);
	}

	QString localdir = locateLocal("data", "kamikaze/");
	QDir dir(localdir);
	for(unsigned int i = 0; i < dir.count(); i++)
	{
		QString name = dir[i];

		if(name == ".") continue;
		if(name == "..") continue;

		combolevel->insertItem(name);
		m_map[name] = name;
		if(lastlevel == name) combolevel->setCurrentText(name);
	}

	combotheme = new QComboBox(this);

	KSimpleConfig conf2(DATADIR "/pics/themes.registry");
	conf2.setGroup("themes");
	QStringList l2 = conf2.readListEntry("themes");
	for(QStringList::iterator it = l2.begin(); it != l2.end(); it++)
	{
		conf2.setGroup((*it));
		QString name = conf2.readEntry("Name", i18n("(unknown)"));
		combotheme->insertItem(name);
		m_map2[name] = (*it);
		if(Resource::theme() == (*it)) combotheme->setCurrentText(name);
	}

	comboplayers = new QComboBox(this);

	preview = new QFrame(this);
	preview->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	preview->setFixedSize(128, 128);

	combocustom = new QComboBox(this);

	editcustom = new QPushButton(i18n("Manage..."), this);
	
	xlayout = new QVBoxLayout(preview, 2);

	ok = new QPushButton(i18n("Select"), this);
	cancel = new QPushButton(i18n("Cancel"), this);

	vbox = new QVBoxLayout(this, 5);
	vbox->add(desclevel);
	vbox->add(combolevel);
	vbox->add(desctheme);
	vbox->add(combotheme);
	vbox->add(descplayers);
	vbox->add(comboplayers);
	hbox2 = new QHBoxLayout(vbox, 5);
	hbox2->addStretch(1);
	hbox2->add(preview);
	hbox2->addStretch(1);
	hbox3 = new QHBoxLayout(vbox, 5);
	hbox3->add(combocustom);
	hbox3->add(editcustom);
	hbox = new QHBoxLayout(vbox, 5);
	hbox->add(ok);
	hbox->add(cancel);

	connect(comboplayers, SIGNAL(activated(int)), SLOT(minimap()));
	connect(combolevel, SIGNAL(activated(int)), SLOT(minimap()));
	connect(combotheme, SIGNAL(activated(int)), SLOT(minimap()));
	connect(ok, SIGNAL(clicked()), SLOT(slotAccept()));
	connect(cancel, SIGNAL(clicked()), SLOT(reject()));
	connect(editcustom, SIGNAL(clicked()), SLOT(slotCustom()));

	setBots(8);
	if(lastbots) comboplayers->setCurrentItem(lastbots - 1);

	setCaption(i18n("Select a level"));

	minimap();

	QString customrule = kapp->config()->readEntry("Customrule");

	KSimpleConfig rulesconf("kamikazerules");
	QStringList rulelist = rulesconf.groupList();
	combocustom->insertItem(i18n("Standard rules"));
	for(QStringList::iterator it = rulelist.begin(); it != rulelist.end(); it++)
	{
		combocustom->insertItem((*it));
		if((*it) == customrule) combocustom->setCurrentText(customrule);
	}
}

Select::~Select()
{
}

void Select::setBots(int bots)
{
	comboplayers->clear();
	for(int i = 0; i < bots; i++)
	{
		comboplayers->insertItem(QString("%1").arg(i + 1));
		comboplayers->setCurrentItem(i);
	}
}

void Select::setBotsReadOnly()
{
	comboplayers->setEnabled(false);
}

void Select::slotAccept()
{
	if(m_playground) delete m_playground;
	if(m_playcanvas) delete m_playcanvas;

	kapp->config()->writeEntry("Customrule", combocustom->currentText());

	accept();
}

void Select::minimap()
{
	if(m_playground) delete m_playground;
	if(m_playcanvas) delete m_playcanvas;

	int oldsize = Resource::tilesize();
	Resource::setTilesize(6);
	m_playcanvas = new PlayCanvas(preview);
	m_playcanvas->setTheme(theme());
	m_playcanvas->setPlayers(players());
	m_playcanvas->buildLevel(level());
	Resource::setTilesize(oldsize);

	m_playground = new Playground(m_playcanvas, preview);
	//m_playground->move(preview->x(), preview->y());
	m_playground->resize(preview->width() - 4, preview->height() - 4);
	xlayout->add(m_playground);
}

QString Select::level()
{
	return m_map[combolevel->currentText()];
}

QString Select::theme()
{
	return m_map2[combotheme->currentText()];
}

int Select::players()
{
	return comboplayers->currentText().toInt();
}

Rule *Select::rules()
{
	Rules rulesdlg;
	if(!m_rules)
	{
		rulesdlg.setName(combocustom->currentText());
		m_rules = rulesdlg.rules();
	}
	return m_rules;
}

void Select::slotCustom()
{
	Rules rulesdlg;

	rulesdlg.setName(combocustom->currentText());

	int ret = rulesdlg.exec();
	if(ret == QDialog::Accepted)
	{
		if(rulesdlg.name().isNull()) return;
		m_rules = rulesdlg.rules();
		m_rules->setCustom();
		if(rulesdlg.name() != combocustom->currentText())
		{
			combocustom->insertItem(rulesdlg.name());
			combocustom->setCurrentText(rulesdlg.name());
		}
	}
}

