#ifndef ANSMJPEG_H
#define ANSMJPEG_H
#define ANSMJPEG_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "http_mjpeg_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "http_mjpeg_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>

namespace ANSCENTER
{
	class ANSMJPEG_API ANSMJPEGClient
	{
	protected:
		std::unique_ptr<CHttpMjpegPlayer> _playerClient = std::make_unique<CHttpMjpegPlayer>();
		std::string  _username;
		std::string  _password;
		std::string  _url;
		bool		 _useFullURL;
		int _imageRotateDeg = 0;
		int _displayWidth = 0;   // 0 = no resize (return original resolution)
		int _displayHeight = 0;
		cv::Mat  _pLastFrame;
		std::string _lastJpegImage;
		int		 _imageWidth, _imageHeight;
		int64_t  _pts;
		bool	 _isPlaying;
		std::recursive_mutex		_mutex;
	public:
		ANSMJPEGClient();
		~ANSMJPEGClient() noexcept;
		[[nodiscard]] bool Init(std::string licenseKey, std::string url);
		[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
		[[nodiscard]] bool Setup();
		[[nodiscard]] bool Reconnect();
		[[nodiscard]] bool Start();
		[[nodiscard]] bool Pause();
		[[nodiscard]] bool IsPaused();
		[[nodiscard]] bool IsPlaying();
		[[nodiscard]] bool IsRecording();
		[[nodiscard]] bool Stop();
		void Destroy();
		void EnableAudio(bool status);
		void SetAudioVolume(int volume);
		bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
		[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
		[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
		[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
		void SetImageRotate(int mode) {
			std::lock_guard<std::recursive_mutex> lock(_mutex);
			_imageRotateDeg = mode;
			if (mode > 360) _imageRotateDeg = 360;
		};
		void SetBBox(cv::Rect bbox);
		void SetCrop(bool crop);
		static void SetMaxHWDecoders(int maxDecoders);
		static int  AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
		void SetHWDecoding(int hwMode, int preferredGpu = -1);
		bool IsHWDecodingActive();
		int  GetHWDecodingGpuIndex();
		void SetDisplayResolution(int width, int height);  // Set display output size; 0,0 = original (no resize)
		void SetImageQuality(int mode);  // 0=fast (AI), 1=quality (display)
		AVFrame* GetNV12Frame();  // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
		AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
		bool     IsCudaHWAccel();  // true when decoder uses CUDA (NV12 stays in GPU VRAM)
	public:
		void CheckLicense();
		SPDLogger&      _logger = SPDLogger::GetInstance("ANSMJPEG", false);
		bool            _licenseValid{ false };
		std::string     _licenseKey;
		//std::once_flag  licenseOnceFlag;

	};
}
extern "C" __declspec(dllexport) int CreateANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int GetMJPEGImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetMJPEGStrImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);

extern "C" __declspec(dllexport) int StartMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int StopMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int PauseMJPEG(ANSCENTER::ANSMJPEGClient** Handle);

extern "C" __declspec(dllexport) int IsMJPEGPaused(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int IsMJPEGRunning(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int IsMJPEGRecording(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) void SetMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetMJPEGImageRotation(ANSCENTER::ANSMJPEGClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetMJPEGMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int  AutoConfigureMJPEGHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetMJPEGHWDecoding(ANSCENTER::ANSMJPEGClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int  IsMJPEGHWDecodingActive(ANSCENTER::ANSMJPEGClient** Handle);
extern "C" __declspec(dllexport) int  GetMJPEGHWDecodingGpuIndex(ANSCENTER::ANSMJPEGClient** Handle);
extern "C" __declspec(dllexport) void SetMJPEGImageQuality(ANSCENTER::ANSMJPEGClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height);
#endif