#include "ai.h"
#include <string>

AI_SHADER_NODE_EXPORT_METHODS(apEntrySwitch)

	enum apEntrySwitchParams {
	p_entry_polymesh,
	p_entry_curves,
	p_entry_nurbs,
	p_entry_point,
	p_entry_volume,
	p_default_color
};

struct ShaderData
{
	AtRGB entry_ploymesh;
	AtRGB entry_curves;
	AtRGB entry_nurbs;
	AtRGB entry_point;
	AtRGB entry_volume;
	AtRGB default_color;
};

node_parameters
{	
	AiParameterRGB("polymesh", 1, 0, 0);
	AiParameterRGB("curves", 0, 1, 0);
	AiParameterRGB("nurbs", 0, 0, 1);
	AiParameterRGB("point", 0, 1, 1);
	AiParameterRGB("volume", 1, 1, 0);
	AiParameterRGB("default_color", 1, 0, 1);
}

node_initialize
{
	ShaderData *data = (ShaderData*) AiMalloc(sizeof(ShaderData));
	data->entry_ploymesh = AtRGB(1.0, 0, 0);
	data->entry_curves = AtRGB(0, 1.0, 0);
	data->entry_nurbs = AtRGB(0, 0, 1.0);
	data->entry_point = AtRGB(0, 1.0, 1.0);
	data->entry_volume = AtRGB(1.0, 1.0, 0);
	data->default_color = AtRGB(1.0, 0, 1.0);
	AiNodeSetLocalData(node, data);
}
node_update
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	data->entry_ploymesh = AiNodeGetRGB(node, "polymesh");
	data->entry_curves = AiNodeGetRGB(node, "curves");
	data->entry_nurbs = AiNodeGetRGB(node, "nurbs");
	data->entry_point = AiNodeGetRGB(node, "point");
	data->entry_volume = AiNodeGetRGB(node, "volume");
	data->default_color = AiNodeGetRGB(node, "default_color");
}

node_finish
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	AiFree(data);
}

shader_evaluate
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	AtString entry_name(AiNodeEntryGetName(AiNodeGetNodeEntry(sg->Op)));

	if (entry_name == AtString("polymesh")) {
		sg->out.RGB() = data->entry_ploymesh;
		return;
	}
	if (entry_name == AtString("curves")) {
		sg->out.RGB() = data->entry_curves;
		return;
	}
	if (entry_name == AtString("nurbs")) {
		sg->out.RGB() = data->entry_nurbs;
		return;
	}
	if (entry_name == AtString("point")) {
		sg->out.RGB() = data->entry_point;
		return;
	}
	if (entry_name == AtString("volume")) {
		sg->out.RGB() = data->entry_volume;
		return;
	}
	sg->out.RGB() = data->default_color;
	return;
}
node_loader
{
	if (i > 0)
		return false;

	node->methods = apEntrySwitch;
	node->output_type = AI_TYPE_RGBA;
	node->name = "apEntrySwitch";
	node->node_type = AI_NODE_SHADER;
	strcpy(node->version, AI_VERSION);
	return true;
}