#include "CWindow.hpp"

CWindow::CWindow(std::string title, std::string iconPath, unsigned int width, unsigned int height, bool resizable, bool fullscreen, unsigned int sdlFlags) :
	m_title(title),
	m_iconPath(iconPath),
	m_size(width, height),
	m_visible(false),
	m_fullscreen(fullscreen),
	m_resizable(resizable),
	m_sdlFlags(sdlFlags) {
	m_pwindow = nullptr;
}

CWindow::CWindow(std::string title, SDL_Window* window, unsigned int sdlFlags, unsigned int imageFlags): m_sdlFlags(sdlFlags), m_size(600,400)
{
	m_title = title;
	m_pwindow = window;
}

CWindow::~CWindow(void) {
	if (m_pwindow != nullptr) {
		SDL_DestroyWindow(m_pwindow);
	}
}

SDL_Window* CWindow::getWindow(void) {
	return m_pwindow;
}

bool CWindow::isVisible(void) const {
	return m_visible;
}

void CWindow::setVisible(bool newVisible) {
	m_visible = newVisible;
	if (m_pwindow != nullptr) {
		if (m_visible) {
			SDL_ShowWindow(m_pwindow);
		}
		else {
			SDL_HideWindow(m_pwindow);
		}
	}
}

std::string CWindow::getTitle(void) const {
	return m_title;
}

void CWindow::setTitle(std::string newTitle) {
	m_title = newTitle;
	if (m_pwindow != nullptr) {
		SDL_SetWindowTitle(m_pwindow, m_title.c_str());
	}
}

std::string CWindow::getIconPath(void) const {
	return m_iconPath;
}

void CWindow::setIconPath(std::string newIconPath) {
	m_iconPath = newIconPath;
	if (m_pwindow != nullptr) {
		SDL_Surface* image;
		image = IMG_Load(m_iconPath.c_str());
		if (image != nullptr) {
			SDL_SetWindowIcon(m_pwindow, image);
			SDL_FreeSurface(image);
		}
	}
}

unsigned int CWindow::getWidth(void) const {
	return m_size.first;
}

unsigned int CWindow::getHeight(void) const {
	return m_size.second;
}

std::pair<unsigned int, unsigned int> CWindow::getSize(void) const {
	return m_size;
}

void CWindow::setSize(std::pair<unsigned int, unsigned int> newSize) {
	m_size = newSize;
	if (m_pwindow != nullptr) {
		SDL_SetWindowSize(m_pwindow, m_size.first, m_size.second);
	}
}

void CWindow::setSize(unsigned int newWidth, unsigned int newHeight) {
	m_size.first = newWidth;
	m_size.second = newHeight;
	if (m_pwindow != nullptr) {
		SDL_SetWindowSize(m_pwindow, m_size.first, m_size.second);
	}
}

bool CWindow::isResizable(void) const {
	return m_resizable;
}

void CWindow::setResizable(bool newResizable) {
	m_resizable = newResizable;
	if (m_pwindow != nullptr) {
		if (m_resizable) {
			SDL_SetWindowResizable(m_pwindow, SDL_TRUE);
		}
		else {
			SDL_SetWindowResizable(m_pwindow, SDL_FALSE);
		}
	}
}

bool CWindow::isFullscreen(void) const {
	return m_fullscreen;
}

void CWindow::setFullscreen(bool newFullscreen) {
	m_fullscreen = newFullscreen;
	if (m_pwindow != nullptr) {
		// Set the fullscreen property.
		if (m_fullscreen) {
			SDL_SetWindowFullscreen(m_pwindow, SDL_WINDOW_FULLSCREEN);
		}
		else {
			SDL_SetWindowFullscreen(m_pwindow, 0);
		}
	}
}

bool CWindow::isInitialized(void) const {
	return m_pwindow != nullptr;
}

void CWindow::initialization(void) {
	// Lancement de la preinitialisation.
	preinitialization();

	// Création de la fenetre.
	m_pwindow = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_size.first, m_size.second, SDL_WINDOW_HIDDEN | m_sdlFlags);

	// Handle error.
	if (m_pwindow == nullptr) {
		throw CLibException(std::string("Unable to create window: ") + SDL_GetError());
	}

	// Set the icon.
	SDL_Surface* image;
	image = IMG_Load(m_iconPath.c_str());
	if (image != nullptr) {
		SDL_SetWindowIcon(m_pwindow, image);
		SDL_FreeSurface(image);
	}

	// Set the rezisable property.
	if (m_resizable) {
		SDL_SetWindowResizable(m_pwindow, SDL_TRUE);
	}
	else {
		SDL_SetWindowResizable(m_pwindow, SDL_FALSE);
	}

	// Set the fullscreen property.
	if (m_fullscreen) {
		SDL_SetWindowFullscreen(m_pwindow, SDL_WINDOW_FULLSCREEN);
	}
	else {
		SDL_SetWindowFullscreen(m_pwindow, 0);
	}

	// Set the visible property.
	if (m_visible) {
		SDL_ShowWindow(m_pwindow);
	}
	else {
		SDL_HideWindow(m_pwindow);
	}

	// Lancement de la postinitialisation.
	postinitialization();
}