/*
	Copyright 2016, 2017 Michele "King_DuckZ" Santullo

	This file is part of MyCurry.

	MyCurry is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	MyCurry is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with MyCurry.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "vector.hpp"
#include "tileindextype.hpp"
#include "observersmanager.hpp"
#include "tileproperty.hpp"
#include <vector>
#include <cstdint>

namespace curry {
	class WorldSizeNotifiable;

	class WorldGrid {
		using WorldSizeWatcherTicket = cloonel::ObserversManager<WorldSizeNotifiable*>::TicketType;

	public:
		explicit WorldGrid (vec2us parTileSize);

		const vec2us& tile_size() const;
		const vec2us& world_size() const;
		uint16_t layer_count() const;
		const TileIndex* tile (const vec2us& parIndex) const;
		void add_layer (vec2us parWorldSize, const std::vector<TileIndex>& parLayer);
		void set_layers (vec2us parWorldSize, const std::vector<std::vector<TileIndex>>& parLayers);
		void set_tile_properties (const std::vector<TileProperty>& parProperties);
		const TileProperty& tile_property (const TileIndex* parIndex);
		void unload_layers();
		WorldSizeWatcherTicket register_observer (WorldSizeNotifiable* parWatcher);
		void unregister_observer (WorldSizeWatcherTicket parTicket);

	private:
		void set_world_size (const vec2us& parSize);

		std::size_t tile_count() const;

		cloonel::ObserversManager<WorldSizeNotifiable*> m_world_size_watchers;
		std::vector<TileIndex> m_layers;
		std::vector<TileProperty> m_tile_properties;
		vec2us m_tile_size;
		vec2us m_world_size;
		uint16_t m_layer_count;
	};

	vec2i world_size_pixel (const WorldGrid& parWorld);
	vec2us pixel_to_world_tile (const WorldGrid& parWorld, const vec2f& parPos);
	bool position_is_on_map (const WorldGrid& parWorld, const vec2i& parPos);
} //namespace curry
