#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <cube36.h>
#include <sudoku.h>
#include <solitaire.h>
#include <queens.h>
#include <nikolaus.h>

#include <math.h>

#include <QDebug>
#include <QGraphicsRectItem>
//#include <QGLWidget>
#include <QTime>
#include <QImage>
#include <QTimer>

#include <base/kickstart.h>

bool dummy12 = SQt::Kickstart::setKickStart(
	new SQt::Kickstart::Widget<MainWindow>("backtracker", "???")
);

MainWindow::MainWindow(QWidget *parent) :
	MainWindowT(parent)//,
	//ui(new Ui::MainWindow)
{
	//ui()->setupUi(this);

	Scene = new QGraphicsScene(ui()->graphicsView);
	//QGraphicsRectItem *rect = Scene->addRect(QRectF(0, 0, 100, 100));
	ui()->graphicsView->setScene(Scene);
	//ui->graphicsView->fitInView(0,0,60,60,Qt::KeepAspectRatio);
	//ui->graphicsView->setViewport(new QGLWidget);

	//Trackable* t = new Sudoku(".17.........4......3.2..56.....162...8.....3...689..5....3..6.42...7..8.1........");
	//Trackable* t = new Sudoku(".....6....59.....82....8....45........3........6..3.54...325..6..................");
	//Trackable* t = new Cube36(true);
	//Trackable* t = new Solitaire();
	//Trackable* t = new Queens(8);
	//Trackable* t = new Nikolaus();


	//Tracker* b = new Tracker(t);
	//qRegisterMetaType<Tracker::StatesT>("Tracker::StatesT");
	//qRegisterMetaType<Trackable::ListT>("Trackable::ListT");
	/*
	connect(
		b,
		SIGNAL(progress(Tracker::StatesT, int, Trackable::ListT)),
		this,
		SLOT(drawProgress(Tracker::StatesT, int, Trackable::ListT)),
		Qt::QueuedConnection
	);
	*/


	//QTimer* timer = new QTimer(this);
	//timer->setInterval(2000);
	//timer->setSingleShot(true);
	//connect(timer,SIGNAL(timeout()), b, SLOT(start()));
	//timer->start();

	//QThreadPool::globalInstance()->start(new Solitaire());
	//QThreadPool::globalInstance()->start(t);
	//QThreadPool::globalInstance()->start(new Sudoku(".17.........4......3.2..56.....162...8.....3...689..5....3..6.42...7..8.1........"));


	//SolitaireProblem* ps = new SolitaireProblem(this);
	//ps->solve(Solitaire());


}

void MainWindow::initEvent() {

	//Backtracker<Cube36>* b = new Backtracker<Cube36>(this, QThreadPool::globalInstance(), false);
	//b->solve(Cube36());


	Backtracker<Queens>* b = new Backtracker<Queens>(this);
	b->solve(Queens(12));
	//Cube36Problem* p = new Cube36Problem(this);
	//p->solve(Cube36());
	//p->waitForFinished();
	//for (const auto& res: p->results()) {
		//qDebug() << res.toString();
	//}
}

