/*
 * @file llpanelgroupbanlist.cpp
 * @brief Group banlist panel
 *
 * Copyright (c) 2014, Cinder Roxley <cinder@sdf.org>
 *
 * 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 "llpanelgroupbanlist.h"

#include "llnamelistctrl.h"
#include "llagent.h"
#include "llfloatergroupbulkban.h"
#include "roles_constants.h"

static LLPanelInjector<LLPanelGroupBanlist> t_panel_group_banlist("panel_group_banlist");

LLPanelGroupBanlist::LLPanelGroupBanlist()
:	LLPanelGroupTab()
,	mBanListCtrl(nullptr)
,	mAddBtn(nullptr)
,	mRemoveBtn(nullptr)
,	mRefreshBtn(nullptr)
{
	mCommitCallbackRegistrar.add("Banlist.add", boost::bind(&LLPanelGroupBanlist::onCommitBan, this));
	mCommitCallbackRegistrar.add("Banlist.remove", boost::bind(&LLPanelGroupBanlist::onCommitRemoveBan, this));
	mCommitCallbackRegistrar.add("Banlist.refresh", boost::bind(&LLPanelGroupBanlist::requestAndRefreshList, this));
}

LLPanelGroupBanlist::~LLPanelGroupBanlist()
{
}

BOOL LLPanelGroupBanlist::postBuild()
{
	mBanListCtrl = getChild<LLNameListCtrl>("ban_list");
	mBanListCtrl->setCommitCallback(boost::bind(&LLPanelGroupBanlist::onCommitSelection, this));
	mAddBtn = getChild<LLButton>("ban_create");
	mRemoveBtn = getChild<LLButton>("ban_delete");
	mRefreshBtn = getChild<LLButton>("ban_refresh");
	
	return TRUE;
}

void LLPanelGroupBanlist::setGroupID(const LLUUID& id)
{
	LLPanelGroupTab::setGroupID(id);
	mRemoveBtn->setEnabled(gAgent.hasPowerInGroup(mGroupID, GP_GROUP_BAN_ACCESS)
						   && !mBanListCtrl->getAllSelected().empty());
	requestAndRefreshList();
}

void LLPanelGroupBanlist::update(LLGroupChange gc)
{
	mAddBtn->setEnabled(gAgent.hasPowerInGroup(mGroupID, GP_GROUP_BAN_ACCESS));
	refreshList();
}

void LLPanelGroupBanlist::onCommitSelection()
{
	mRemoveBtn->setEnabled(gAgent.hasPowerInGroup(mGroupID, GP_GROUP_BAN_ACCESS)
						   && !mBanListCtrl->getAllSelected().empty());
}

void LLPanelGroupBanlist::onCommitBan()
{
	LLFloaterGroupBulkBan::showForGroup(mGroupID);
}

void LLPanelGroupBanlist::onCommitRemoveBan()
{
	std::vector<LLScrollListItem*> selection = mBanListCtrl->getAllSelected();
	if(selection.empty())
	{
		return;
	}
	
	LLGroupMgrGroupData* gdatap = LLGroupMgr::getInstance()->getGroupData(mGroupID);
	if(!gdatap)
	{
		LL_WARNS("Groups") << "Unable to get group data for group " << mGroupID << LL_ENDL;
		return;
	}
	
	if (!(gAgent.isGodlike() || gAgent.hasPowerInGroup(mGroupID, GP_GROUP_BAN_ACCESS)))
		return;
	
	std::vector<LLUUID> ban_ids;
	std::vector<LLScrollListItem*>::iterator itor;
	for(itor = selection.begin(); itor != selection.end(); ++itor)
	{
		const LLUUID ban_id = (*itor)->getUUID();
		ban_ids.push_back(ban_id);
		
		gdatap->removeBanEntry(ban_id);
		mBanListCtrl->removeNameItem(ban_id);
	}
	
	LLGroupMgr::getInstance()->sendGroupBanRequest(LLGroupMgr::REQUEST_POST, mGroupID, LLGroupMgr::BAN_DELETE, ban_ids);
}

void LLPanelGroupBanlist::requestAndRefreshList()
{
	LLGroupMgr::getInstance()->sendGroupBanRequest(LLGroupMgr::REQUEST_GET, mGroupID);
	refreshList();
}

void LLPanelGroupBanlist::refreshList()
{
	LLGroupMgrGroupData* gdatap = LLGroupMgr::getInstance()->getGroupData(mGroupID);
	if(!gdatap)
	{
		LL_WARNS("Groups") << "Unable to get group data for group " << mGroupID << LL_ENDL;
		return;
	}
	
	mBanListCtrl->deleteAllItems();
	for (std::pair<LLUUID,LLGroupBanData> entry: gdatap->mBanList)
	{
		LLNameListCtrl::NameItem ban_entry;
		ban_entry.value = entry.first;
		LLGroupBanData bd = entry.second;
		
		ban_entry.columns.add().column("name").font.name("SANSSERIF_SMALL").style("NORMAL");
		
		// Baker TODO: MAINT-
		// Check out utc_to_pacific_time()
		
		ban_entry.columns.add().column("ban_date").value(bd.mBanDate.toHTTPDateString("%Y/%m/%d")).font.name("SANSSERIF_SMALL").style("NORMAL");
		
		mBanListCtrl->addNameItemRow(ban_entry);
	}
}
