/*
 * @file llfloatermediafilter.cpp
 * @brief Stupid floater for listing junk
 * @author Cinder Biscuits
 *
 * 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 "llfloatermediafilter.h"

#include "llnotificationsutil.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "lltrans.h"
#include "lluictrlfactory.h"

void on_add_to_list(bool white);
bool handle_add_callback(const LLSD& notification, const LLSD& response, const bool& white);
// TODO: Maybe add removal confirmation?
//bool handle_remove_callback(const LLSD& notification, const LLSD& response);

LLFloaterMediaFilter::LLFloaterMediaFilter(const LLSD& key)
:	LLFloater(key)
{
	mCommitCallbackRegistrar.add("MediaFilter.OnAdd", boost::bind(on_add_to_list, boost::bind(&LLSD::asBoolean, _2)));
	mCommitCallbackRegistrar.add("MediaFilter.OnRemove", boost::bind(&LLFloaterMediaFilter::onRemoveFromList, this, boost::bind(&LLSD::asBoolean, _2)));
	mMediaListConnection = LLMediaFilter::getInstance()->setMediaListUpdateCallback(boost::bind(&LLFloaterMediaFilter::updateLists, this, _1));
	LLUICtrlFactory::getInstance()->buildFloater(this, "floater_media_lists.xml", NULL, false);
}

LLFloaterMediaFilter::~LLFloaterMediaFilter()
{
	if (mMediaListConnection.connected())
		mMediaListConnection.disconnect();
}

BOOL LLFloaterMediaFilter::postBuild()
{
	mWhitelist = getChild<LLScrollListCtrl>("whitelist");
	mBlacklist = getChild<LLScrollListCtrl>("blacklist");
	updateLists(LLMediaFilter::WHITELIST);
	updateLists(LLMediaFilter::BLACKLIST);
	mWhitelist->setCommitOnSelectionChange(true);
	mBlacklist->setCommitOnSelectionChange(true);
	mWhitelist->setCommitCallback(boost::bind(&LLFloaterMediaFilter::enableButton, this, getChildView("remove_whitelist"), mWhitelist));
	mBlacklist->setCommitCallback(boost::bind(&LLFloaterMediaFilter::enableButton, this, getChildView("remove_blacklist"), mBlacklist));

	return TRUE;
}

void LLFloaterMediaFilter::updateLists(LLMediaFilter::EMediaList list_type)
{
	bool white(list_type == LLMediaFilter::WHITELIST);
	const LLMediaFilter& inst(LLMediaFilter::instance());
	const LLMediaFilter::string_list_t& list = white ? inst.getWhiteList() : inst.getBlackList();
	LLScrollListCtrl* scroll(white ? mWhitelist : mBlacklist);
	scroll->clearRows();
	for (const auto& value : list)
	{
		LLSD element;
		element["columns"][0]["column"] = "list";
		element["columns"][0]["value"] = value;
		scroll->addElement(element);
	}
	enableButton(getChildView(white ? "remove_whitelist" : "remove_blacklist"), scroll);
}

void LLFloaterMediaFilter::enableButton(LLView* btn, const LLScrollListCtrl* scroll)
{
	btn->setEnabled(!scroll->isEmpty() && scroll->getFirstSelected());
}

void on_add_to_list(bool white)
{
	LLSD args;
	args["LIST"] = LLTrans::getString(white ? "MediaFilterWhitelist" : "MediaFilterBlacklist");
	LLNotificationsUtil::add("AddToMediaList", args, LLSD(), boost::bind(handle_add_callback, _1, _2, white));
}

void LLFloaterMediaFilter::onRemoveFromList(bool white)
{
	std::vector<LLScrollListItem*> selected = (white ? mWhitelist : mBlacklist)->getAllSelected();
	LLMediaFilter::string_vec_t domains;
	for (auto* item : selected)
	{
		domains.push_back(item->getColumn(0)->getValue().asString());
	}
	LLMediaFilter::getInstance()->removeFromMediaList(domains, white ? LLMediaFilter::WHITELIST : LLMediaFilter::BLACKLIST);
}

bool handle_add_callback(const LLSD& notification, const LLSD& response, const bool& white)
{
	if (LLNotificationsUtil::getSelectedOption(notification, response) == 0)
	{
		LLMediaFilter::instance().addToMediaList(response["url"].asString(), white ? LLMediaFilter::WHITELIST : LLMediaFilter::BLACKLIST);
	}
	return false;
}
