#ifndef BASIC_GL_CAMERA_H_
#define BASIC_GL_CAMERA_H_

#define NBOARDS      40      // max. number of boards
#define NPIX       1440      // max. number of pixels
#define NTMARK      160      // max. number of timeMarker signals

#define MAX_NUM_PIXELS 1600
#define ACTUAL_NUM_PIXELS 1438

#include <QtOpenGL/QGLWidget>
#include <QtGui/QMouseEvent>
#include <vector>

#include <QtGui/QRadioButton>
#include <QtGui/QLabel>
#include <QtGui/QButtonGroup>

#include <set>

using namespace std;
//#include <QtGui/QMouseEvent>
///structure for storing edges of hexagons (for blurry display)
struct edge
{
    int first;
    int second;
};

///structure for storing neighbors of pixels. For camera position calculation and blurry display
struct PixelsNeighbors
{
    //neighbors. clockwise, starting from top
    int neighbors[6];
    PixelsNeighbors()
    {
        for (int i=0;i<6;i++)
            neighbors[i] = -1;
    }
    int& operator[](int index){return neighbors[index];}
};

class BasicGlCamera : public QGLWidget
{
    Q_OBJECT

public:
    BasicGlCamera(QWidget* parent = 0);
    ~BasicGlCamera();

    int fWhite;
    int fWhitePatch;

    int64_t fMin;
    int64_t fMax;

    bool pixelColorUpToDate;

    GLfloat patchColour[3];
    GLfloat pixelContourColour[3];
    GLfloat patchesCoulour[3];
    GLfloat highlightedPatchesCoulour[3];
    GLfloat highlightedPixelsCoulour[3];
    GLfloat tooHighValueCoulour[3];
    GLfloat tooLowValueCoulour[3];

    string dataText;
    string unitsText;
    string titleText;

    void setUnits(const string& units);
    void setTitle(const string& title);
    void SetWhite(int idx);
    void SetMin(int64_t min);
    void SetMax(int64_t max);

    float ss[5];// = {0.00, 0.25, 0.5, 0.75, 1.00};
    float rr[5];// = {0.15, 0.00, 0.00, 1.00, 0.85};
    float gg[5];// = {0.15, 0.00, 1.00, 0.00, 0.85};
    float bb[5];// = {0.15, 1.00, 0.00, 0.00, 0.85};

public Q_SLOTS:
        void linearScalePlease(bool);
        void logScalePlease(bool);
        void regularPalettePlease(bool);
        void prettyPalettePlease(bool);
        void greyScalePalettePlease(bool);
        void glowingPalettePlease(bool);
        void zeroRotationPlease(bool);
        void plus90RotationPlease(bool);
        void minus90RotationPlease(bool);

Q_SIGNALS:
         void signalCurrentPixel(int pixel);
         void signalPixelMoveOver(int pixel);
         void signalPixelDoubleClick(int pixel);
         void colorPaletteHasChanged();



protected:
    void initializeGL();
    void resizeGL(int width, int height);
    virtual void paintGL();
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);
    virtual void drawCamera(bool alsoWire);
    virtual void drawPatches();
    int PixelAtPosition(const QPoint &pos);
    void drawHexagon(int index, bool solid);

    int fPixelStride;
    int fcSlice;
    vector<double>fData;

 //   bool recalcColorPlease;
    GLfloat pixelsCoords[MAX_NUM_PIXELS][3];
     PixelsNeighbors neighbors[MAX_NUM_PIXELS];
     int hardwareMapping[NPIX];
    GLfloat pixelsColor[NPIX][3];
    GLfloat verticesList[NPIX*6][2];
    vector<edge> patchesIndices[160];
    int verticesIndices[NPIX][6];
    int pixelsPatch[NPIX];
    int softwareMapping[NPIX];
    int patches[160][9];
    float shownSizex;
    float shownSizey;
    float pixelSize;
    void DrawCameraText();
    void UpdateText();
    void DrawScale();
    void toggleInterfaceDisplay();
    QRadioButton* linearButton;
    QRadioButton* logButton;
    QRadioButton* regularPaletteButton;
    QRadioButton* prettyPaletteButton;
    QRadioButton* greyScalePaletteButton;
    QRadioButton* glowingPaletteButton;
    QRadioButton* zeroRotationButton;
    QRadioButton* minus90RotationButton;
    QRadioButton* plus90Rotationbutton;
    QLabel*       scaleLabel;
    QLabel*       colorPaletteLabel;
    QLabel*       rotationLabel;
    QButtonGroup* scaleGroup;
    QButtonGroup* colorGroup;
    QButtonGroup* rotationGroup;


    bool logScale;
    int cameraRotation;
    void buildVerticesList();
    virtual void buildPatchesIndices();
    void updateNeighbors(int currentPixel);
    void calculatePixelsCoords();
private:
    void skipPixels(int start, int howMany);
    float hexRadius;
    float hexTolerance;
    float viewSize;
     int numVertices;

};

#endif
