/***
 * Warthog Robotics
 * University of Sao Paulo (USP) at Sao Carlos
 * http://www.warthog.sc.usp.br/
 *
 * This file is part of WRJoy project.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***/

// Includes class header
#include <WRJoy/JoystickGUI/playersettings.hh>

// Includes Qt library
#include <QtCore/QtCore>
#include <QtWidgets/QtWidgets>

// Includes WRJoy
#include <WRJoy/wrjoy.hh>

// Selects WRJoy namespace
using namespace WRJoy;


WRJoy::JoystickGUI::PlayerSettings::PlayerSettings(Joystick* joystick) {
    // Sets the controlled joystick
    _joystick = joystick;

    // Sets this object name
    this->setObjectName(QString::fromUtf8("Player settings widget"));

    // Configures widget elements
    configureMainWindow();
    configureToolBar();
    configureCentralWidget();

    // Configures elements connections
    configureConnections();
}

void WRJoy::JoystickGUI::PlayerSettings::configureMainWindow() {
    // Creates the tool bar
    _toolBar = this->addToolBar(QString::fromUtf8("Player settings tool bar"));

    // Creates the central widget
    this->setCentralWidget(new QWidget());
    _centralWidget = this->centralWidget();
    _centralWidget->setObjectName(QString::fromUtf8("Player settings central widget"));
}

void WRJoy::JoystickGUI::PlayerSettings::configureToolBar() {
    // Configures the tool bar
    _toolBar->setMovable(false);
    _toolBar->setFloatable(false);
    _toolBar->setIconSize(QSize(24,24));
    _toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}

void WRJoy::JoystickGUI::PlayerSettings::configureCentralWidget() {
    // Configures the central widget
    QFormLayout* layout = new QFormLayout(_centralWidget);
    layout->setFormAlignment(Qt::AlignCenter);
    layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
    _centralWidget->setLayout(layout);

    // Creates the team list spin box
    _teamsSpinBox = new QSpinBox();
    _teamsSpinBox->setMaximum(PLAYER_SETTINGS_MAX_TEAM);
    _teamsSpinBox->setObjectName(QString::fromUtf8("Team settings spin box"));

    // Creates the joysticks list spin box
    _playersSpinBox = new QSpinBox();
    _playersSpinBox->setMaximum(PLAYER_SETTINGS_MAX_PLAYER);
    _playersSpinBox->setObjectName(QString::fromUtf8("Player settings spin box"));

    // Creates the double spin box for set the maximun speed for robot
    _speedSpinBox = new QDoubleSpinBox();
    _speedSpinBox->setMaximum(PLAYER_SETTINGS_MAX_SPEED);
    _speedSpinBox->setSingleStep(PLAYER_SETTINGS_PLAYER_SPINBOX_STEP);
    _speedSpinBox->setObjectName(QString::fromUtf8("Speed settings double spin box"));

    //Creates the check box for the kick activation
    _kickEnable = new QCheckBox();
    _kickEnable->setObjectName(QString::fromUtf8("Kick settings check box"));
    _kickEnable->setChecked(true);

    _kickChipEnable = new QCheckBox();
    _kickChipEnable->setObjectName(QString::fromUtf8("Chip kick settings check box"));
    _kickChipEnable->setChecked(true);

    layout->addRow(QString::fromUtf8("Team:"), _teamsSpinBox);
    layout->addRow(QString::fromUtf8("Player:"), _playersSpinBox);
    layout->addRow(QString::fromUtf8("Player Speed:"), _speedSpinBox);
    layout->addRow(QString::fromUtf8("Kick on Touch:"), _kickEnable);
    layout->addRow(QString::fromUtf8("Chip Kick:"), _kickChipEnable);

    this->fillForm();
}

void WRJoy::JoystickGUI::PlayerSettings::configureConnections() {

    // Configures connections for the actions
    void (QSpinBox:: *signalTeam)(int) = &QSpinBox::valueChanged;
    QObject::connect(_teamsSpinBox, signalTeam, this,  &WRJoy::JoystickGUI::PlayerSettings::changeTeam);

    void (QSpinBox:: *signalPlayer)(int) = &QSpinBox::valueChanged;
    QObject::connect(_playersSpinBox, signalPlayer, this,  &WRJoy::JoystickGUI::PlayerSettings::changePlayer);

    void (QDoubleSpinBox:: *signalSpeed)(double) = &QDoubleSpinBox::valueChanged;
    QObject::connect(_speedSpinBox, signalSpeed, this,  &WRJoy::JoystickGUI::PlayerSettings::changeSpeed);

    void (QCheckBox:: *signalKick)(int) = &QCheckBox::stateChanged;
    QObject::connect(_kickEnable, signalKick, this,  &WRJoy::JoystickGUI::PlayerSettings::changeKick);

    void (QCheckBox:: *signalChipKick)(int) = &QCheckBox::stateChanged;
    QObject::connect(_kickChipEnable, signalChipKick, this,  &WRJoy::JoystickGUI::PlayerSettings::changeChipKick);

    void (Joystick:: *signalPlayerChanged)(quint8) = &Joystick::playerChanged;
    connect(_joystick, signalPlayerChanged, this, &WRJoy::JoystickGUI::PlayerSettings::playerChanged);
}

void WRJoy::JoystickGUI::PlayerSettings::changeTeam(int id) {
    _joystick->setTeam((quint8)id);
}

void WRJoy::JoystickGUI::PlayerSettings::changePlayer(int id) {
    _joystick->setPlayer((quint8)id);
}

void WRJoy::JoystickGUI::PlayerSettings::changeSpeed(double speed){
    _joystick->setSpeed(speed);
}

void WRJoy::JoystickGUI::PlayerSettings::changeKick() {
    if(_kickEnable->isChecked()) {
        _joystick->setKickEnable(true);
    } else {
        _joystick->setKickEnable(false);
    }
}

void WRJoy::JoystickGUI::PlayerSettings::changeChipKick() {
    if(_kickChipEnable->isChecked()) {
        _joystick->setKickChipEnable(true);
    } else {
        _joystick->setKickChipEnable(false);
    }
}

void JoystickGUI::PlayerSettings::playerChanged(quint8 player) {
    _playersSpinBox->setValue(player);
}

void WRJoy::JoystickGUI::PlayerSettings::fillForm() {
    // Sets the speed spin box default value
    _speedSpinBox->setValue(_joystick->getMaxAnalogSpeed());

    // Sets the default kick enable chechbox
    if(_joystick->getKickEnable())
        _kickEnable->setCheckState(Qt::Checked);

    if(_joystick->getChipKickEnable())
        _kickChipEnable->setCheckState(Qt::Checked);
}
