#include "input_plot.hh"
#include "../../3rdParty/qwt/qwt_plot_layout.h"

InputPlot::InputPlot(QWidget* parent) :
    QwtPlot(parent),
    inputCurve_(std::make_unique<QwtPlotCurve>()),
    inputGrid_(std::make_unique<QwtPlotGrid>())
{
    initPlot();
    initAxes();
    initCurve();
    initGrid();
}

void InputPlot::initPlot()
{
    QFont plotTitleFont;
    plotTitleFont.setPointSize(11);
    plotTitleFont.setBold(true);

    auto plotTitle = QwtText(tr("Neural network inputs"));
    plotTitle.setFont(plotTitleFont);

    setTitle(plotTitle);
    plotLayout()->setCanvasMargin(10, QwtPlot::yRight);

    setAutoFillBackground(true);
    setPalette(Qt::white);
    setCanvasBackground(Qt::white);
    setFrameStyle(QFrame::Plain | QFrame::StyledPanel);

    auto frame = static_cast<QFrame*>(canvas());
    frame->setFrameStyle(QFrame::NoFrame);
}

void InputPlot::initAxes()
{
    QFont axisTitleFont;
    axisTitleFont.setPointSize(11);

    QFont axisFont;
    axisFont.setPointSize(10);

    auto xBottomAxisTitle = QwtText(tr("Input number"));
    xBottomAxisTitle.setFont(axisTitleFont);

    auto yLeftAxisTitle = QwtText(tr("Input value"));
    yLeftAxisTitle.setFont(axisTitleFont);

    setAxisTitle(QwtPlot::xBottom, xBottomAxisTitle);
    setAxisScale(QwtPlot::xBottom, 0.0, 10.0);
    setAxisMaxMinor(QwtPlot::xBottom, 0);
    setAxisMaxMajor(QwtPlot::xBottom, 11);
    setAxisFont(QwtPlot::xBottom, axisFont);

    setAxisTitle(QwtPlot::yLeft, yLeftAxisTitle);
    setAxisAutoScale(QwtPlot::yLeft);
    setAxisFont(QwtPlot::yLeft, axisFont);
}

void InputPlot::initCurve()
{
    auto pen = inputCurve_->pen();
    pen.setWidth(2);
    pen.setColor(Qt::darkBlue);
    inputCurve_->setPen(pen);
    inputCurve_->attach(this);
}

void InputPlot::initGrid()
{
    QPen majorInputPen = inputGrid_->majorPen();
    majorInputPen.setColor(Qt::gray);
    majorInputPen.setStyle(Qt::DashLine);
    inputGrid_->setMajorPen(majorInputPen);
    inputGrid_->attach(this);
}

void InputPlot::setPlotData(const QVector<double>& inputs)
{
    QVector<double> xVals;

    for (int i = 0; i < inputs.size(); i++)
        xVals.append(i);

    inputCurve_->setSamples(xVals, inputs);
}
