/* AtCore Test Client
    Copyright (C) <2018>
    Author: Chris Rizzitello - rizzitello@kde.org

    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/>.
*/
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>

#include "axiscontrol.h"
#include "movementwidget.h"

class MovementWidget::MovementWidgetPrivate {
    public:
    QComboBox *comboMoveAxis = nullptr;
    QDoubleSpinBox *sbMoveAxis = nullptr;
    AxisControl *axisControl = nullptr;

    QPushButton *disableMotors = nullptr;
    QPushButton *homeAll = nullptr;
    QPushButton *homeX = nullptr;
    QPushButton *homeY = nullptr;
    QPushButton *homeZ = nullptr;
    QPushButton *move = nullptr;

    QList<int> _axisMaxes {200, 200, 200};
};

MovementWidget::MovementWidget(QWidget *parent)
: QWidget(parent)
, d(new MovementWidgetPrivate())
{
    initialize();
}

MovementWidget::~MovementWidget() {
    delete d;
}

void MovementWidget::initialize()
{
    auto *mainLayout = new QVBoxLayout;
    auto *hBoxLayout = new QHBoxLayout;

    d->homeAll = new QPushButton(tr("Home All"), this);
    hBoxLayout->addWidget(d->homeAll);
    connect(d->homeAll, &QPushButton::clicked, this, &MovementWidget::homeAllPressed);

    d->homeX = new QPushButton(tr("Home X"), this);
    hBoxLayout->addWidget(d->homeX);
    connect(d->homeX, &QPushButton::clicked, this, &MovementWidget::homeXPressed);

    d->homeY = new QPushButton(tr("Home Y"), this);
    hBoxLayout->addWidget(d->homeY);
    connect(d->homeY, &QPushButton::clicked, this, &MovementWidget::homeYPressed);

    d->homeZ = new QPushButton(tr("Home Z"), this);
    hBoxLayout->addWidget(d->homeZ);
    connect(d->homeZ, &QPushButton::clicked, this, &MovementWidget::homeZPressed);

    mainLayout->addLayout(hBoxLayout);

    d->disableMotors = new QPushButton(tr("Disable Motors"), this);
    mainLayout->addWidget(d->disableMotors);
    connect(d->disableMotors, &QPushButton::clicked, this, &MovementWidget::disableMotorsPressed);

    d->comboMoveAxis = new QComboBox(this);
    d->comboMoveAxis->addItem(tr("Move X Axis to"));
    d->comboMoveAxis->addItem(tr("Move Y Axis to"));
    d->comboMoveAxis->addItem(tr("Move Z Axis to"));

    d->sbMoveAxis = new QDoubleSpinBox(this);
    d->sbMoveAxis->setRange(0, d->_axisMaxes.at(d->comboMoveAxis->currentIndex()));

    connect(d->comboMoveAxis, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this] (int index) {
        d->sbMoveAxis->setMaximum(d->_axisMaxes.at(index));
    });

    d->move = new QPushButton(tr("Go"), this);
    connect(d->move, &QPushButton::clicked, this, [this] {
        if (d->comboMoveAxis->currentIndex() == 0) {
            emit absoluteMove(QLatin1Char('X'), d->sbMoveAxis->value());
        } else if (d->comboMoveAxis->currentIndex() == 1) {
            emit absoluteMove(QLatin1Char('Y'), d->sbMoveAxis->value());
        } else if (d->comboMoveAxis->currentIndex() == 2) {
            emit absoluteMove(QLatin1Char('Z'), d->sbMoveAxis->value());
        }
    });

    hBoxLayout = new QHBoxLayout;
    hBoxLayout->addWidget(d->comboMoveAxis);
    hBoxLayout->addWidget(d->sbMoveAxis);
    hBoxLayout->addWidget(d->move);
    mainLayout->addLayout(hBoxLayout);

    d->axisControl = new AxisControl(this);
    mainLayout->addWidget(d->axisControl);
    connect(d->axisControl, &AxisControl::clicked, this, &MovementWidget::relativeMove);
    connect(d->axisControl, &AxisControl::unitsChanged, this, &MovementWidget::unitsChanged);

    setLayout(mainLayout);
}

void MovementWidget::setHomeButtonsVisible(bool visible)
{
    d->homeAll->setVisible(visible);
    d->homeX->setVisible(visible);
    d->homeY->setVisible(visible);
    d->homeZ->setVisible(visible);
}

void MovementWidget::setDisableMotorsButtonVisible(bool visible)
{
    d->disableMotors->setVisible(visible);
}

void MovementWidget::setAxisMax(int xMax, int yMax, int zMax)
{
    xMax = std::max(0, xMax);
    yMax = std::max(0, yMax);
    zMax = std::max(0, zMax);
    d->_axisMaxes = QList<int> {xMax, yMax, zMax};
    d->sbMoveAxis->setMaximum(d->comboMoveAxis->currentIndex());
}
