#ifndef DRAWPOINTSDIALOG_H
#define DRAWPOINTSDIALOG_H

#include "ui_drawpointsdialog.h"
#include <QtGui>


class DrawPointsDialogScene : public QGraphicsScene{
    Q_OBJECT
public:
    DrawPointsDialogScene(QWidget *parent): QGraphicsScene(parent){}
    ~DrawPointsDialogScene(){}

protected:
    //To avoid all that weird mouse grabber stuff..
    void mousePressEvent(QGraphicsSceneMouseEvent *event){
        emit addPoint(event->scenePos());
    }

signals:
    void addPoint(QPointF point);

};

namespace Ui{
    class DrawPointsDialog;
}

class DrawPointsDialog : public QDialog{
    Q_OBJECT
public:
    DrawPointsDialog(QString fileName, QString *variable,
                     QString *value, QWidget *parent = 0)
        :QDialog(parent), ui(new Ui::DrawPointsDialog),
        value(value), variable(variable){
        ui->setupUi(this);
        scene = new DrawPointsDialogScene(this);
        ui->graphicsView->setScene(scene);

        pixmap = new QPixmap(fileName);
        p = new QGraphicsPixmapItem();
        p->setPixmap(*pixmap);
        scene->addItem(p);

        ui->graphicsView->show();

        dots = new QList<QGraphicsEllipseItem*>;

        connect(ui->zoomInButton, SIGNAL(clicked()), ui->graphicsView, SLOT(zoomIn()));
        connect(ui->zoomOutButton, SIGNAL(clicked()), ui->graphicsView, SLOT(zoomOut()));
        connect(scene, SIGNAL(addPoint(QPointF)), this, SLOT(addPoint(QPointF)));
    }

private:
    Ui::DrawPointsDialog *ui;
    QGraphicsScene *scene;
    QPixmap *pixmap;
    QGraphicsPixmapItem *p;

    QString *variable, *value;

    //Finish implementation!
    QList<QGraphicsEllipseItem*> *dots;

public slots:
    void addPoint(QPointF point){
        QPointF itemPoint = p->mapFromScene(point);  //Converts to item coords
        if(p->contains(itemPoint)){
            if(ui->valueE->text().isEmpty()){
                ui->valueE->setText(QString::number(round(itemPoint.x()), 'g') + ":" + QString::number(round(itemPoint.y()), 'g'));
            }else
            {
                ui->valueE->insert("," + QString::number(round(itemPoint.x()), 'g') + ":" + QString::number(round(itemPoint.y()), 'g'));
            }

            if(pixmap->toImage().valid(point.toPoint())){
                //This isnt centered,and the dots should scale
                dots->append(new QGraphicsEllipseItem(point.x()-2, point.y()-2, 5,5, p, scene));
                if(QColor::fromRgb(pixmap->toImage().pixel(point.toPoint())).value() > 128){
                    dots->last()->setBrush(QBrush(Qt::black, Qt::SolidPattern));
                }else{
                    dots->last()->setBrush(QBrush(Qt::white, Qt::SolidPattern));
                }
            }else{
                //TODO Should be consoleout.
                //This case should never happen!
            }
        }
    }

private slots:
    void on_buttonBox_accepted(){
        *this->variable = ui->variableE->text();
        *this->value = ui->valueE->text();
    }
    void on_valueClearButton_clicked(){
        this->ui->valueE->clear();
        for(int i= 0;i<dots->size();++i){
            scene->removeItem(dots->at(i));
        }
        dots->clear();
    }
};

#endif // DRAWPOINTSDIALOG_H
