#ifndef CV_PARAMS_H
#define CV_PARAMS_H

#include <QDialog>
#include <QComboBox>

#include "libs/camera/camera.h"
#include "libs/cv/line.h"
#include "libs/cv/mat_methods.h"
#include "libs/util/logger.h"
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>

#include <QMessageBox>
#include <QFileDialog>
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QTreeWidgetItem>
#include <QModelIndex>

namespace features{
    enum eItem{none, pre_processing, hsv_colors};

    enum eSide{front_position, side_position};
/*
    struct th{
        uint8_t H, L;
    };

    struct morph_th{
        th Hue, Saturation, Value;
    };

    struct morp_HSV{
        morph_th front, sideL, sideR;
    };*/
}//namespace_features

namespace Ui {
class cv_Params;
}

class cv_Params : public QDialog
{
    Q_OBJECT

public:
    explicit cv_Params(QWidget *parent = nullptr, Logger *logger = nullptr);
    ~cv_Params();

    void process();

    void setItem(features::eItem what);

    void unsetItem();

    void getParameters(tcpc::sCV_parameters& CV_parameters);

    void getParameters(tcpc::sHSV_boundaries& HSV_parameters);

    void setParameters(tcpc::sCV_parameters parameters);

    void setParameters(tcpc::sHSV_boundaries parameters);

    void detectHSV();

private slots:
    void on_load_IMG_button_clicked();

    void on_Params_tree_itemChanged(QTreeWidgetItem *item, int column);

    void on_Params_tree_itemDoubleClicked(QTreeWidgetItem *item, int column);

    void on_buttonBox_rejected();

    void on_cv_Params_rejected();

    void changedShowMask(QString text);

    void changedShowSurface(QString text);

    void changed_Hue_L(int value);

    void changed_Hue_H(int value);

    void changed_Saturation_L(int value);

    void changed_Saturation_H(int value);

    void changed_Value_L(int value);

    void changed_Value_H(int value);

    void on_save_IMG_button_clicked();

private:
    void generateShow(cv::Mat mat2show);

    void initParams_Tree();

private:
    Logger *log;
    Ui::cv_Params *ui;
    camera::frame frameRAW, frame;
    cv::Mat pre_processing_img;
    tcpc::sHSV_Mat_Pattern mask, maskedFrame;
    QImage image;
    features::eItem eWhat;
    features::eSide selected_HSV_side = features::eSide::front_position;
    tcpc::sHSV_boundaries HSV_parameters;
    tcpc::sCV_parameters cv_parameters, cv_default_parameters;
    bool maskFLAG = false;

    //Widget
    QTreeWidgetItem *canny_itm;
        QTreeWidgetItem *canny_L_TR;
        QTreeWidgetItem *canny_H_TR;
    QTreeWidgetItem *hough_itm;
        QTreeWidgetItem *hough_rho;
        QTreeWidgetItem *hough_theta;
        QTreeWidgetItem *hough_threshold;
        QTreeWidgetItem *hough_minLen;
        QTreeWidgetItem *hough_maxGap;
    QTreeWidgetItem *blur_itm;
        QTreeWidgetItem *blur_kernel;
    QTreeWidgetItem *dilate_itm;
        QTreeWidgetItem *dilate_thickness;
    QTreeWidgetItem *sort_itm;

    QTreeWidgetItem *sort_Xparallel;

    QTreeWidgetItem *sort_filterStright;
    QTreeWidgetItem *sort_Yintersec;

    QTreeWidgetItem *setSurf;
    QComboBox *surfBox;

    QTreeWidgetItem *hueL_itm;
        QSlider *hueL;
    QTreeWidgetItem *hueH_itm;
        QSlider *hueH;
    QTreeWidgetItem *saturL_itm;
        QSlider *saturL;
    QTreeWidgetItem *saturH_itm;
         QSlider *saturH;
    QTreeWidgetItem *valL_itm;
         QSlider *valL;
    QTreeWidgetItem *valH_itm;
         QSlider *valH;

    QTreeWidgetItem *setShow;
         QComboBox *showBox;
};

#endif // CV_PARAMS_H
