/* Copyright 2015, Michele Santullo
 * This file is part of DoorKeeper.
 *
 * DoorKeeper 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.
 *
 * DoorKeeper 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 DoorKeeper.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef id873715F57B504CCF8227CE03EA28CAFA
#define id873715F57B504CCF8227CE03EA28CAFA

#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/implem/helpers.hpp"
#include "doorkeeper/components/tilecoords.hpp"
#include "doorkeeper/components/pixelconv.hpp"
#include "doorkeeper/components/tile.hpp"
#include <boost/iterator/iterator_facade.hpp>
#include <iterator>
#include <vector>
#include <type_traits>
#include <cstdint>
#include <utility>
#include <limits>

namespace dk {
	template <typename T, uint32_t D, typename T1>
	class TileIterator;

	namespace implem {
		template <typename I, typename O>
		struct TypeWithQualifiers {
			typedef typename std::conditional<std::is_volatile<I>::value && std::is_const<I>::value,
						typename std::add_cv<O>::type,
						typename std::conditional<std::is_volatile<I>::value,
							typename std::add_volatile<typename std::remove_cv<O>::type>::type,
							typename std::conditional<std::is_const<I>::value,
								typename std::add_const<typename std::remove_cv<O>::type>::type,
								typename std::remove_cv<O>::type
							>::type
						>::type
					>::type result;
		};
	} //namespace implem

	//TODO: replace with a custom iterator rewrite, not using iterator_facade.
	//The idea is to have operator* return a Tile<>, which is heavy to
	//construct but it's unlikely to be called very often, and then have
	//operator-> return a TileHandle<> object that only wraps a pointer to
	//the iterator's current state, so it's much more lightweight and it's not
	//expected to be kept around by client code.
	template <typename T, uint32_t D, typename T1=typename std::remove_cv<T>::type>
	class TileIterator : public boost::iterator_facade<TileIterator<T, D>, T, boost::random_access_traversal_tag, Tile<T, D>, CoordinateDistType> {
		friend class boost::iterator_core_access;
		typedef std::vector<T1> vector_type;
		typedef typename implem::TypeWithQualifiers<T, vector_type>::result qualif_vector_type;
		typedef boost::iterator_facade<TileIterator<T, D>, T, boost::random_access_traversal_tag, Tile<T, D>, CoordinateDistType> base_class;
	public:
		typedef TileCoords<D> TileCoordsType;
		typedef typename TileCoordsType::coords coords;
		typedef typename base_class::difference_type difference_type;
		typedef typename base_class::value_type value_type;
		typedef typename base_class::pointer pointer;
		typedef typename base_class::reference reference;
		typedef typename base_class::iterator_category iterator_category;

		TileIterator ( void ) = default;
		TileIterator ( const TileIterator& parOther ) = default;
		TileIterator ( TileIterator&& parOther ) = default;
		TileIterator ( qualif_vector_type* parData, const PixelConv<D>& parPixConv, const coords& parArea );
		TileIterator ( qualif_vector_type* parData, const PixelConv<D>& parPixConv, const coords& parStart, const coords& parArea );
		~TileIterator ( void ) = default;

	private:
		void increment ( void );
		void decrement ( void );
		void advance ( size_t parAdvance );
		difference_type distance_to ( const TileIterator& parOther );
		bool equal ( const TileIterator& parOther ) const;
		reference dereference ( void ) const;

		TileCoordsType m_tile_range;;
		qualif_vector_type* m_data;
		const PixelConv<D>& m_pixel_conv;
	};

	template <uint32_t D>
	Vector<D> make_past_end_coords ( const Vector<D>& parTo ) a_pure;
} //namespace dk

#include "doorkeeper/implem/tileiterator.inl"

#endif
