void SceneManager::pop()
{
	if (m_activeList.empty()) 
	{
		throw std::runtime_error("Attempted to pop from an empty game state stack");
	}

	bool poppingExclusive = false;
	if (m_activeList.back().second == Modality::Exclusive)
	{
		poppingExclusive = true;
	}

	delete m_activeList.back().first;
	m_activeList.pop_back();

	if (!m_activeList.empty())
	{
		updateExclusiveScene();

		if (!poppingExclusive)
		{
			m_activeList.back().first->notifyUncoveredObjects();
		}

		auto go = m_activeList.back().first->getAllGameObjects();
		for (size_t i = 0; i < go.size(); i++)
		{
			//go[i]->activate();
			if (poppingExclusive)
			{
				go[i]->setEnabled(go[i]->wasEnabled(), false);
			}
		}

		if (poppingExclusive)
		{
			go = m_activeList[m_exclusiveScene].first->getAllGameObjects();
			for (size_t i = 0; i < go.size(); i++)
			{
				AudioSource * audio = go[i]->getGameComponent<AudioSource>();
				if (audio != nullptr)
				{
					if (audio->getType() == AudioType::STREAM && audio->wasPlaying())
					{
						audio->play();
					}
				}
			}
		}
	}
}
void SceneManager::push(Scene* scene, Modality modality /*= Modality::Exclusive*/)
{
	if (!m_activeList.empty())
	{
		auto go = m_activeList.back().first->getAllGameObjects();
		for (size_t i = 0; i < go.size(); i++)
		{
			//go[i]->deactivate();
			if (modality == Modality::Exclusive)
			{
				go[i]->setWasEnabled(go[i]->isEnabled());
				go[i]->setEnabled(false, false);

				AudioSource * audio = go[i]->getGameComponent<AudioSource>();
				if (audio != nullptr)
				{
					if (audio->getType() == AudioType::STREAM)
					{
						audio->setWasPlaying(audio->isPlaying());
						// pause music
						audio->stop();
					}
				}
			}
		}
	}

	if (modality == Modality::Popup)
	{
		m_activeList.back().first->notifyCoveredObjects();
	}

	m_activeList.push_back(std::make_pair(scene, modality));

	scene->init(*m_viewport);
	scene->setEngine(m_coreEngine);
	auto attachedToRoot = scene->getRoot()->getAllAttached();
	for (size_t i = attachedToRoot.size() - 1; i > 0; i--)
	{
		auto gameComponents = attachedToRoot[i]->getAllGameComponents();
		for (size_t j = 0; j < gameComponents.size(); j++)
		{
			gameComponents[j]->onStart();
		}
	}

	updateExclusiveScene();	
}