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

extern std::unique_ptr<QSettings> settings;

// event functions
void MainWindow::buttonPressed(QPushButton *button) {
    QString text = "\u2605";
    button->setText(text);
}

// initialisation functions
void MainWindow::initialiseGameGrid() {
    double max_width = MainWindow::width();
    double max_height = MainWindow::height() - ui->menuBar->height() - ui->labelScore->height();

    qDebug() << "gameGridLayout width: " << max_width << " height: " << max_height;

    QSize button_size(max_width/n_columns, max_height/n_rows);

    qDebug() << "Width: " << button_size.width() << " Height: " << button_size.height();

    for(int i = 0; i < n_columns; i++)
        {
            for(int j = 0; j < n_rows; j++)
            {
                QPushButton * button = new QPushButton(this);
                button->setFixedSize(button_size);
                button->setStyleSheet("background-color: " + game.getCell(i,j) + "; border: none;");
                ui->gameGridLayout->addWidget(button, j, i);

                // Set size text etc. for each button

                connect(button, &QPushButton::clicked, [=](){
                    buttonPressed(button);
                });
            }
        }
}

void MainWindow::initialiseWindow() {
    MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff");
    MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png"));
}

void MainWindow::initialiseMenuBar() {
    ui->menuPlay->addAction(QIcon(":/icons/game.png"), "New Game");
    ui->menuPlay->addSeparator();
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , n_rows(settings->value("size/rows", 9).toInt())
    , n_columns(settings->value("size/columns", 9).toInt())
    , game(n_rows, n_columns)
{
    ui->setupUi(this);

    initialiseGameGrid();
    initialiseWindow();
    initialiseMenuBar();
}

MainWindow::~MainWindow()
{
    delete ui;
}

