/*
 *  This file is part of RawTherapee.
 *
 *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
 *
 *  RawTherapee 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  RawTherapee 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 RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "preprocess.h"
#include "guiutils.h"
#include "../rtengine/safegtk.h"
#include <sstream>

using namespace rtengine;
using namespace rtengine::procparams;

PreProcess::PreProcess () : FoldableToolPanel(this)
{
	
	Gtk::HBox* hotdeadPixel = Gtk::manage( new Gtk::HBox () );
	hotdeadPixel->set_spacing(4);
	hotPixel = Gtk::manage(new Gtk::CheckButton((M("TP_PREPROCESS_HOTPIXFILT"))));
	deadPixel = Gtk::manage(new Gtk::CheckButton((M("TP_PREPROCESS_DEADPIXFILT"))));
	
	hotPixel->set_tooltip_markup (M("TP_PREPROCESS_HOTPIXFILT_TOOLTIP"));
	deadPixel->set_tooltip_markup (M("TP_PREPROCESS_DEADPIXFILT_TOOLTIP"));
	
	hotdeadPixel->pack_start( *hotPixel, Gtk::PACK_SHRINK);
	hotdeadPixel->pack_start( *deadPixel, Gtk::PACK_SHRINK, 0);
	pack_start(*hotdeadPixel, Gtk::PACK_SHRINK, 0);
//	hotdeadPixel->show();
	hpixelconn = hotPixel->signal_toggled().connect ( sigc::mem_fun(*this, &PreProcess::hotPixelChanged), true);
	dpixelconn = deadPixel->signal_toggled().connect ( sigc::mem_fun(*this, &PreProcess::deadPixelChanged), true);
}

void PreProcess::read(const rtengine::procparams::ProcParams* pp, const ParamsEdited* pedited)
{
	disableListener ();
	hpixelconn.block (true);
	dpixelconn.block (true);
	if(pedited ){
		hotPixel->set_inconsistent (!pedited->raw.hotPixelFilter);
		deadPixel->set_inconsistent (!pedited->raw.deadPixelFilter);
	}

	lastHot = pp->raw.hotPixelFilter;
	lastDead = pp->raw.deadPixelFilter;
	hotPixel->set_active (pp->raw.hotPixelFilter);
	deadPixel->set_active (pp->raw.deadPixelFilter);

	hpixelconn.block (false);
	dpixelconn.block (false);
	enableListener ();
}

void PreProcess::write( rtengine::procparams::ProcParams* pp, ParamsEdited* pedited)
{
	pp->raw.hotPixelFilter = hotPixel->get_active();
	pp->raw.deadPixelFilter = deadPixel->get_active();

	if (pedited) {
		pedited->raw.hotPixelFilter = !hotPixel->get_inconsistent();
		pedited->raw.deadPixelFilter = !deadPixel->get_inconsistent();
	}
}

void PreProcess::hotPixelChanged ()
{
	if (batchMode) {
		if (hotPixel->get_inconsistent()) {
			hotPixel->set_inconsistent (false);
			hpixelconn.block (true);
			hotPixel->set_active (false);
			hpixelconn.block (false);
		}
		else if (lastHot)
			hotPixel->set_inconsistent (true);

		lastHot = hotPixel->get_active ();
	}
	if (listener)
		listener->panelChanged (EvPreProcessHotPixel, hotPixel->get_active()?M("GENERAL_ENABLED"):M("GENERAL_DISABLED"));
}

void PreProcess::deadPixelChanged ()
{
	if (batchMode) {
		if (deadPixel->get_inconsistent()) {
			deadPixel->set_inconsistent (false);
			dpixelconn.block (true);
			deadPixel->set_active (false);
			dpixelconn.block (false);
		}
		else if (lastDead)
			deadPixel->set_inconsistent (true);

		lastDead = deadPixel->get_active ();
	}
	if (listener)
		listener->panelChanged (EvPreProcessDeadPixel, deadPixel->get_active()?M("GENERAL_ENABLED"):M("GENERAL_DISABLED"));
}
