#ifndef ANSFILEPLAYER_H
#define ANSFILEPLAYER_H
#define ANSFILEPLAYER_API __declspec(dllexport)
#include <iostream>
#include <cstdint>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "rtsp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "file_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <thread>
#include <chrono>
#include <atomic>
#include <turbojpeg.h>

namespace ANSCENTER
{
	class ANSFILEPLAYER_API ANSFILEPLAYER_CV
	{
	protected:
		cv::VideoCapture cap;
		int  _resWidth;
		int  _resHeight;
		int64_t  _totalFrames;
		int64_t  _currentFrame;
		int64_t  _previousPTS;
		double _fps;
		bool		_isPlaying;
		std::string _lastJpegImage;
		//int64_t _previousPTS;
		cv::Mat _previousImage;
		bool m_bPaused;
		int _imageRotateDeg = 0;
		std::string  _url;
		cv::Rect	 _bbox;
		bool		 _crop;
		std::recursive_mutex		_mutex;
		std::shared_ptr<std::atomic<bool>> _stopThread;
		std::shared_ptr<std::thread> _timerThread;
		std::mutex _cvMutex;
		std::condition_variable _cv;
		//EngineType		engineType;
		//// Initialize nvJPEG structures
		//nvjpegHandle_t nv_handle;
		//nvjpegEncoderState_t nv_enc_state;
		//nvjpegEncoderParams_t nv_enc_params;
		//cudaStream_t stream;
		tjhandle _jpegCompressor;

	public:
		ANSFILEPLAYER_CV();
		~ANSFILEPLAYER_CV() noexcept;
		[[nodiscard]] bool Init(std::string licenseKey, std::string url);
		[[nodiscard]] bool Setup();
		[[nodiscard]] bool Reconnect();
		[[nodiscard]] bool Start();
		[[nodiscard]] bool IsPaused();
		[[nodiscard]] bool IsPlaying();
		[[nodiscard]] bool IsRecording();
		void SetBBox(cv::Rect bbox);
		void SetCrop(bool crop);
		[[nodiscard]] bool Stop();
		void Destroy();
		void EnableAudio(bool status);
		void SetAudioVolume(int volume);
		void SetImageRotate(int mode) {
			std::lock_guard<std::recursive_mutex> lock(_mutex);
			_imageRotateDeg = mode;
			if (mode > 360) _imageRotateDeg = 360;
		};
		[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
		[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
		[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
	public:
		void CheckLicense();
		SPDLogger& _logger = SPDLogger::GetInstance("ANSFilePlayer_CV", false);
		bool            _licenseValid{ false };
		//std::once_flag	licenseOnceFlag;
		std::string     _licenseKey;
	private:
		std::string encodeJpegString(const cv::Mat& img, int quality = 80);
		//std::string encodeMatToJpegWithNvJPEG(const cv::Mat& inputMat, int quality = 80);
		//void uploadPlanarBGRToGPU(const cv::Mat& inputMat, unsigned char** data);
		void StartTimer(std::shared_ptr<std::atomic<bool>> stopFlag, int interval_ms);
		void StopTimerThread();
	};
}
extern "C" __declspec(dllexport) int CreateANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int GetFileCVPlayerImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFileCVPlayerStrImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage);
extern "C" __declspec(dllexport) int GetFileCVPlayerCVImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int ReconnectFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int StopFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerPaused(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerRunning(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerRecording(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) void SetFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int volume);
extern "C" __declspec(dllexport) void EnableFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int status);
extern "C" __declspec(dllexport) void SetFileCVPlayerImageRotation(ANSCENTER::ANSFILEPLAYER_CV** Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int cropFlag);


#endif