#ifndef _H_AUDIO_STREAM_H
#define _H_AUDIO_STREAM_H

#define STB_VORBIS_HEADER_ONLY
#include "thirdparty/stb_vorbis.c"

#include "utility/logger.h"
#include "util.h"

#include <AL/al.h>
#include <AL/alc.h>
#include <string>
#include <queue>
#include <mutex>
#include <thread>

class AudioStream
{
public:
	AudioStream();

	bool PlayStream();
	void AddToQueue(std::string songName);
	void PauseStream() { paused = !paused; }

	void kill() { stopThread = true; }
	~AudioStream();
private:
	std::queue<std::string> songNames;
	std::mutex mutex;
	std::atomic<bool> stopThread;
	std::atomic<bool> paused;
	std::string CurStreamName();
	bool eof;
	std::thread streamThread;

	int loadInitalChunk();
	int loadNextChunk();

	int loadChunk(ALuint buffer);

	void popQueue();
	void cycleQueue();

	bool openFile(std::string fileName);

	void stream();

	short *data;
	stb_vorbis *file;
	stb_vorbis_info info;
	ALuint buffers[4];
	ALuint source;
	ALint state;
	ALint format;
	size_t last_buffer;
};

#endif // _H_AUDIO_STREAM_H
