#include "ai.h"

AI_SHADER_NODE_EXPORT_METHODS(apClosure)

enum apClosureParams
{
	p_input,
	p_closure_type,
	p_weight
};

static const char* ClosureNames[] = {
	"Emission",
	"Transparency",
	"Matte",
	"Background",
	NULL
};

struct ShaderData
{
	int closure_type;
};

node_parameters
{
	AiParameterClosure("input");
	AiParameterEnum("closure_type", 0,  ClosureNames);
	AiParameterRGB("weight", 1, 1, 1);
}

node_initialize
{
	ShaderData *data = (ShaderData *)AiMalloc(sizeof(ShaderData));
	AiNodeSetLocalData(node, data);
}

node_update
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	data->closure_type = AiNodeGetInt(node, "closure_type");
}

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

shader_evaluate
{
	ShaderData *data = (ShaderData *)AiNodeGetLocalData(node);
	AtRGB weight = AiShaderEvalParamRGB(p_weight);
	
	AtClosureList closures;
	closures.add(AiShaderEvalParamClosure(p_input));
	
	switch (data->closure_type)
	{
	case 0:
		closures.add(AiClosureEmission(sg, weight));
		break;
	case 1:
		closures.add(AiClosureTransparent(sg, weight));
		break;
	case 2:
		closures.add(AiClosureMatte(sg, weight));
		break;
	case 3:
		closures.add(AiClosureBackground(sg, weight));
		break;
	}
	sg->out.CLOSURE() = closures;
	return;
}

node_loader
{
	if (i > 0)
	return false;

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