#ifndef KYLINCAMERA_H
#define KYLINCAMERA_H

#include <QObject>
#include <QtMultimedia/QAudioInput>
#include "kylincamera_global.h"
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>

#include "../kylinlogger/kylinlogger_CPP/kylinlogger_cpp.h"
extern "C" {
#include "../kylinlogger/kylinlogger_C/kylinlogger_c.h"
}

class VideoDisplay;
class VideoDisplayGL;

enum CameraOptions {
    CAMERA_NONE = 0,
    CAMERA_CAPTURE,
    CAMERA_DECODE,
    CAMERA_ENCODE,
    CAMERA_PHOTO
};

enum CameraState {
    CAMERA_STATE_NONE = 0,
    CAMERA_CAPTURE_READ,
    CAMERA_CAPTURE_READ_ERR,
    CAMERA_DECODE_OK,
    CAMERA_DECODE_ERR,
    CAMERA_ENCODE_START,
    CAMERA_ENCODE_STOP,
    CAMERA_PHOTO_BUZY,
    CAMERA_PHOTO_FREE
};

#define WHITE_BALANCE       V4L2_CID_WHITE_BALANCE_TEMPERATURE    //白平衡色温
#define BRIGHTNESS          V4L2_CID_BRIGHTNESS                   //亮度
#define CONTRAST            V4L2_CID_CONTRAST                     //对比度
#define SATURATION          V4L2_CID_SATURATION                   //饱和度
#define HUE                 V4L2_CID_HUE                          //色度
#define SHARPNESS           V4L2_CID_SHARPNESS                    //锐度
#define BACKL_COMP          V4L2_CID_BACKLIGHT_COMPENSATION       //背光补偿
#define GAMMA               V4L2_CID_GAMMA                         //伽玛值
#define COLORFX             V4L2_CID_COLORFX                      //色调

enum colorfx {
    COLORFX_NONE			= 0,
    COLORFX_BW				= 1,
    COLORFX_SEPIA			= 2,
    COLORFX_NEGATIVE		= 3,
    COLORFX_EMBOSS			= 4,
    COLORFX_SKETCH			= 5,
    COLORFX_SKY_BLUE		= 6,
    COLORFX_GRASS_GREEN		= 7,
    COLORFX_SKIN_WHITEN		= 8,
    COLORFX_VIVID			= 9,
    COLORFX_AQUA			= 10,
    COLORFX_ART_FREEZE		= 11,
    COLORFX_SILHOUETTE		= 12,
    COLORFX_SOLARIZATION	= 13,
    COLORFX_ANTIQUE			= 14,
    COLORFX_SET_CBCR		= 15,
};

#define EXPOSURE_AUTO       V4L2_CID_EXPOSURE_AUTO                      //自动曝光

enum  camera_exposure_auto_type {
    CAMERA_EXPOSURE_AUTO = 0,
    CAMERA_EXPOSURE_MANUAL = 1,
    CAMERA_EXPOSURE_SHUTTER_PRIORITY = 2,
    CAMERA_EXPOSURE_APERTURE_PRIORITY = 3
};
#define EXPOSURE_ABSOLUTE             V4L2_CID_EXPOSURE_ABSOLUTE		//手动曝光时间
#define EXPOSURE_AUTO_PRIORITY        V4L2_CID_EXPOSURE_AUTO_PRIORITY   //

class KylinCamera : public QObject
{
    Q_OBJECT
public:
    explicit KylinCamera(QObject *parent = nullptr);
    ~KylinCamera();

    V4l2DeviceInfo device_info;

    KylinCameraInfo *next_dinfo;

    VideoDisplayGL *create(QWidget *parent, KylinCameraInfo *device);
    static int enum_device(V4l2DeviceInfo *device);
    static int fourcc(unsigned int format);
    static int camera_log_init(int verbose);

    void camera_capture_pause();
    void camera_capture_restore();
    int camera_set_param(KylinCameraInfo *device);
    int camera_get_param(KylinCameraInfo *device);
    int camera_get_mode();
    int camera_take_photo(char *photopath );
    int camera_start_record(char *videopath, bool hwenc=false);
    int camera_start_record(char *videopath, int enclevel = -1, bool hwenc = false);
    int camera_stop_record();
    int camera_record_pause(bool on);
    long long camera_get_record_time();
    int camera_set_contrl(int id, int value);
    int camera_get_contrl(int id, int *pvalue);
    int camera_query_contrl(int id, struct v4l2_queryctrl *pvalue);
//    int camera_video_enable(bool enable);

    int camera_audio_init(const QAudioDeviceInfo &deviceInfo);
    int camera_audio_disable();

    void camera_audio_volume_set(qreal volume);
    qreal camera_audio_volume();

    enum CameraState camera_get_state(enum CameraOptions opt);
    void camera_display_mirror(bool on);
    void camera_record_mirror(bool on);
    void camera_photo_mirror(bool on);

    void camera_set_background(QColor color);
    void camera_set_cover(float cover);

public Q_SLOTS:
    void camera_decoder_reinit();

Q_SIGNALS:
    void camera_photo_error();

private:
    VideoDisplayGL *videoWiget;

    static int enum_frame_intervals(int fd, TypePixFormats *pixfmt, TypeFramesizes *fsize);
    static int enum_frame_sizes(int fd, TypePixFormats *pixfmt);
    unsigned long m_rate;           //sample rate
    unsigned int m_channels;	       //audio channels
    unsigned long m_bit_size;           //sample bit size
};

#endif // KYLINCAMERA_H
