#ifndef		__VECTOR_IMAGE_H__
#define		__VECTOR_IMAGE_H__


#include <math.h>
#include <vector>
#include "aadraw.h"
#include "vector2d.h"
#include "defines.h"


class VectorImage {
	public:
		class Stroke {
			protected:
				std::vector<Vector2D> points;
				int colour;
				int flags;
				float thickness;
				float curviness;

			public:
				void Add(const Vector2D &p);
				void SetPoint(int i, const Vector2D &p);
				void Remove(const Vector2D &p, float bias=0.01);
				int Find(const Vector2D &p, float bias=0.01);
				int GetLength();
				const Vector2D& GetPoint(int i);
				void SetColour(int c);
				int GetColour();
				void Move(const Vector2D &d);
				void SetFlag(int f, bool on=true);
				bool TestFlag(int f);
				void SetThickness(float t);
				float GetThickness();
				void SetCurviness(float c);
				float GetCurviness();
				void Render(BITMAP *canvas, float zoom=1.0f, int offset_x=0, int offset_y=0, int mode=0xff, bool debug=false);

			public:
				Stroke();
				~Stroke();

				friend class VectorImage;
		};


		class Gradient {
			protected:
				// start and end colour
				int colour[2];

				// anchor point and two reference points
				Vector2D anchor[2];

				// seed points
				std::vector<Vector2D> points;

				// 0 (solid), 1 (linear) or 2 (ellipse)
				int type;

				// variables for floodfill
				int p0x, p0y, vx, vy;
				float d, d_2;
				float r1, g1, b1, r2, g2, b2;
				int minX, maxX, minY, maxY;

				int GetFillColour(int x, int y);

				// taken off some website
				void Fill(BITMAP *canvas, int x, int y);

			public:
				void SetColour(int which, int col);
				int GetColour(int which);
				void SetAnchor(int which, const Vector2D &p);
				void SetPoint(int which, const Vector2D &p);
				const Vector2D &GetAnchor(int which);
				const Vector2D &GetPoint(int which);
				void AddPoint(const Vector2D &p);
				void RemovePoint(const Vector2D &p);
				int GetPointCount();
				void SetType(int t);
				int GetType();

				// returns index of the point that is close enough to the passe point,
				// or -2 or -3 if an anchor is close or -1 if no points are close
				int Find(const Vector2D &p, float bias=0.01);
				void Render(BITMAP *canvas, float zoom=1.0f, int offset_x=0, int offset_y=0, int mode=0xff, bool debug=false);

			public:
				Gradient();

				friend class VectorImage;
		};


	protected:
		std::vector<Stroke> strokes;
		std::vector<Gradient> gradients;
		int background[4];

		void RenderBackground(BITMAP *canvas, float zoom=1.0f, int offset_x=0, int offset_y=0);

	public:
		void AddStroke();
		void RemoveStroke(int which);
		Stroke &GetStroke(int which);
		int GetStrokeCount();
		void AddGradient();
		void RemoveGradient(int which);
		Gradient &GetGradient(int which);
		int GetGradientCount();
		void SetBackground(int which, int col);
		int GetBackground(int which);

		void Render(BITMAP *canvas, float zoom=1.0f, int offset_x=0, int offset_y=0, int mode=0xff, int selectedStroke=-1, int selectedGradient=-1);
		int Load(const char *filename);
		int Save(const char *filename);
		void Clear();

	public:
		VectorImage();
		~VectorImage();
};


#endif		//__VECTOR_IMAGE_H__
