#pragma once

#include "client/entities/ClientEntity.h"

namespace caveexpress {

class ClientParticle: public ClientEntity {
private:
	using Super = ClientEntity;
	struct ParticleData {
		ParticleData() :
				angle(0), lifetime(0) {
		}

		EntityAngle angle;
		uint32_t lifetime;
		vec2 pos;
		vec2 prevPos;
		vec2 nextPos;
	};

	ParticleData *_particles = nullptr;
	int _maxParticles = 0;
	int _lifetime = 0;
	int _particleType = 0;

	ClientParticle (uint16_t id, const std::string& sprite, float x, float y);
public:
	class Factory: public IClientEntityFactory {
		ClientEntityPtr create (const ClientEntityFactoryContext *ctx) const override;
	};
	static Factory FACTORY;

	virtual ~ClientParticle ();

	void resetParticles (uint8_t maxParticles, uint32_t lifetime);
	void updateParticle (int index, float x, float y, uint32_t lifetime, EntityAngle angle);
	void setParticleType (int type);
	int getParticleType () const;

	// ClientEntity
	std::string getSpriteName() const override;
	bool update (uint32_t deltaTime, bool lerpPos) override;
	void render (IFrontend *frontend, Layer layer, int scale, float zoom, int offsetX, int offsetY, int mapPixelWidth, int mapPixelHeight) const override;
};

inline int ClientParticle::getParticleType () const
{
	return _particleType;
}

inline void ClientParticle::setParticleType (int type)
{
	_particleType = type;
}

}
