
/*
 * CINELERRA
 * Copyright (C) 2007 Andraz Tori
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "clip.h"
#include "language.h"
#include "renderprofiles.h"
#include "mwindow.h"
#include "theme.h"
#include "bchash.h"
#include "string.h"
#include "render.h"
#include "asset.h"
#include "errorbox.h"
#include "mwindowgui.h"

#define LISTWIDTH xS(200)

RenderProfileItem::RenderProfileItem(const char *text, int value)
 : BC_ListBoxItem(text)
{
	this->value = value;
}


RenderProfile::RenderProfile(MWindow *mwindow,
	RenderWindow *rwindow,
	int x,
	int y,
	int use_nothing)
{
	this->mwindow = mwindow;
	this->rwindow = rwindow;
	this->x = x;
	this->y = y;
	this->use_nothing = use_nothing;
	for (int i = 1; i < MAX_PROFILES; i++)
	{
		char string_name[100];
		char name[100] = "";
		sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
		mwindow->defaults->get(string_name, name);
		if (strlen(name) != 0)
			profiles.append(new RenderProfileItem(name, i));

	}
}

RenderProfile::~RenderProfile()
{
//	delete title;
//	delete textbox;
//	delete listbox;
	for(int i = 0; i < profiles.total; i++)
		delete profiles.values[i];
}



int RenderProfile::calculate_h(BC_WindowBase *gui)
{
	return BC_TextBox::calculate_h(gui, MEDIUMFONT, 1, 1);
}

int RenderProfile::create_objects()
{
	int x = this->x, y = this->y;
	const char *default_text = "";
	rwindow->add_subwindow(new BC_Title(x, y, _("RenderProfile:")));

	int old_y = y;
	rwindow->add_subwindow(title = new BC_Title(x, y, _("Render profile:")));
	y += yS(25);
	rwindow->add_subwindow(textbox = new BC_TextBox(x, y, LISTWIDTH, 1, default_text));
	x += textbox->get_w();
	rwindow->add_subwindow(listbox = new RenderProfileListBox(rwindow, this, x, y));

	y = old_y;
	x += listbox->get_w() + xS(10);
	rwindow->add_subwindow(saveprofile = new SaveRenderProfileButton(this, x, y));
	y += yS(25);
	rwindow->add_subwindow(deleteprofile = new DeleteRenderProfileButton(this, x, y));

	return 0;
}

int RenderProfile::get_h()
{
	int result = 0;
	result = MAX(result, title->get_h());
	result = MAX(result, textbox->get_h());
	return result;
}

int RenderProfile::get_x()
{
	return x;
}

int RenderProfile::get_y()
{
	return y;
}

int RenderProfile::reposition_window(int x, int y)
{
	this->x = x;
	this->y = y;
	title->reposition_window(x, y);
	y += yS(20);
	textbox->reposition_window(x, y);
	x += textbox->get_w();
	listbox->reposition_window(x,
		y,
		LISTWIDTH);
	return 0;
}


RenderProfileListBox::RenderProfileListBox(BC_WindowBase *window,
	RenderProfile *renderprofile, int x, int y)
 : BC_ListBox(x, y, LISTWIDTH, yS(150), LISTBOX_TEXT,
	(ArrayList<BC_ListBoxItem *>*)&renderprofile->profiles,
	0, 0, 1, 0, 1)
{
	this->window = window;
	this->renderprofile = renderprofile;
}

RenderProfileListBox::~RenderProfileListBox()
{
}

int RenderProfileListBox::handle_event()
{
	RenderProfileItem *item = (RenderProfileItem *)get_selection(0, 0);
	if( item ) {
		renderprofile->textbox->update(item->get_text());
		renderprofile->rwindow->load_profile(item->value);
	}
	return 1;
}

int RenderProfile::get_profile_slot_by_name(const char *profile_name)
{
	for (int i = 1; i < MAX_PROFILES; i++)
	{
		char string_name[100];
		char name[100] = "";
		sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);

		mwindow->defaults->get(string_name, name);
		if (strcmp(name, profile_name) == 0)
			return i;
	}
// No free profile slots!
	return -1;
}

int RenderProfile::get_new_profile_slot()
{
	for (int i = 1; i < MAX_PROFILES; i++)
	{
		char string_name[100];
		char name[100] = "";
		sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
		mwindow->defaults->get(string_name, name);
		if (strlen(name) == 0)
			return i;
	}
	return -1;
}


int RenderProfile::save_to_slot(int profile_slot, const char *profile_name)
{
	char string_name[100];
	sprintf(string_name, "RENDER_%i_PROFILE_NAME", profile_slot);
	mwindow->defaults->update(string_name, profile_name);

	sprintf(string_name, "RENDER_%i_FILE_PER_LABEL", profile_slot);
	mwindow->defaults->update(string_name,
		rwindow->render->use_labels ? FILE_PER_LABEL : SINGLE_PASS);
	sprintf(string_name, "RENDER_%i_LOADMODE", profile_slot);
	mwindow->defaults->update(string_name, rwindow->render->load_mode);
	sprintf(string_name, "RENDER_%i_RANGE_TYPE", profile_slot);
	mwindow->defaults->update(string_name, rwindow->render->range_type);

	sprintf(string_name, "RENDER_%i_", profile_slot);
	rwindow->asset->save_defaults(mwindow->defaults,
		string_name, 1, 1, 1, 1, 1);

	mwindow->save_defaults();
	return 0;
}



SaveRenderProfileButton::SaveRenderProfileButton(RenderProfile *profile, int x, int y)
 : BC_GenericButton(x, y, _("Save profile"))
{
	this->profile = profile;
}
int SaveRenderProfileButton::handle_event()
{

	const char *profile_name = profile->textbox->get_text();
	if (strlen(profile_name) == 0)     // Don't save when name not defined
		return 1;
	int slot = profile->get_profile_slot_by_name(profile_name);
	if (slot < 0)
	{
		slot = profile->get_new_profile_slot();
		if (slot < 0)
		{
			ErrorBox error_box(_(PROGRAM_NAME ": Error"),
					   profile->mwindow->gui->get_abs_cursor_x(1),
					   profile->mwindow->gui->get_abs_cursor_y(1));
			error_box.create_objects(_("Maximum number of render profiles reached"));
			error_box.raise_window();
			error_box.run_window();
			return 1;
		}

		profile->profiles.append(new RenderProfileItem(profile_name, slot));
		profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);

	}

	if (slot >= 0)
	{
		profile->save_to_slot(slot, profile_name);
	}
	return 1;
}


DeleteRenderProfileButton::DeleteRenderProfileButton(RenderProfile *profile, int x, int y)
 : BC_GenericButton(x, y, _("Delete profile"))
{
	this->profile = profile;
}
int DeleteRenderProfileButton::handle_event()
{
	const char *profile_name = profile->textbox->get_text();
	int slot = profile->get_profile_slot_by_name(profile_name);
	if (slot >= 0)
	{
		for(int i = 0; i < profile->profiles.total; i++)
		{
			if(profile->profiles.values[i]->value == slot)
			{
				profile->profiles.remove_object_number(i);
				profile->save_to_slot(slot, "");

				break;
			}
		}
		profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
		profile->textbox->update("");

	}
	return 1;
}

