#pragma once
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstddef>

#include <SFML/Graphics.hpp>
#include <imgui-SFML.h>
#include <imgui.h>

#include "Utility.hpp"

class Light
{
public:
	Light() = default;

	Light(sf::Vector2f origin, bool visible = true)
		: m_origin(origin)
	{
		m_light.push_back(sf::Vertex{ m_origin, m_lightColor});
		visible ? setFlags(Flags::visibility) : unsetFlags(Flags::visibility);
	}

	Light(sf::Vector2f origin, sf::Angle center, sf::Angle offset, bool visible = true)
		: m_center(center)
		, m_offset(offset)
		, m_origin(origin)
	{
		m_light.push_back(sf::Vertex{ m_origin, m_lightColor });
		visible ? setFlags(Flags::visibility) : unsetFlags(Flags::visibility);
	}

public:
	enum class Flags
	{
		visibility = 1 << 0,
		infiniteLength = 1 << 1,
		drawRays = 1 << 2,
		drawEndPoints = 1 << 3
	};

	void addAngleBounds(sf::Angle center, sf::Angle offset)
	{
		m_center = center;
		m_offset = offset;
	}

	void removeAngleBounds()
	{
		m_center.reset();
		m_offset.reset();
	}

	void setFlags(Flags flags) { m_flags |= (int)flags; }
	void unsetFlags(Flags flags) { m_flags &= ~(int)flags; }
	void toggleFlags(Flags flags) { m_flags ^= (int)flags; }
	bool hasFlags(Flags flags) const { return (m_flags & (int)flags) == (int)flags; }

	void setCenterAngle(sf::Angle center) { m_center = center; }
	std::optional<sf::Angle> getCenterAngle() const { return m_center; }

	void setOffsetAngle(sf::Angle offset) { m_offset = offset; }
	std::optional<sf::Angle> getOffsetAngle() const { return m_offset; }

	void setOrigin(sf::Vector2f point) { m_origin = point; }
	sf::Vector2f getOrigin() const { return m_origin; }

	void setRayCount(int input) { m_rayCount = input; }
	int getRayCount() const { return m_rayCount; };

	float getRayLength() { return m_rayLength; }
	void setRayLength(float input) { m_rayLength = input; }

	void draw(sf::RenderWindow& window);

	void clearRays() { m_rays.clear(); m_light.clear(); m_light.push_back(sf::Vertex{ {0.f,0.f}, m_lightColor }); }

	void changeLightColor(sf::Color color) { m_lightColor = color; }
	void changeEndPointColor(sf::Color color) { m_endPointColor = color; }
	void changeRayColor(sf::Color color) { m_rayColor = color; }

	void processRays(const std::vector<sf::VertexArray>& polygons);

private:
	std::vector<sf::Vertex> m_light{};
	std::vector<sf::Vertex> m_rays{};
	std::optional<sf::Angle> m_center{};
	std::optional<sf::Angle> m_offset{};
	sf::Vector2f m_origin{};
	sf::Color m_lightColor{ sf::Color::Yellow };
	sf::Color m_endPointColor{ sf::Color::Magenta };
	sf::Color m_rayColor{ sf::Color::Red };

	float m_rayLength{ 500.f };
	int m_rayCount{ 500 };
	int m_flags{};

	enum
	{
		start,
		end
	};//used for sf::VertexArray index

private:
	void constructFullLight(const std::vector<sf::VertexArray>& polygons);
	void constructPartialLight(const std::vector<sf::VertexArray>& polygons);

	void createAndSortRays(const std::vector<sf::VertexArray>& polygons);
	void createAndSortRays(const std::vector<sf::VertexArray>& polygons, sf::Angle lowerAngleBounds, sf::Angle upperAngleBounds);// for bounded light

	void calculateIntersects(const std::vector<sf::VertexArray>& polygons);

	bool withinBounds(sf::Vector2f point, sf::Vector2f lower, sf::Vector2f upper);
};

