#include "window.h"
#include <QApplication>
#include <QStandardPaths>
#include <QDebug>
#include <QThread>
#include <QProgressBar>
#include <QSlider>

Window::Window(QWidget *parent)
    : QWidget{parent}
{
    setFixedWidth(1600);
    m_button = new QPushButton("solve", this);
    connect(m_button, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
    m_button->setGeometry(1200, height() - 30, width() - 1200, 30);
    m_slider = new QSlider(this);
    m_slider->setRange(0,1000);
    m_slider->setOrientation(Qt::Horizontal);
    m_slider->setValue(m_delay);
    m_slider->setGeometry(1200, height() - 60, width() - 1200, 30);
    connect(m_slider,SIGNAL(valueChanged(int)),this,SLOT(setValue(int)));
    
    int dx = 0;
    int dy = 0;
    for (int x = 0; x < 9; x++)
    {
        for (int y = 0; y < 9; y++)
        {
            if (x == 3)
                dx = 4;
            if (x == 6)
                dx = 8;
            if (y < 3)
                dy = 0;
            if (y >= 3)
                dy = 4;
            if (y >= 6)
                dy = 8;
            m_cell[x+y*9].setParent(this); 
            m_cell[x+y*9].setGeometry(x * 100 + dx, y * 100 + dy, 100, 100);
            /* code */
        }
    }
    // init signals
    for (int x = 0; x < 9; x++)
    {
        for (int y = 0; y < 9; y++)
        {
            for (int d = 0; d < 9; d++)  //rows and columns
            {
                connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[x+d*9], SLOT(removeOption(int)));
                connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[d+y*9], SLOT(removeOption(int)));

                connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[x+d*9], SLOT(addOption(int)));
                connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[d+y*9], SLOT(addOption(int)));
            }
            int a = x / 3 * 3;
            int b = y / 3 * 3;
            for (int x1 = a; x1 < a + 3; x1++) //square
            {
                for (int y1 = b; y1 < b + 3; y1++)
                {
                    connect(&m_cell[x+y*9], SIGNAL(update(int)), &m_cell[x1+y1*9], SLOT(removeOption(int)));

                    connect(&m_cell[x+y*9], SIGNAL(undo(int)), &m_cell[x1+y1*9], SLOT(addOption(int)));
                }
            }
        }
    }
}

void Window::solveButtonClicked()
{
    // std::array<Cell,81> start = m_cell;
    std::list<Cell*> hist;
    srand(time(NULL));
    while (1)
    {
        std::vector<Cell *> b; //vector of cells with least entropy
        int minEtropy = 10; 
        for (int x = 0; x < 9; x++) //loop through all cells and fill list b --> this could be optimzed 
        {
            for (int y = 0; y < 9; y++)
            {
                if (m_cell[x+y*9].collapsed)
                    continue;
                else if (m_cell[x+y*9].possibleStates < minEtropy)
                {
                    minEtropy = m_cell[x+y*9].possibleStates;
                    b.clear();
                    b.push_back(&m_cell[x+y*9]);
                }
                else if (m_cell[x+y*9].possibleStates == minEtropy)
                {
                    b.push_back(&m_cell[x+y*9]);
                }
            }
        }
        if (b.size() == 0) //if b is empty -> solved 
        {
            qInfo("Solved!!");
            return;
        }
        else if(minEtropy ==0){
            while (hist.size()>0)
            {
                hist.back()->un();
                hist.pop_back();
            }
            return;
            //solveButtonClicked();
        }
        int choiceCell = rand() % b.size(); //choose a random cell 
        int choiceNr = rand() % b[choiceCell]->index.size(); //choose a random number;
        b[choiceCell]->collapse(b[choiceCell]->index[choiceNr]); //collapse that random cell with chosen number;
        hist.push_back(b[choiceCell]);
        repaint(); //draw cells again
        QThread::msleep(m_delay);
    }
    // qInfo("%d---:)");
}

void Window::setValue(int s){
    m_delay = s;
    return;
}