#include "ui/hotseat_game/hotseatgame.h"
#include "ui_hotseatgame.h"
#include "gui/guiscenemanager.h"

#include "cell.h"
#include "hotseatgame/gamemanager.h"

#include <cmath>

#include <QDebug>
#include <QGraphicsScene>
#include <QEvent>
#include <QResizeEvent>

HotSeatGame::HotSeatGame(QWidget *parent) :
    Scene(parent),
    ui(new Ui::HotSeatGame)
{
    qDebug() << "Initialising game";
    ui->setupUi(this);
    game_scene_ = new QGraphicsScene(ui->game_content);
    ui->game_content->setScene(game_scene_);
    setMouseTracking(true);
    ui->game_content->setMouseTracking(true);
    game_scene_watcher_ = new DrawSceneEventListener(this);
    ui->game_content->installEventFilter(game_scene_watcher_);
}

void HotSeatGame::init() {
    GameProperties::FIELD_ROWS_NUMBER = 6;
    GameProperties::FIELD_COLUMNS_NUMBER = 14;
    int left_right_margin = 0;
    int up_down_margin = 0;

    int widget_width = ui->game_content->width();
    int widget_height = ui->game_content->height();

    qDebug() << widget_width << " " << widget_height;

    int predictable_min_radius = (widget_width - left_right_margin * 2) / ((GameProperties::FIELD_COLUMNS_NUMBER + 1) * 2);
    int predictable_max_radius = (widget_height - up_down_margin * 2) / ((GameProperties::FIELD_ROWS_NUMBER + 1) * 2);

    qDebug() << predictable_min_radius << " " << predictable_max_radius;

    if (abs(predictable_max_radius * sin(60.0 * M_PI / 180.0)) > abs(predictable_min_radius)) {
        predictable_max_radius = abs(predictable_min_radius /  sin(60.0 * M_PI / 180.0));
    } else {
        predictable_min_radius = abs(predictable_max_radius * sin(60.0 * M_PI / 180.0));
    }

    qDebug() << predictable_min_radius << " " << predictable_max_radius;

    GameProperties::CELL_MIN_RADIUS = predictable_min_radius;
    GameProperties::CELL_MAX_RADIUS = predictable_max_radius;

    left_right_margin = (widget_width - predictable_min_radius * GameProperties::FIELD_COLUMNS_NUMBER * 2) / 2;
    up_down_margin = (widget_height - predictable_max_radius * GameProperties::FIELD_ROWS_NUMBER * 2) / 2;
    qDebug() << left_right_margin << " " << up_down_margin;
    qDebug() << predictable_min_radius * GameProperties::FIELD_COLUMNS_NUMBER * 2 << " " << predictable_max_radius * GameProperties::FIELD_ROWS_NUMBER * 2;
    GameManager::getInstance().buildGameTable(left_right_margin + GameProperties::CELL_MIN_RADIUS, -up_down_margin - GameProperties::CELL_MAX_RADIUS);
    drawField();
}

void HotSeatGame::deinit()
{
    auto field = GameManager::getInstance().getGameField();
    for (unsigned i = 0; i < field.size(); i++) {
        for (unsigned j = 0; j < field[i].size(); j++){
            field[i][j]->setVisible(false);
            game_scene_->removeItem(field[i][j]);
        }
    }
}


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

void HotSeatGame::on_surrender_button__clicked()
{
    //GuiSceneManager::getInstance().showMainWindow();
    //GuiSceneManager::getInstance().hideDetachedScene("hotseatgame");
    //GuiSceneManager::getInstance().changeScene("hotseatgame_results");
    GuiSceneManager::getInstance().changeScene("main_menu");
}

void HotSeatGame::drawField()
{
    auto field = GameManager::getInstance().getGameField();
    game_scene_->addRect(0, 0, 0.1, 0.1);
    for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
        for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
            auto cell = field[col][row];

            double center_x = cell->getXCoordinate();
            double center_y = cell->getYCoordinate();

            qDebug() << center_x << " " << center_y;
            game_scene_->addItem(cell);
            cell->setPos(cell->getXCoordinate(), cell->getYCoordinate());
            cell->setVisible(true);
        }
    }
}

DrawSceneEventListener::DrawSceneEventListener(HotSeatGame *scene, QObject *parent) : QObject(parent) {
    scene_ = scene;
}

bool DrawSceneEventListener::eventFilter(QObject *, QEvent *event)
{
    if (event->type() == QEvent::Resize) {
        scene_->deinit();
        scene_->init();
    }

    if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonPress ) {
        qDebug() << "ha-ha!!";

        QMouseEvent* evt = dynamic_cast<QMouseEvent*>(event);
        auto cells = GameManager::getInstance().getGameField();
        qDebug() << evt->globalPos();
        for (unsigned i = 0; i < cells.size(); i++) {
            for (unsigned j = 0; j < cells[i].size(); j++) {
                if (cells[i][j]->contains(evt->localPos())) {
                    qDebug() << "He-he!!!";
                    cells[i][j]->setHovered(true);
                } else {
                    cells[i][j]->setHovered(false);
                }
                cells[i][j]->show();
            }
        }
    }
    return false;
}
