/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/nodes/composite/nodes/node_composite_outputFile.c
 *  \ingroup cmpnodes
 */


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

#include "node_composite_util.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "intern/openexr/openexr_multi.h"


/* **************** OUTPUT FILE ******************** */

/* find unique path */
static int unique_path_unique_check(void *arg, const char *name)
{
	struct {ListBase *lb; bNodeSocket *sock;} *data= arg;
	bNodeSocket *sock;
	for (sock=data->lb->first; sock; sock=sock->next) {
		if (sock != data->sock) {
			NodeImageMultiFileSocket *sockdata = sock->storage;
			if (strcmp(sockdata->path, name)==0)
				return 1;
		}
	}
	return 0;
}
void ntreeCompositOutputFileUniquePath(ListBase *list, bNodeSocket *sock, const char defname[], char delim)
{
	NodeImageMultiFileSocket *sockdata;
	struct {ListBase *lb; bNodeSocket *sock;} data;
	data.lb = list;
	data.sock = sock;

	/* See if we are given an empty string */
	if (ELEM(NULL, sock, defname))
		return;

	sockdata = sock->storage;
	BLI_uniquename_cb(unique_path_unique_check, &data, defname, delim, sockdata->path, sizeof(sockdata->path));
}

/* find unique EXR layer */
static int unique_layer_unique_check(void *arg, const char *name)
{
	struct {ListBase *lb; bNodeSocket *sock;} *data= arg;
	bNodeSocket *sock;
	for (sock=data->lb->first; sock; sock=sock->next) {
		if (sock != data->sock) {
			NodeImageMultiFileSocket *sockdata = sock->storage;
			if (strcmp(sockdata->layer, name)==0)
				return 1;
		}
	}
	return 0;
}
void ntreeCompositOutputFileUniqueLayer(ListBase *list, bNodeSocket *sock, const char defname[], char delim)
{
	NodeImageMultiFileSocket *sockdata;
	struct {ListBase *lb; bNodeSocket *sock;} data;
	data.lb = list;
	data.sock = sock;

	/* See if we are given an empty string */
	if (ELEM(NULL, sock, defname))
		return;

	sockdata = sock->storage;
	BLI_uniquename_cb(unique_layer_unique_check, &data, defname, delim, sockdata->layer, sizeof(sockdata->layer));
}

bNodeSocket *ntreeCompositOutputFileAddSocket(bNodeTree *ntree, bNode *node, const char *name, ImageFormatData *im_format)
{
	NodeImageMultiFile *nimf = node->storage;
	bNodeSocket *sock = nodeAddSocket(ntree, node, SOCK_IN, "", SOCK_RGBA);
	
	/* create format data for the input socket */
	NodeImageMultiFileSocket *sockdata = MEM_callocN(sizeof(NodeImageMultiFileSocket), "socket image format");
	sock->storage = sockdata;
	
	BLI_strncpy_utf8(sockdata->path, name, sizeof(sockdata->path));
	ntreeCompositOutputFileUniquePath(&node->inputs, sock, name, '_');
	BLI_strncpy_utf8(sockdata->layer, name, sizeof(sockdata->layer));
	ntreeCompositOutputFileUniqueLayer(&node->inputs, sock, name, '_');
	
	if (im_format) {
		sockdata->format= *im_format;
		if (BKE_imtype_is_movie(sockdata->format.imtype)) {
			sockdata->format.imtype= R_IMF_IMTYPE_OPENEXR;
		}
	}
	else
		BKE_imformat_defaults(&sockdata->format);
	/* use node data format by default */
	sockdata->use_node_format = TRUE;
	
	nimf->active_input = BLI_findindex(&node->inputs, sock);
	
	return sock;
}

int ntreeCompositOutputFileRemoveActiveSocket(bNodeTree *ntree, bNode *node)
{
	NodeImageMultiFile *nimf = node->storage;
	bNodeSocket *sock = BLI_findlink(&node->inputs, nimf->active_input);
	int totinputs = BLI_countlist(&node->inputs);
	
	if (!sock)
		return 0;
	
	if (nimf->active_input == totinputs-1)
		--nimf->active_input;
	
	/* free format data */
	MEM_freeN(sock->storage);
	
	nodeRemoveSocket(ntree, node, sock);
	return 1;
}

void ntreeCompositOutputFileSetPath(bNode *node, bNodeSocket *sock, const char *name)
{
	NodeImageMultiFileSocket *sockdata = sock->storage;
	BLI_strncpy_utf8(sockdata->path, name, sizeof(sockdata->path));
	ntreeCompositOutputFileUniquePath(&node->inputs, sock, name, '_');
}

void ntreeCompositOutputFileSetLayer(bNode *node, bNodeSocket *sock, const char *name)
{
	NodeImageMultiFileSocket *sockdata = sock->storage;
	BLI_strncpy_utf8(sockdata->layer, name, sizeof(sockdata->layer));
	ntreeCompositOutputFileUniqueLayer(&node->inputs, sock, name, '_');
}

static void init_output_file(bNodeTree *ntree, bNode *node, bNodeTemplate *ntemp)
{
	NodeImageMultiFile *nimf= MEM_callocN(sizeof(NodeImageMultiFile), "node image multi file");
	ImageFormatData *format = NULL;
	node->storage= nimf;

	if (ntemp->scene) {
		RenderData *rd = &ntemp->scene->r;
		BLI_strncpy(nimf->base_path, rd->pic, sizeof(nimf->base_path));
		nimf->format = rd->im_format;
		if (BKE_imtype_is_movie(nimf->format.imtype)) {
			nimf->format.imtype= R_IMF_IMTYPE_OPENEXR;
		}
		
		format = &nimf->format;
	}
	else
		BKE_imformat_defaults(&nimf->format);
	
	/* add one socket by default */
	ntreeCompositOutputFileAddSocket(ntree, node, "Image", format);
}

static void free_output_file(bNode *node)
{
	bNodeSocket *sock;
	
	/* free storage data in sockets */
	for (sock=node->inputs.first; sock; sock=sock->next) {
		MEM_freeN(sock->storage);
	}
	
	MEM_freeN(node->storage);
}

static void copy_output_file(struct bNode *node, struct bNode *target)
{
	bNodeSocket *sock, *newsock;
	
	target->storage = MEM_dupallocN(node->storage);
	
	/* duplicate storage data in sockets */
	for (sock=node->inputs.first, newsock=target->inputs.first; sock && newsock; sock=sock->next, newsock=newsock->next) {
		newsock->storage = MEM_dupallocN(sock->storage);
	}
}

static void update_output_file(bNodeTree *UNUSED(ntree), bNode *node)
{
	bNodeSocket *sock;
	
	/* automatically update the socket type based on linked input */
	for (sock=node->inputs.first; sock; sock=sock->next) {
		if (sock->link) {
			int linktype = sock->link->fromsock->type;
			if (linktype != sock->type)
				nodeSocketSetType(sock, linktype);
		}
	}
}

void register_node_type_cmp_output_file(bNodeTreeType *ttype)
{
	static bNodeType ntype;

	node_type_base(ttype, &ntype, CMP_NODE_OUTPUT_FILE, "File Output", NODE_CLASS_OUTPUT, NODE_OPTIONS|NODE_PREVIEW);
	node_type_socket_templates(&ntype, NULL, NULL);
	node_type_size(&ntype, 140, 80, 300);
	node_type_init(&ntype, init_output_file);
	node_type_storage(&ntype, "NodeImageMultiFile", free_output_file, copy_output_file);
	node_type_update(&ntype, update_output_file, NULL);

	nodeRegisterType(ttype, &ntype);
}
