// XDrawChem
// Copyright (C) 2004-2005  Bryan Herger <bherger@users.sourceforge.net>
// Copyright (C) 2020  Yaman Qalieh <ybq987@gmail.com>

// 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 <https://www.gnu.org/licenses/>.

// graphwidget.cpp - definitions for functions of class GraphWidget

#include <QApplication>
#include <QClipboard>
#include <QPaintEvent>
#include <QPainter>
#include <QPrintDialog>
#include <QPrinter>

#include "defs.h"
#include "graphdata.h"
#include "graphwidget.h"
#include "render2d.h"

GraphWidget::GraphWidget(QWidget *parent) : QWidget(parent) {
    datatype = 0;
    output = 0;
    PrintSetup();
    m_toclip = "";
}

void GraphWidget::paintEvent(QPaintEvent *pe) {
    QPainter p;
    QString s, ft;
    bool cmp2 = false;
    int cx = 10, cy = 10;

    if (output == 0)
        p.begin(this);
    else
        p.begin(printer);

    if (p1.width() > 0) {
        p.drawPixmap(QPoint(10, 10), p1);
        cy = cy + p1.height() + 5;
    }
    if (datatype == 1) { // 13C-NMR
        cy = cy + 90;
        // draw baseline
        p.drawLine(QPoint(50, cy), QPoint(550, cy));
        p.drawLine(QPoint(500, cy), QPoint(500, cy + 3));
        p.drawText(QPoint(500, cy + 15), "0 ppm");
        p.drawLine(QPoint(440, cy), QPoint(440, cy + 3));
        p.drawText(QPoint(440, cy + 15), "20");
        p.drawLine(QPoint(380, cy), QPoint(380, cy + 3));
        p.drawText(QPoint(380, cy + 15), "40");
        p.drawLine(QPoint(320, cy), QPoint(320, cy + 3));
        p.drawText(QPoint(320, cy + 15), "60");
        p.drawLine(QPoint(260, cy), QPoint(260, cy + 3));
        p.drawText(QPoint(260, cy + 15), "80");
        p.drawLine(QPoint(200, cy), QPoint(200, cy + 3));
        p.drawText(QPoint(200, cy + 15), "100");
        p.drawLine(QPoint(140, cy), QPoint(140, cy + 3));
        p.drawText(QPoint(140, cy + 15), "120");
        p.drawLine(QPoint(80, cy), QPoint(80, cy + 3));
        p.drawText(QPoint(80, cy + 15), "140");
        foreach (GraphData *tg, peaks) {
            int cx = 500 - (int)(tg->value * 3.0);
            int intense = tg->intensity * 30;

            if (intense > 90)
                intense = 90;
            p.setPen(tg->pcolor);
            p.drawLine(QPoint(cx, cy), QPoint(cx, cy - intense));
        }
        cy = cy + 45;
    }
    if (datatype == 2) // IR
        cy = cy + 20;
    if (datatype == 3) { // 1H-NMR
        cy = cy + 90;
        // draw baseline
        p.drawLine(QPoint(50, cy), QPoint(550, cy));
        p.drawLine(QPoint(500, cy), QPoint(500, cy + 3));
        p.drawText(QPoint(500, cy + 15), "-2 ppm");
        p.drawLine(QPoint(440, cy), QPoint(440, cy + 3));
        p.drawText(QPoint(440, cy + 15), "0");
        p.drawLine(QPoint(380, cy), QPoint(380, cy + 3));
        p.drawText(QPoint(380, cy + 15), "2");
        p.drawLine(QPoint(320, cy), QPoint(320, cy + 3));
        p.drawText(QPoint(320, cy + 15), "4");
        p.drawLine(QPoint(260, cy), QPoint(260, cy + 3));
        p.drawText(QPoint(260, cy + 15), "6");
        p.drawLine(QPoint(200, cy), QPoint(200, cy + 3));
        p.drawText(QPoint(200, cy + 15), "8");
        p.drawLine(QPoint(140, cy), QPoint(140, cy + 3));
        p.drawText(QPoint(140, cy + 15), "10");
        p.drawLine(QPoint(80, cy), QPoint(80, cy + 3));
        p.drawText(QPoint(80, cy + 15), "12");
        foreach (GraphData *tg, peaks) {
            int cx = 440 - (int)(tg->value * 30.0);
            int intense = tg->intensity * 10;

            if (intense > 90)
                intense = 90;
            p.setPen(tg->pcolor);
            p.drawLine(QPoint(cx, cy), QPoint(cx, cy - intense));
        }
        cy = cy + 45;
    }

    foreach (GraphData *tg, peaks) {
        s.setNum(tg->intensity);
        ft = tg->fulltext;
        if (datatype == 1) { // 13C-NMR
            ft.append(", intensity ");
            ft.append(s);
        }
        p.setPen(tg->pcolor);
        if (tg->pcolor == QColor(0, 0, 255))
            cmp2 = true;
        p.drawText(QPoint(cx, cy), ft);
        // And now to clipboard
        m_toclip += ft + "\n";
        cy = cy + 15;
    }
    if (cmp2) {
        p.setPen(QColor(255, 0, 0));
        p.drawText(QPoint(cx, cy), tr("Red is reactant"));
        cy = cy + 15;
        p.setPen(QColor(0, 0, 255));
        p.drawText(QPoint(cx, cy), tr("Blue is product"));
    }

    p.end();
}

void GraphWidget::PrintSetup() {
    printer = new QPrinter;
    printer->setFullPage(true);
    printer->setPageSize(QPrinter::Letter);
    printer->setOrientation(QPrinter::Portrait);
    printer->setColorMode(QPrinter::GrayScale);
}

void GraphWidget::Print() {
    qDebug() << "Graph::Print";
    QPrintDialog dialog(printer, this);
    if (!dialog.exec()) {
        return;
    }

    output = 1;
    update();
    output = 0;
    update();
}

void GraphWidget::AddPeak(double v1, QColor c1, QString l1, QString t1) {
    GraphData *g1 = new GraphData;

    g1->value = v1;
    g1->fulltext = t1;
    g1->pcolor = c1;
    if (l1.isEmpty() == false) {
        g1->label = l1;
        g1->drawlabel = true;
    }
    // check for duplicate peaks
    foreach (GraphData *tg, peaks) {
        if (tg->value == v1) {
            tg->intensity++;
            return;
        }
    }
    peaks.append(g1);
}

void GraphWidget::AddPeak(double v1, int m1, QColor c1, QString l1, QString t1) {
    GraphData *g1 = new GraphData;

    g1->value = v1;
    g1->fulltext = t1;
    g1->pcolor = c1;
    g1->intensity = m1;
    if (l1.isEmpty() == false) {
        g1->label = l1;
        g1->drawlabel = true;
    }
    peaks.append(g1);
}

void GraphWidget::Export() {
    QClipboard *clip = QApplication::clipboard();

    if (m_toclip != "")
        clip->setText(m_toclip);
}

// cmake#include "graphwidget.moc"
