#pragma once
#include 
#include 
#include 

namespace dae
{
	class Ghost;
	class GhostThread
	{
	public:
		GhostThread(std::weak_ptr phost);
		~GhostThread();

		void Run();

		void TriggerUpdate() { m_ShouldUpdate = true; }
		void TriggerLateUpdate() { m_ShouldLateUpdate = true; }

		void Exit() 
		{
			if (m_Thread.joinable())
				m_Thread.detach();
		}
		static std::atomic GhostThreadUpdate;
		static std::atomic GhostThreadLateUpdate;
		static std::atomic GhostThreadRun;
		static std::mutex Mutex;
	private:
		std::shared_ptr m_pGhost;
		std::thread m_Thread;

		bool m_ShouldUpdate = false;
		bool m_ShouldLateUpdate = false;
		bool m_ShouldExit = false;
	};
}


#include "MiniginPCH.h"
#include "GhostThread.h"
#include "Characters/Ghost.h"
#include 

std::atomic dae::GhostThread::GhostThreadUpdate = 0;
std::atomic dae::GhostThread::GhostThreadLateUpdate = 0;
std::atomic dae::GhostThread::GhostThreadRun = true;
std::mutex dae::GhostThread::Mutex;

dae::GhostThread::GhostThread(std::weak_ptr ghost)
	:m_pGhost(ghost)
{
	m_Thread = std::thread(&GhostThread::Run, this);
}

dae::GhostThread::~GhostThread()
{
}

void dae::GhostThread::Run()
{
	while(GhostThreadRun)
	{
		if (!m_pGhost)
			break;

		if(m_ShouldUpdate &&GhostThreadUpdate > 0)
		{
			m_pGhost->Update();
			m_ShouldUpdate = false;
			--GhostThreadUpdate;
		}
		else if(m_ShouldLateUpdate &&GhostThreadLateUpdate > 0)
		{
			m_pGhost->LateUpdate();
			m_ShouldLateUpdate = false;
			--GhostThreadLateUpdate;
		}
	}
}