/***
 * 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/joystickgui.hh>

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

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

// Selects WRJoy namespace
using namespace WRJoy;


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

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

    // Configures elements connections
    configureConnections();
}


void WRJoy::JoystickGUI::JoystickGUI::configureMainWindow() {
    // Inserts the tool bar
    this->addToolBar(Qt::TopToolBarArea, _toolBar);

    // Inserts the tab widget
    this->setCentralWidget(_tabWidget);

    //Insert icon
    this->setWindowIcon(QIcon(QString::fromUtf8("/usr/share/warthog-robotics/logos/wr_logo.svg")));
}


void WRJoy::JoystickGUI::JoystickGUI::configureToolBar() {
    // Creates and configures the tool bar
    _toolBar = new QToolBar(QString::fromUtf8("ToolBar"), this);
    _toolBar->setMovable(false);
    _toolBar->setFloatable(false);
    _toolBar->setIconSize(QSize(24,24));
    _toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    // Adds joystick run-time actions
    this->configureRunTimeActions(_toolBar);

    _toolBar->addSeparator();

    // Adds reset action
    this->configureAdvancedActions(_toolBar);
}


void WRJoy::JoystickGUI::JoystickGUI::configureRunTimeActions(QToolBar* toolBar) {
    // Adds the start joystick button
    _startJoystickAction = new QAction(QIcon(QString::fromUtf8(":/start.png")), QString::fromUtf8("Start joystick"), _toolBar);
    _startJoystickAction->setCheckable(false);
    _startJoystickAction->setEnabled(false);
    toolBar->addAction(_startJoystickAction);

    // Adds the stop joystick button
    _stopJoystickAction = new QAction(QIcon(QString::fromUtf8(":/stop.png")), QString::fromUtf8("Stop joystick"), _toolBar);
    _stopJoystickAction->setCheckable(false);
    _stopJoystickAction->setEnabled(false);
    toolBar->addAction(_stopJoystickAction);
}


void WRJoy::JoystickGUI::JoystickGUI::configureAdvancedActions(QToolBar* toolBar) {
    // Adds the reset joystick button
    _resetAction = new QAction(QIcon(QString::fromUtf8(":/reset.png")), QString::fromUtf8("Reset joystick"), _toolBar);
    _resetAction->setCheckable(false);
    toolBar->addAction(_resetAction);
}


void WRJoy::JoystickGUI::JoystickGUI::configureTabWidget() {
    // Creates and configures the tab widget
    _tabWidget = new QTabWidget(this);
    _tabWidget->setTabPosition(QTabWidget::North);
    _tabWidget->setIconSize(QSize(24,24));

    // Adds basic settings tabs
    _tabWidget->addTab(new WRJoy::JoystickGUI::BasicSettings(_joystick), QIcon(QString::fromUtf8(":/configuration.png")), QString::fromUtf8("Basic settings"));
    //_tabWidget->addTab(new QWidget(), QString());
    //_tabWidget->tabBar()->setTabEnabled(_tabWidget->count()-1, false);

    // Adds Player Settings tab
    _tabWidget->addTab(new WRJoy::JoystickGUI::PlayerSettings(_joystick), QIcon(QString::fromUtf8(":/joystick.png")), QString::fromUtf8("Players Settings"));
    //_tabWidget->addTab(new QWidget(), QString());
    //_tabWidget->tabBar()->setTabEnabled(_tabWidget->count()-1, false);

    // Adds monitor tab
    _tabWidget->addTab(new WRJoy::JoystickGUI::Monitor(_joystick), QIcon(QString::fromUtf8(":/monitor.png")), QString::fromUtf8("Monitor"));
    //_tabWidget->addTab(new QWidget(), QString());
    //_tabWidget->tabBar()->setTabEnabled(_tabWidget->count()-1, false);

    // Adds advanced and button settings tab
    //_tabWidget->addTab(new WRJoy::JoystickGUI::ButtonsSettings(_joystick), QIcon(QString::fromUtf8(":/joystick.png")), QString::fromUtf8("Buttons settings"));
    _tabWidget->addTab(new WRJoy::JoystickGUI::AdvancedSettings(_joystick), QIcon(QString::fromUtf8(":/configuration.png")), QString::fromUtf8("Advanced settings"));
}


void WRJoy::JoystickGUI::JoystickGUI::configureConnections() {
    QObject::connect(_startJoystickAction, &QAction::triggered, this, &WRJoy::JoystickGUI::JoystickGUI::startJoystick);
    QObject::connect(_stopJoystickAction, &QAction::triggered, this, &WRJoy::JoystickGUI::JoystickGUI::stopJoystick);
    QObject::connect(_resetAction, &QAction::triggered, this, &WRJoy::JoystickGUI::JoystickGUI::resetJoystick);
    QObject::connect(_joystick, &Joystick::opened, this, &WRJoy::JoystickGUI::JoystickGUI::joystickOpened);
    QObject::connect(_joystick, &Joystick::closed, this, &WRJoy::JoystickGUI::JoystickGUI::joystickClosed);
    QObject::connect(_joystick, &Joystick::started, this, &WRJoy::JoystickGUI::JoystickGUI::joystickStarted);
    QObject::connect(_joystick, &Joystick::finished, this, &WRJoy::JoystickGUI::JoystickGUI::joystickStoped);
    QObject::connect(_joystick, &Joystick::reseted, this, &WRJoy::JoystickGUI::JoystickGUI::joystickReseted);
}


void WRJoy::JoystickGUI::JoystickGUI::startJoystick() {
    // Starts the joystick
//    _joystick->open(0); // GAMBIARRA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    _joystick->start();
}


void WRJoy::JoystickGUI::JoystickGUI::stopJoystick() {
    // Shows a confirmation dialog
    QString dialogTitle   = _joystick->name().append(QString::fromUtf8(" - Stop joystick"));
    QString dialogMessage = QString::fromUtf8("Are you sure you want to stop ").append(_joystick->name()).append(QString::fromUtf8("?"));

    QMessageBox::StandardButton response;
    response = QMessageBox::warning(this, dialogTitle, dialogMessage,
                                    QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No),
                                    QMessageBox::No);

    // Stops the joystick
    if (response == QMessageBox::Yes) {
        _joystick->stop();
    }
}


void WRJoy::JoystickGUI::JoystickGUI::resetJoystick() {
    // Shows a confirmation dialog
    QString dialogTitle   = _joystick->name().append(QString::fromUtf8(" - Reset joystick"));
    QString dialogMessage = QString::fromUtf8("Are you sure you want to reset ").append(_joystick->name()).append(QString::fromUtf8("?"));

    QMessageBox::StandardButton response;
    response = QMessageBox::warning(this, dialogTitle, dialogMessage,
                                    QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No),
                                    QMessageBox::No);

    // Resets the joystick
    if (response == QMessageBox::Yes) {
        qWarning() << QString::fromUtf8("TODO: Reset joystick!!");
    }
}


void WRJoy::JoystickGUI::JoystickGUI::joystickOpened() {
    // Enables run-time actions
    _startJoystickAction->setEnabled(true);
    _stopJoystickAction->setEnabled(false);
}


void WRJoy::JoystickGUI::JoystickGUI::joystickClosed() {
    // Disables run-time actions
    _startJoystickAction->setEnabled(false);
    _stopJoystickAction->setEnabled(false);
}


void WRJoy::JoystickGUI::JoystickGUI::joystickStarted() {
    // Toogles run-time actions
    _startJoystickAction->setEnabled(false);
    _stopJoystickAction->setEnabled(true);
}

void WRJoy::JoystickGUI::JoystickGUI::joystickStoped() {
    // Toogles run-time actions
    if (_joystick->isOpened()) {
        _startJoystickAction->setEnabled(true);
        _stopJoystickAction->setEnabled(false);
    }
}

void WRJoy::JoystickGUI::JoystickGUI::joystickReseted() {
    qWarning() << QString::fromUtf8("TODO: Joystick reseted!!");
}
