/* #########################################
 * Traffic monitoring using Raspberry PI
 * Author: Michal Zacpal
 * MAIN WINDOW FILE
 * 2015
 * ######################################### */
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <QImage>
#include <QTimer>
#include <QDateTime>
#include <QString>
#include <QtGui>
#include <ctime>
#include "wiringPi.h"
#include "raspicam/raspicam_cv.h"

#include "RaspiTemp.h"
#include "CameraTools.h"
#include "ImageProcessing.h"
#include "basefunctions.h"
#include "roadlane.h"

#define SensTempOut "28-0000059d81c7"
#define SensTempIn "28-0000059dfe09"
// #define REAR_TVTM_INIT 5
#define CAR_CENTER 0
#define CAR_LEFT 1
#define CAR_RIGHT 2
#define MAXITER 10
#define MAX_LANE_WIDTH 40
#define MIN_LANE_WIDTH 10
#define MAX_LANE_BUFFER_SIZE 4

// Setting GPIO pins
/*
 * Pay attention for different markings BCM vs. wPI
 */
#define KEY_PIN 0            // BCM(GPI17) wPI(GPIO0)
#define POWER_PIN 26         // BCM(GPI12) wPI(GPI26)
#define SOUND_PIN 2          // BCM(GPI27) wPI(GPIO2)

using namespace std;
using namespace cv;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:

    Ui::MainWindow *ui;
    // Bool
    bool bFrontCamActive;
    bool bRearCamActive;
    bool bSound;            // Enable or disable sound
    bool bSidePanel;        // Show or hide side panel
    bool bPopUpWindow;      // pop Upwindow si active
    bool bSettingsWin;  // Settings win. is active
    bool bCamWin;       // Camera win. is active
    bool bLDWWin;       // Lane departure warning win. is active
    bool bLDWinPic;     // Display lane departre pictures
    bool bGrabbing;     // Grabbing is activated
    bool bGrabFront;    // Grabing from front cam
    bool bGrabRear;    // Grabing from rear cam
    bool bRearTVTM;
    bool bParkView;
    bool bLDWSwasAct;
    bool bPrintTemp;
    bool bLDWROISet;
    bool bLDWSActive;
    bool bKey;
    // Double
    double f;               // TVTM Front CAM param.
    double H;               // TVTM Front CAM param.
    double theta;           // TVTM Front CAM param.
    double rearf;           // TVTM Rear CAM param.
    double rearH;           // TVTM Rear CAM param.
    double rearTheta;       // TVTM Rear CAM param.
    // Int
    int iGrabPeriod;        // Grabbing period[ms]
    int iSDCount;           // ShutDown counting
    int iROIFramePos;       // Position of frame ROI for LDWS
    int iprevROIFramePos;       // Position of frame ROI for LDWS
    int iRoiH;                  // Height of region of interest
    int iLaneOffset;            // Offset for lane crop
    int iMaxLaneWidth;
    int iMinLaneWidth;
    int iHeight;        // Height of the MainWindow
    int iWidth;         // Width of the MainWindow
    int CenterX;        // Center of vehicle
    int iFrontCam;      // Front cam number (For videocapture)
    int iRearCam;       // Rear cam number (For videocapture)
    int iPic;           //
    int iRoiRowsF;      // Region of interest first row
    int iRoiRowsL;      // Region of interest last row
    //Mat
    Mat matFrontImg;                    // Captured image from front CAM
    Mat matRearImg;                    // Captured image from rear CAM
    Mat DispLDWs;       // For Displaying TVT image
    Mat RearBEV;        // Rear bird eye view
    Mat _ParkView;      // Rear TVTM
    Mat RoiImg;         // Region os interes
    Mat GrabbedImg;     //
    Mat inputImg;       //
    // QString
    QString Path;
    //QTime
    QTimer* tmrTimer;        // Timer - Process frame & update GUI
    QTimer* tmrGrab;         // Grabbing timer
    QTimer* tmrShutDown;     // Timer for shutdown
    QTimer* tmrTemperature;  // Timer for temperature print
    QTime TripStart;         // time of trip start
    QTime ActTime;           // actual Time
    // RoadLane
    RoadLane RightLane;     // Right road lane
    RoadLane LeftLane;      // Left Road lane
    // Raspicam
    RaspiCam_Cv VidFrontCam;         // Front camera raspberry pi module
    // TVTM
    TVTM TVTModel;
    TVTM rearTV;            // Rear bird TOP view
    // Vector
    vector<int> LeftLaneArr;    //
    vector<int> RightLaneArr;   //
    // VideoCapture
    VideoCapture VidRearCam;       // Camera - Rear
//  VideoCapture VidFrontCam;      // Camera - front
private slots:

    bool RearTVTMInit();                        //
    bool StartFrontCam();           // Start front cam
    bool StartRearCam();                        //
    bool TVTMInit();                            //
    int DetermineLane(vector<int> LaneBuffer);  //
    Mat DrawLane(RoadLane Lines, Mat InImg);    // Draw lane in to TVTM image
    Mat LDWRoutine(Mat InImg);                  // Lane detection routine
    Mat ParkView(Mat inImg);                    //
    RoadLane ReturnLane(Mat Lane, int iPos, int iLaneOffset, int iMinLaneWidth, int iMaxLaneWidth); //
    void AllFramesInvisible();      // make all frames invisible
    void InfoAlarm(int iAlarm);             // Display actual warning, alarm
    void InfoMessage(QString sMessage, bool bBeep);   // Display info mesage with sound if it is enabled
    void StopFrontCam();            // Stop front cam
    void on_btn_sound_clicked();                // Disable/enable sound
    void on_btn_ShowHideSideFrame_clicked();    //
    void on_btn_minim_clicked();                // Minimize window
    void on_btn_maxim_clicked();                // maxiamize window
    void on_btnSettings_clicked();              // Settings
    void FillPopUpWin(QString qsTitle);         //
    void on_btn_ClosePUWin_clicked();           //
    void on_btn_SavePPWin_clicked();            //
    void MoveCar(int position);                 // Move with animated car
    void StopRearCam();                         //
    void GPIOInit();                            // Initialisation of GPIO
    void CheckKeyIngnition();
    void PlaySound();
    // Buttons
    void on_btnCam_clicked();
    void on_btnResizeFront_clicked();
    void on_btnFrontCam_clicked();
    void on_btnResizeRear_clicked();
    void on_btnRearCam_clicked();
    void on_btnLDW_clicked();
    void on_btnShutDown_clicked();
    void on_btnLDWSetROI_clicked();
    void on_btnROIUP_clicked();
    void on_btnROIDown_clicked();
    void on_btn_time_clicked();
    void on_btn_RearCamInit_clicked();
public slots:
    void PrintTmpRoutine();             // Temperature timer
    void processFrameAndupdateGUI();    // Timer
    void SaveImgTimerRoutine();         // Grabbing timer
    void ShutDownCounting();            // Counting timer
};
#endif // MAINWINDOW_H
