
#include "vector2.h"

#include "vector3.h"

#include "vector4.h"

#include "vector.h"

#include "char.h"

#include "classConfiguration.h"

#include <stdarg.h>

#include <stdbool.h>

#include <math.h>


class element{

	int index;

	vector3 position					= new vector3( 0, 0, 0 );

	vector2 size						= new vector2( 0, 0 );

	vector3 backgroundColor				= new vector3( 0, 0, 0 );



	vector3 originalPosition					= new vector3( 0, 0, 0 );

	vector2 originalSize						= new vector2( 0, 0 );

	vector3 originalBackgroundColor				= new vector3( 0, 0, 0 );



	float opacity						= 1;

	char * backgroundImagePath			= "";

	bool useBackgroundImage 			= false;

	int features						= 0;

	vector<char *> * featureNames		= new vector();


	constructor() {

		this->featureNames->add( "useBackgroundImage" );

		this->featureNames->add( "useBorder" );

	}


	vector4 colorConverter( char * hexValue )
	{

		int r;

		int g; 

		int b;

		int a;

		sscanf( hexValue, "%02x%02x%02x%02x", &r, &g, &b, &a );

		printf("opacity:		%i\n");

		struct vector4 rgbColor = new vector4( r, g, b, (float)a );

		return rgbColor; 
	}


	int textureIndex;

	void * background;

	setOriginal() {

		this->originalSize	= this->size;

		this->originalPosition = this->position;

		this->originalBackgroundColor = this->backgroundColor;

	}

	set size( vector2 a ) {

		this->size.x = a.x;

		this->size.y = a.y;

		printf("set size:		%f %f\n", this->size.x, this->size.y);

		

	}

	set position( vector3 a ) {

		this->position.x = a.x;

		this->position.y = a.y;

		this->position.z = a.z;

		printf("set position:		%f %f\n", this->position.x, this->position.y);

		

	}

	bool featureIsEnabled( int featureValue ) {
		
		return ( this->features & featureValue ) > 0;

	}



	int getFeatureValueByFeatureName( char * featureName ) {

		int count	= this->featureNames->length();

		for (int i = 0; i < count; ++i)
		{
			char * currentFeatureName = this->featureNames->get( i );

			if( featureName == currentFeatureName ) {

				return powf( 2, i );

			}

		}

		return 0;

	}

	int updateFeature() {

		int currentFeatureValue;

		currentFeatureValue 			= this->getFeatureValueByFeatureName( "useBackgroundImage" );

		if( this->featureIsEnabled( currentFeatureValue ) ) {

			if( !this->useBackgroundImage ) {

				this->features	-= currentFeatureValue;

			}

		} else {

			if( this->useBackgroundImage ) {

				this->features	+= currentFeatureValue;

			}

		}

		
		return this->features;

		//featureIsEnabled( int featureValue )


	}

	set background( ... ) {

		va_list args;

		va_start( args, count );

		int datatype 		= datatypes[0];


		printf("datatype:	%i\n\n\n", datatype);

		if( datatype == -2 ) {

			char * message = va_arg( args, char * );

			if( ( char ) message[0] == 35 ) {

				printf("Hex color\n\n\n");

				message++;

				vector4 rgba				= this->colorConverter( message );

				this->backgroundColor		= new vector3( rgba.x, rgba.y, rgba.z );

				this->opacity				= rgba.w / 256;

				this->useBackgroundImage	= false;

			} else {

				this->backgroundImagePath	= message;

				this->useBackgroundImage	= true;

				printf("path\n\n\n");

			}

			printf(" char *:			%s\n\n\n ", message);

		}

		if( datatype > 0 ) {

			char * className	= getClassName( datatype );

			if( className == "vector3" ) {

				vector3 message = va_arg( args, vector3 );

				this->backgroundColor		= message;

				this->useBackgroundImage	= false;

			}

			if( className == "vector4" ) {

				vector4 message = va_arg( args, vector4 );

				this->backgroundColor	= new vector3( message.x, message.y, message.z );

				this->opacity			= message.w;

				this->useBackgroundImage	= false;

			}

		}

		

		//printf(" 	%f %f %f  %f \n\n\n ", this->backgroundColor.x, this->backgroundColor.y, this->backgroundColor.z, this->opacity);




	}

	click() {


		this->backgroundColor 	= new vector3( 0, 256, 256 );

	}

	mousedown() {

		//printf("executed click event\n\n");

		this->backgroundColor 	= new vector3( 0, 256, 256 );

		this->position			= new vector3( 256, 256, 100 );

	}

	mouseup() {

		//printf("executed click event\n\n");

		this->backgroundColor 	= this->originalBackgroundColor;

		this->position			= this->originalPosition;

	}

	mouseover() {

		this->backgroundColor 	= new vector3( 256, 256, 256 );

	}

	mouseleave() {

		printf("mouse leave\n");

		this->backgroundColor 	= this->originalBackgroundColor;

	}
}