#ifndef _LI_DVDRIPPER
#define _LI_DVDRIPPER 1
#include <iostream>
#include <dvdcss.h>
#include <thread>
#include <string>
#include <fstream>

#include "../Scsi/IoCtl.hpp"
#include "../Scsi/OpticalDrive.hpp"

namespace Li::Dvd {
	class DvdRipper {
	private:
		bool error;
		bool done;
		std::string errorMsg;		

		bool inFile;
		uint32_t fileReadSoFar;
		uint32_t fileRemain;

		uint32_t sectorsAtOnce;
		uint32_t sectorsReadSoFar;

		std::string outputPath;
		std::thread* ripinThread;

		bool keepRunning;
		bool decrypt;

		std::ofstream* iso;
		std::byte* tmpBuffer;

		std::byte* titleKey;

		Li::Scsi::IoCtl* driveIoCtl;
		Li::Scsi::OpticalDrive* drive;
		void ripThread();
		void readNonFileSectors();
		void readFileSectors();

		void readUnprotectedSectors();
		void readCssSectors();

		int read1SectorATimeSkippingBadSectors(int toRead);
	public:
		DvdRipper(Li::Scsi::OpticalDrive* opticalDrive, std::string outputISO, uint32_t sectorsAtOnce);
		~DvdRipper();
		
		uint32_t SectorsReadSoFar();
		float PercentDone();
		std::string OutputPath();

		uint32_t FileSoFar();
		uint32_t FileLen();

		bool Decrypt();
		std::byte* GetTitleKey();
		std::byte* GetDiscKey();

		bool Done();
		bool Error();
		std::string ErrorMessage();

		void StartRip(uint32_t driveSpeed, bool decryptCss);
		void EndRip();
	};
}
#endif