/*
 * @file fsfloatercontactsetconfiguration.cpp
 * @brief Contact set configuration floater
 *
 * (C) 2013 Cinder Roxley @ Second Life <cinder.roxley@phoenixviewer.com>
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "llviewerprecompiledheaders.h"

#include "fsfloatercontactsetconfiguration.h"

#include "lggcontactsets.h"
#include "llbutton.h"
#include "llcheckboxctrl.h"
#include "llcolorswatch.h"
#include "lllineeditor.h"
#include "llnotificationsutil.h"
#include "llviewercontrol.h"

FSFloaterContactSetConfiguration::FSFloaterContactSetConfiguration(const LLSD& target_set)
:	LLFloater(target_set),
	mContextConeOpacity(0.f),
	mContextConeInAlpha(0.f),
	mContextConeOutAlpha(0.f),
	mContextConeFadeTime(0.f)
{
	mContactSet = target_set.asString();
	mContextConeInAlpha = gSavedSettings.getF32("ContextConeInAlpha");
	mContextConeOutAlpha = gSavedSettings.getF32("ContextConeOutAlpha");
	mContextConeFadeTime = gSavedSettings.getF32("ContextConeFadeTime");
}

BOOL FSFloaterContactSetConfiguration::postBuild()
{
	updateTitle();

	mSetName = getChild<LLLineEditor>("set_name_editor");
	mSetName->setText(mContactSet);

	mRenameButton = getChild<LLButton>("rename_btn");
	mRenameButton->setCommitCallback(boost::bind(&FSFloaterContactSetConfiguration::onRenameSet, this));

	mSetSwatch = getChild<LLColorSwatchCtrl>("set_swatch");
	mSetSwatch->setCommitCallback(boost::bind(&FSFloaterContactSetConfiguration::onCommitSetColor, this));

	mGlobalSwatch = getChild<LLColorSwatchCtrl>("global_swatch");
	mGlobalSwatch->setCommitCallback(boost::bind(&FSFloaterContactSetConfiguration::onCommitDefaultColor, this));

	mNotificationCheckBox = getChild<LLCheckBoxCtrl>("show_set_notifications");
	mNotificationCheckBox->setCommitCallback(boost::bind(&FSFloaterContactSetConfiguration::onCommitSetNotifications, this));
	return TRUE;
}

void FSFloaterContactSetConfiguration::draw()
{
	static LLCachedControl<F32> max_opacity(gSavedSettings, "PickerContextOpacity", 0.4f);
	drawConeToOwner(mContextConeOpacity, max_opacity, mFrustumOrigin.get(), mContextConeFadeTime, mContextConeInAlpha, mContextConeOutAlpha);
	LLFloater::draw();
}

void FSFloaterContactSetConfiguration::onOpen(const LLSD& target_set)
{
	mSetSwatch->set(LGGContactSets::getInstance()->getSetColor(mContactSet), TRUE);
	mGlobalSwatch->set(LGGContactSets::getInstance()->getDefaultColor(), TRUE);
	mNotificationCheckBox->set(LGGContactSets::getInstance()->getNotifyForSet(mContactSet));
}

void FSFloaterContactSetConfiguration::onCommitSetColor()
{
	LGGContactSets::getInstance()->setSetColor(mContactSet, mSetSwatch->get());
}

void FSFloaterContactSetConfiguration::onCommitSetNotifications()
{
	LGGContactSets::getInstance()->setNotifyForSet(mContactSet, mNotificationCheckBox->getValue().asBoolean());
}

void FSFloaterContactSetConfiguration::onCommitDefaultColor()
{
	LGGContactSets::getInstance()->setDefaultColor(mGlobalSwatch->get());
}

void FSFloaterContactSetConfiguration::onRenameSet()
{
	std::string new_name = mSetName->getText();

	if (!new_name.empty() && LGGContactSets::getInstance()->renameSet(mContactSet, new_name))
	{
		mKey = LLSD(new_name);
		mContactSet = new_name;
		updateTitle();
	}
	else
	{
		LLSD substitutions;
		substitutions["SET"] = mContactSet;
		substitutions["NEW_NAME"] = new_name;
		LLNotificationsUtil::add("RenameContactSetFailure", substitutions);
	}
}

void FSFloaterContactSetConfiguration::updateTitle()
{
	LLStringUtil::format_map_t map;
	map["NAME"] = mContactSet;
	setTitle(getString("title", map));
}

void FSFloaterContactSetConfiguration::setFrustumOrigin(LLView* frustumOrigin)
{
	if (frustumOrigin)
	{
		mFrustumOrigin = frustumOrigin->getHandle();
	}
}
