#pragma once
#include <array>
#include <cmath>
#include <map>

#include "structs.h"
#include "utils.h"

class Player;
class WorldTile;

namespace Collision
{

	enum CollisionDirection {
		Top,
		Bottom,
		Left,
		Right
	};

	struct CollisionRect
	{
		CollisionRect() = default;
		CollisionRect(const Vector2f& pos, const Vector2f& size) : pos(pos), size(size) {}
		CollisionRect(const Vector2f& pos, const Vector2f& size, const Vector2f& vel) : pos(pos), size(size), vel(vel) {}
		Vector2f pos;
		Vector2f size;
		Vector2f vel;

		std::map<CollisionDirection, CollisionRect*> ContactMap{};
	};

	struct TileCollisionRect 
	{
		Vector2f pos;
		Vector2f size;
		Vector2f vel;
		
		TileCollisionRect(const Vector2f& pos, const Vector2f& size, WorldTile* tile);
		WorldTile* tile;

		bool Contains(Vector2f point2_f) {
			return utils::IsPointInRect(point2_f, Rectf{ pos.x, pos.y, size.x, size.y });
		}

		Collision::CollisionRect getCollisionRect() {
			return Collision::CollisionRect{ pos, size, vel };
		}
	};

	bool PointVsRect(const Vector2f p, const CollisionRect& r);

	bool RectVsRect(const CollisionRect& r1, const CollisionRect r2);

	bool RayVsRect(const Vector2f& rayOrigin, const Vector2f& rayDirection, const Collision::CollisionRect target, Vector2f& contactPoint, Vector2f& contactNormal, float& t_HitNear);

	bool DynamicRectVsRect(const CollisionRect& dynamicRectangle, float ElapsedTime, const CollisionRect& staticRectangle, Vector2f& contactPoint, Vector2f& contactNormal, float& contactTime);

	bool ResolveDynamicRectVsRect(CollisionRect& dynamicRectangle, float ElapsedTime, CollisionRect* staticRectangle);

	bool ResolvePlayerVsRect(Player& player, float ElapsedTime, Collision::CollisionRect* staticRectangle);
}