/*
MainWindow::~MainWindow()
{
	//delete ui;
}
*/
/*
void MainWindow::drawProgress(Tracker::StatesT state, int counter, Trackable::ListT payload)
{
	static int solutions=0;
	const bool savesolution = false;
	const bool drawsolution = true;

	static QTime stopwatch;

	if (state&Tracker::Started)
		stopwatch.start();

	Trackable* t = payload.last();
	Cube36* cube36 = dynamic_cast <Cube36*> (t);
	Sudoku* sudoku = dynamic_cast <Sudoku*> (t);
	Solitaire* solitaire = dynamic_cast <Solitaire*> (t);
	Queens* queens = dynamic_cast <Queens*> (t);
	Nikolaus* nikolaus = dynamic_cast <Nikolaus*> (t);

	if (cube36)
		Boardsize=36;
	else if (sudoku)
		Boardsize=81;
	else if (solitaire)
		Boardsize=49;
	else if (queens)
		Boardsize=queens->hashSize();
	else if (nikolaus)
	{
		qDebug() << nikolaus->toString();
		Boardsize=0;
		//return;
	}
	else
		throw;

	static QVector <QGraphicsRectItem*> Rects(Boardsize,0);
	static QVector <QGraphicsSimpleTextItem*> Texts(Boardsize,0);
	static QMap <Cube36::Pin::ColorT, QColor> ColorMap;

	if (ColorMap.isEmpty())
	{
		ColorMap[Cube36::Pin::Red]=Qt::red;
		ColorMap[Cube36::Pin::Green]=QColor("limegreen");//Qt::green;
		ColorMap[Cube36::Pin::Blue]=QColor("deepskyblue");//Qt::blue;
		ColorMap[Cube36::Pin::Orange]=QColor("darkorange");
		ColorMap[Cube36::Pin::Yellow]=QColor("gold");//Qt::yellow;
		ColorMap[Cube36::Pin::Violet]=QColor("slateblue");//QColor("purple");
		ColorMap[Cube36::Pin::Undefined]=Qt::gray;
	}

	//static QTime lastcall = QTime::currentTime();
	//qDebug() << i;

	if ((drawsolution) && (state&(Tracker::NewBest|Tracker::Solution)) ) //(1)//(i%100==0) || (i==-1))
	{

		//QStringList parts = payload->toString().split(' ', QString::SkipEmptyParts);

		for (int i = 0; i < Boardsize; ++i)
		{
			int l = sqrt(Boardsize);
			int x = i%l;
			int y = i/l;

			if (!Rects[i])
			{
				Rects[i]=ui()->graphicsView->scene()->addRect(x*10,(l-y)*10,10,10);
				Texts[i]=ui()->graphicsView->scene()->addSimpleText("*", QFont("arial", 5));
				//Texts[i]->setParentItem(Rects[i]);
				Texts[i]->setPos(Rects[i]->rect().topLeft());
				QPointF diff = Rects[i]->boundingRect().center() - Texts[i]->mapToParent(Texts[i]->boundingRect().center());
				Texts[i]->moveBy(diff.x(), diff.y());

			}

			if (cube36)
			{
				Rects[i]->setBrush(ColorMap[cube36->pin(x,y).Col]);
				Texts[i]->setText(QString::number(6-cube36->pin(x,y).Len));
			}

			if (sudoku)
			{
				Texts[i]->setText(QString::number(sudoku->field(x,y).value()));
			}

			if (solitaire)
			{
				QString f (solitaire->field(x,y));
				if (f=="x")
					f="";

				Texts[i]->setText(f);
			}

			if (queens)
			{
				QChar c = queens->toString(false)[i];
				Texts[i]->setText(c);
			}

			//qDebug () << diff;//Rects[i]->boundingRect() << );
		}
	}

	if (state&Tracker::Solution)
	{

		if (savesolution)
		{
			QImage img(200,200,QImage::Format_RGB16);
			img.fill(Qt::white);
			QPainter painter(&img);
			ui()->graphicsView->scene()->render(&painter);

			if (img.save(QString().sprintf("solution%02d.png",solutions)))
			{
				qDebug() << "img saved";
			}
			else
				qDebug() << "img *not* saved";
		}

		solutions++;

		if (solitaire) //soltaer-nachbrenner, um den loesungsweg auszugeben
		{
			Solitaire* last = 0;
			foreach (Trackable* tt, payload)
			{
				Solitaire* s = dynamic_cast<Solitaire*>(tt);
				if (s)
				{
					if (last)
					{
						qDebug() << s->diff(*last);
					}
					last = s;
				}
			}
		}
	}

	QStringList states;
	if (state&Tracker::Finished)
		states << "Finished";
	if (state&Tracker::NewBest)
		states << "NewBest";
	if (state&Tracker::Solution)
		states << "Solution";
	if (state&Tracker::Alive)
		states << "Alive";

	QString s = QString("%1 counter: %2 tiefe: %3 loesungen: %4 nach %5 min").arg(states.join("|")).arg(((long)counter*100/1000000)/100.0).arg(payload.size()).arg(solutions).arg((stopwatch.elapsed()*100/60000)/100.0);



	SQt::Widget::MainWindow::instance().statusBar()->showMessage(s);


	//1. nachricht? => view transformieren
	if (counter==0)
		ui()->graphicsView->fitInView(0,0,10*sqrt(Boardsize),10*sqrt(Boardsize),Qt::KeepAspectRatio);

	//cleanup
	foreach (Trackable* t, payload)
	{
		delete t;
	}
}
*/
void MainWindow::resizeEvent ( QResizeEvent * /*event */)
{
	ui()->graphicsView->fitInView(0,0,10*sqrt(Boardsize),10*sqrt(Boardsize),Qt::KeepAspectRatio);
}
