/*
* Someone tells, that Qt can to build this
*/
#include "include/hotseatgame/gamemanager.h"
#include "cell.h"
#include "hotseatgame/gameproperties.h"
#include <iostream>
#include <cmath>

GameManager::GameManager(QObject *parent) : QObject(parent) {

}

void GameManager::buildGameTable(double start_x_coordinate, double start_y_coordinate) {
    start_x_coordinate_ = start_x_coordinate;
    start_y_coordinate_ = start_y_coordinate;
    generateTable();
}

void GameManager::generateTable() {
    game_table_.assign(GameProperties::FIELD_COLUMNS_NUMBER, std::vector< Cell* >(GameProperties::FIELD_ROWS_NUMBER, nullptr));
    for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
        for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
            game_table_[col][row] = new Cell(nullptr);
        }
    }

    unsigned isEven = 1;
    for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
        isEven ^= 1;
        for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
            if (row != 0 && col != 0 - isEven) {
                game_table_[col][row]->setleftUp(game_table_[col - 1 +isEven][row - 1]);
            }
            if (col != 0) {
                game_table_[col][row]->setleft(game_table_[col - 1][row]);
            }
            if (row != GameProperties::FIELD_ROWS_NUMBER - 1 && col != 0 -isEven) {
                game_table_[col][row]->setleftDown(game_table_[col - 1 +isEven][row + 1]);
            }

            if (row != 0 && col != GameProperties::FIELD_COLUMNS_NUMBER -isEven) {
                game_table_[col][row]->setrightUp(game_table_[col +isEven][row - 1]);
            }
            if (col != GameProperties::FIELD_COLUMNS_NUMBER - 1) {
                game_table_[col][row]->setright(game_table_[col + 1][row]);
            }
            if (row != GameProperties::FIELD_ROWS_NUMBER - 1 && col != GameProperties::FIELD_COLUMNS_NUMBER -isEven) {
                game_table_[col][row]->setrightDown(game_table_[col +isEven][row + 1]);
            }
        }
    }

    game_table_[0][0]->setXCoordinate(start_x_coordinate_);
    game_table_[0][0]->setYCoordinate(start_y_coordinate_);

    for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
        for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
            auto cell = game_table_[col][row];

            auto verticles = cell->getPoints(GameProperties::CELL_MIN_RADIUS * 2, 30.0);

            if (cell->getrightUp()) {
                cell->getrightUp()->setXCoordinate(cell->getXCoordinate() + verticles[0].first );
                cell->getrightUp()->setYCoordinate(cell->getYCoordinate() + verticles[0].second);
            }

            if (cell->getright()) {
                cell->getright()->setXCoordinate(cell->getXCoordinate() + verticles[1].first );
                cell->getright()->setYCoordinate(cell->getYCoordinate() + verticles[1].second);
            }

            if (cell->getrightDown()) {
                cell->getrightDown()->setXCoordinate(cell->getXCoordinate() + verticles[2].first );
                cell->getrightDown()->setYCoordinate(cell->getYCoordinate() + verticles[2].second);
            }

            if (cell->getleftDown()) {
                cell->getleftDown()->setXCoordinate(cell->getXCoordinate() + verticles[3].first );
                cell->getleftDown()->setYCoordinate(cell->getYCoordinate() + verticles[3].second);
            }

            if (cell->getleft()) {
                cell->getleft()->setXCoordinate(cell->getXCoordinate() + verticles[4].first );
                cell->getleft()->setYCoordinate(cell->getYCoordinate() + verticles[4].second);
            }

            if (cell->getleftUp()) {
                cell->getleftUp()->setXCoordinate(cell->getXCoordinate() + verticles[5].first );
                cell->getleftUp()->setYCoordinate(cell->getYCoordinate() + verticles[5].second);
            }
        }
    }

    for (unsigned row = 0; row < GameProperties::FIELD_ROWS_NUMBER; ++row) {
        for (unsigned col = 0; col < GameProperties::FIELD_COLUMNS_NUMBER; ++col) {
            auto cell = game_table_[col][row];
            std::cout << "(" << cell->getXCoordinate() << "," << cell->getYCoordinate() << ") ";
        }
        std::cout << std::endl;
    }
}


UnitsQueue* GameManager::getTurnQueue() {
    return &turn_queue_;
}

void GameManager::AddToUnitQueue(Unit* unit) {
    turn_queue_.add(unit);
}

void GameManager::RmFromUnitQueue(Unit* unit) {
    turn_queue_.remove(unit);
}

int GameManager::getCurPlayerId() {
    return cur_player_id_;
}

std::vector<std::vector<Cell *> > GameManager::getGameField()
{
    return game_table_;
}

Player* GameManager::getCurrentPlayer() {
    return player_manager_->getPlayer(cur_player_id_);
}
