#ifndef IMAGEWORKER_H
#define IMAGEWORKER_H

#include "../image.h"
#include <QFileDialog>
#include <QImage>
#include <QObject>
#include <QPixmap>

class ImageWorker : public QObject {
  Q_OBJECT
public:
  ImageWorker();
  bool save(QString name);         // save image
  bool saveLine(QString name);     // save selected line
  bool saveLineData(QString name); // save selected line data

  void setInterpolate(bool stat) {
    this->doInterpolation = stat;
  } // set interpolate status
  bool getInterpolate() { return this->doInterpolation; } // get interpolate

  bool getLinearModeApp() { return this->linearModeApp; } // set linear mode
  void setLinearModeApp(bool set) {
    this->linearModeApp = set;
  } // get linear mode

  int getLine() { return this->line; }    // get selected line number
  void setLine(int l) { this->line = l; } // set selected line number

  int getPendingLine() {
    return this->pendingLine;
  } // get pending select line number
  void setPendingLine(int l) {
    this->pendingLine = l;
  } // set pending select line number

  void setOnline(bool l) { this->online = l; } // set if online transfer

private:
  Image currentImg;             // current image
  Image currentImgInterpolated; // current image interpolated (only RGB)
  QImage qimg;                  // current QImage

  double viewScale = 1;     // current view scale area mode
  double viewScaleLine = 1; // current view scale linear mode

  bool preview;           // do ROI preview
  bool doInterpolation;   // do interpolation
  bool interpolationDone; // interpolation done on current image
  bool linearModeApp;     // linear mode
  bool online;            // online transfer
  QRect ROIPreview;       // ROI preview rectangle

  int line;        // selected line
  int pendingLine; // pending line

  int x1;         // cursor 1 x position
  int x2;         // cursor 2 x position
  bool c1enabled; // cursor 1 enabled
  bool c2enabled; // cursor 2 enabled

  void drawRectangle(QImage *image, int beginX, int beginY, int widhtX,
                     int heightY, QColor color); // draw rectangle to image
  void interpolate(bool beginWithRG,
                   bool beginWithG); // do Bayer mask interpolation

  void interpolate4Q(bool beginWithRG,
                     bool beginWithG); // Quadrapolar interpolation
  void interpolateNN();                // Nearest Neighbor interpolation

  void emitCursors(bool c1, bool c2); // emit cursors values to GUI

signals:
  void showImg(const QPixmap &);     // show image area mode
  void showImgL(const QPixmap &);    // show image linear mode
  void showImgLine(const QPixmap &); // show selected line
  void showGraph(Image &, uint32_t); // show graph
  void cursor1y(const QString &);    // emit cursor 1 brighntess
  void cursor2y(const QString &);    // emit cursor 2 brighntess
  void y1my2(const QString &);       // emit cursors brighntess difference
  void x1mx2(const QString &);       // emit cursors position difference

public slots:
  void catchImg(Image *img); // catch image
  void reDrawAll();          // redraw all
  void reDrawAreaMode();     // redraw area mode image
  void reDrawLine();         // redraw selected line
  void reDrawLineArea();     // redraw linear mode image
  void setPreviewState(bool state) {
    this->preview = state;
  } // set ROI preview state
  void setROIPreview(QRect roi) { this->ROIPreview = roi; } // set ROI window
  void setViewScale(double scale) {
    this->viewScale = scale;
  } // set view scale area mode
  void setViewScaleL(double scale) {
    this->viewScaleLine = scale;
  }                                  // set view scale linear mode
  void updateViewScale(double upd) { // update view scale
    this->viewScale += upd;
    this->viewScaleLine += upd;
  }
  void setInterpolateState(int state); // set RGB interpolation
  void cursor1Enabled(int);            // set cursor 1 enable
  void cursor2Enabled(int);            // set cursor 2 enable
  void cursor1x(int);                  // set cursor 1 position
  void cursor2x(int);                  // set cursor 2 position
};

#endif // IMAGEWORKER_H
