#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
	{
	public:
		std::unique_ptr<CFilePlayer> _playerClient = std::make_unique<CFilePlayer>();
	protected:
		std::string  _url;
		double		_imageRotateDeg = 0;
		cv::Mat		_pLastFrame;
		std::string _lastJpegImage;
		int			_imageWidth = 0, _imageHeight = 0;
		int64_t		_pts = 0;
		bool		_isPlaying = false;
		std::recursive_mutex		_mutex;

		// Display/inference split (same pattern as ANSVideoPlayer)
		int _displayWidth = 0;
		int _displayHeight = 0;
		cv::Mat _inferenceImage;         // full-res, no display resize
	public:
		// Public: accessed by extern "C" GetFilePlayerCVImage for NV12/CUDA registry attachment
		cv::Mat _inferenceCloneCurr;     // current clone for registry (keeps data alive)
		cv::Mat _inferenceClonePrev;     // previous clone (keeps data alive until next frame)

		ANSFILEPLAYER();
		~ANSFILEPLAYER() noexcept;
		[[nodiscard]] bool Init(std::string licenseKey, std::string url);
		[[nodiscard]] bool Setup();
		[[nodiscard]] bool Reconnect();
		[[nodiscard]] bool Start();
		void SetBBox(cv::Rect bbox);
		void SetCrop(bool crop);
		[[nodiscard]] bool IsPaused();
		[[nodiscard]] bool IsPlaying();
		[[nodiscard]] bool IsRecording();
		[[nodiscard]] bool Stop();
		void Destroy();
		void EnableAudio(bool status);
		void SetAudioVolume(int volume);
		void SetImageRotate(double mode) {
			std::lock_guard<std::recursive_mutex> lock(_mutex);
			_imageRotateDeg = mode;
			if (mode > 360) _imageRotateDeg = 360;
		};
		void SetDisplayResolution(int width, int height);
		[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
		[[nodiscard]] cv::Mat GetInferenceImage();
		[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
		[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts) {
			return GetJpegImage(width, height, pts);
		}
		[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
	public:
		void CheckLicense();
		SPDLogger&      _logger = SPDLogger::GetInstance("ANSFilePlayer", false);
		bool            _licenseValid{ false };
		std::string     _licenseKey;
	};
}
extern "C" __declspec(dllexport) int CreateANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle, const char* licenseKey,  const char* url);
extern "C" __declspec(dllexport) int ReleaseANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int volume);
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int status);
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSFILEPLAYER* *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height);

// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume_V2(uint64_t handleVal, int volume);
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t handleVal, int status);
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation_V2(uint64_t handleVal, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal, int cropFlag);
#endif