/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * 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 "setstatusdialog.h"
#include "xmlsettingsmanager.h"
#include "core.h"
#include "customstatusesmanager.h"
#include "proxyobject.h"

namespace LeechCraft
{
namespace Azoth
{
	namespace
	{
		QByteArray BuildSettingName (const QString& context, State st)
		{
			return QString ("FastStatusText_%1_%2")
					.arg (context)
					.arg (st)
					.toUtf8 ();
		}

		State GetStateForIndex (int index)
		{
			switch (index)
			{
				case 1:
					return SChat;
				case 2:
					return SAway;
				case 3:
					return SDND;
				case 4:
					return SXA;
				case 5:
					return SOffline;
				default:
					return SOnline;
			}
		}
	}

	SetStatusDialog::SetStatusDialog (const QString& context, QWidget *parent)
	: QDialog (parent)
	, Context_ (context)
	{
		Ui_.setupUi (this);
		on_StatusBox__currentIndexChanged ();

		connect (this,
				SIGNAL (accepted ()),
				this,
				SLOT (save ()));

		for (int i = 0; i < Ui_.StatusBox_->count (); ++i)
		{
			const auto state = GetStateForIndex (i);
			Ui_.StatusBox_->setItemIcon (i, Core::Instance ().GetIconForState (state));
			Ui_.StatusBox_->setItemData (i, QVariant::fromValue (state), Roles::ItemState);

			const auto& name = BuildSettingName (Context_, state);
			const auto& str = XmlSettingsManager::Instance ().property (name).toString ();
			Ui_.StatusBox_->setItemData (i, str, Roles::StateText);

			if (Ui_.StatusBox_->currentIndex () == i)
				Ui_.StatusText_->setText (str);
		}

		const auto& customs = Core::Instance ().GetCustomStatusesManager ()->GetStates ();
		for (const auto& custom : customs)
		{
			const auto state = custom.State_;
			const auto& name = custom.Name_ + " (" + ProxyObject ().StateToString (state) + ")";
			Ui_.StatusBox_->addItem (Core::Instance ().GetIconForState (state), name);

			const auto pos = Ui_.StatusBox_->count () - 1;
			Ui_.StatusBox_->setItemData (pos,
					QVariant::fromValue (state), Roles::ItemState);
			Ui_.StatusBox_->setItemData (pos, custom.Text_, Roles::StateText);
		}
	}

	State SetStatusDialog::GetState () const
	{
		const auto idx = Ui_.StatusBox_->currentIndex ();
		return Ui_.StatusBox_->itemData (idx, Roles::ItemState).value<State> ();
	}

	QString SetStatusDialog::GetStatusText () const
	{
		return Ui_.StatusText_->toPlainText ();
	}

	void SetStatusDialog::save ()
	{
		const auto& name = BuildSettingName (Context_, GetState ());
		XmlSettingsManager::Instance ().setProperty (name, GetStatusText ());
	}

	void SetStatusDialog::on_StatusBox__currentIndexChanged ()
	{
		const auto idx = Ui_.StatusBox_->currentIndex ();
		Ui_.StatusText_->setText (Ui_.StatusBox_->itemData (idx, Roles::StateText).toString ());
	}
}
}
