

#pragma once
#include "Component.h"
#include "../Entitys/Object.h"

#include "../core/types/vec2.h"
#include "../core/types/vec4.h"
#include "../core/types/color.h"

#include <glm/glm.hpp>
#include <vector>

#include <string>
#include <yaml-cpp/yaml.h>

enum class ParticleShape
{
    Circle,
    Square,
    // Triangle,
    // Line,
    // Star,
    // Hexagon,
    // Diamond,
    // Cross,

    // Outline
    MAX_SHAPE
};

struct Particle
{
    core::types::Vec2 position;
    core::types::Vec2 velocity;
    float life;
    float size;
    float rotation;
    core::types::Color color;
};

struct ParticleEmitterSettings
{
    int maxParticles = 1000;
    float emissionRate = 100.0f; // particle/s
    float lifeMin = 0.5f;
    float lifeMax = 1.5f;
    float sizeMin = 5.0f;
    float sizeMax = 10.0f;
    float speedMin = 100.0f;
    float speedMax = 300.0f;
    core::types::Vec2 direction = {1.0f, 0.0f};
    float spread = 1.57f; // radians
    core::types::Color startColor = {1, 1, 1, 1};
    core::types::Color endColor = {1, 1, 1, 0};
    bool loop = true;
    bool burst = false;

    float roundness = 1.0f; // 0 = square, 1 = circle
};


class ParticleComponent : public Component
{
public:
    ParticleComponent(Object *owner) : Component(owner) {}

    std::string GetName() const override { return "ParticleComponent"; }

    void Save(YAML::Emitter &out) const override;
    void Load(const YAML::Node &node) override;

    void SetEmitterSettings(const ParticleEmitterSettings &settings);
    const ParticleEmitterSettings &GetEmitterSettings() const { return settings; }
    const std::vector<Particle> &GetParticles() const { return particles; }
    const ParticleEmitterSettings &GetSettings() const { return settings; }

    void Emit();
    void Update(float dt);

private:
    std::vector<Particle> particles;
    ParticleEmitterSettings settings;
    float emitAccumulator = 0.0f;

    void SpawnParticle();
};
