/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/engine/screen_manager.hpp"

#include <iostream>
#if defined(_MSC_VER)
// Visual Studio doesn't define ssize_t by default
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#endif

using namespace polygun::engine;

ScreenManager::ScreenManager(Engine& engine) :
	m_engine(engine),
	m_next_screen(),
	m_screen_stack(),
	m_should_pop_screen(false),
	m_prev_time(std::chrono::steady_clock::now())
{}

ScreenManager::~ScreenManager() {
	if(m_screen_stack.size()>0) {
		for(ssize_t i = m_screen_stack.size()-1; i>=0; i--) {
			m_screen_stack[i]->finish();
		}
	}
}

void ScreenManager::push_screen(std::unique_ptr<Screen> screen) {
	screen->m_engine = &m_engine;
	m_next_screen = std::move(screen);
}

void ScreenManager::pop_screen() {
	Screen* current_screen = get_current_screen();
	if(!current_screen)
		return;

	m_should_pop_screen = true;
}

void ScreenManager::clear() {
	Screen* current_screen = get_current_screen();
	if(current_screen) {
		current_screen->finish();
		m_screen_stack.clear();
	}
}

void ScreenManager::render() {
	Screen* current_screen = get_current_screen();
	if(m_next_screen) {
		if(current_screen)
			current_screen->finish();
		m_screen_stack.push_back(std::move(m_next_screen));
		current_screen = get_current_screen();
		current_screen->begin();
	}
	if(current_screen)
		current_screen->render();
}

void ScreenManager::update() {
	Screen* current_screen = get_current_screen();
	std::chrono::time_point<std::chrono::steady_clock> tm = std::chrono::steady_clock::now();
	if(current_screen) {
		std::chrono::duration<double> interval = tm-m_prev_time;
		current_screen->update(interval.count());
	}
	if(m_should_pop_screen) {
		current_screen->finish();
		m_screen_stack.pop_back();
		m_should_pop_screen = false;
	}
	m_prev_time = tm;
}

void ScreenManager::event(const window::Event& ev) {
	Screen* current_screen = get_current_screen();
	if(current_screen)
		current_screen->event(ev);
}

Screen* ScreenManager::get_current_screen() {
	if(m_screen_stack.size()==0)
		return nullptr;
	return m_screen_stack.back().get();
}

bool ScreenManager::has_screen() {
	return get_current_screen() || m_next_screen;
}
