/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  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 "colorlisteditorwidget.h"
#include <QStandardItemModel>
#include <QColorDialog>
#include <QPainter>
#include <QtDebug>
#include "xmlsettingsmanager.h"

Q_DECLARE_METATYPE (QList<QColor>);

namespace LeechCraft
{
namespace Azoth
{
	namespace
	{
		enum ColorListRoles
		{
			Color = Qt::UserRole + 1
		};
	}

	ColorListEditorWidget::ColorListEditorWidget (QWidget *parent)
	: QWidget (parent)
	, Model_ (new QStandardItemModel (this))
	{
		Ui_.setupUi (this);
		Ui_.ColorsView_->setModel (Model_);

		reject ();
	}

	void ColorListEditorWidget::AddColor (const QColor& color)
	{
		QPixmap px (32, 32);
		QPainter p (&px);
		p.fillRect (px.rect (), color);
		p.end ();

		auto item = new QStandardItem;
		item->setText (color.name ());
		item->setIcon (px);
		item->setForeground (color);
		item->setData (color, ColorListRoles::Color);
		Model_->appendRow (item);
	}

	void ColorListEditorWidget::accept ()
	{
		QList<QColor> colors;
		for (int i = 0, rc = Model_->rowCount (); i < rc; ++i)
		{
			auto item = Model_->item (i);
			colors << item->data (ColorListRoles::Color).value<QColor> ();
		}

		XmlSettingsManager::Instance ().setProperty ("OverrideColorsList",
				QVariant::fromValue (colors));
	}

	void ColorListEditorWidget::reject ()
	{
		Model_->clear ();
		const auto& var = XmlSettingsManager::Instance ()
				.property ("OverrideColorsList");
		const auto& colors = var.value<QList<QColor>> ();
		for (const auto& color : colors)
			AddColor (color);
	}

	void ColorListEditorWidget::on_AddColorButton__released ()
	{
		QColorDialog dia (this);
		if (dia.exec () != QDialog::Accepted)
			return;

		AddColor (dia.selectedColor ());
	}

	void ColorListEditorWidget::on_RemoveColorButton__released ()
	{
		const auto& index = Ui_.ColorsView_->currentIndex ();
		if (!index.isValid ())
			return;

		Model_->removeRow (index.row ());
	}
}
}
