/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2012  Oleg Linkin
 *
 * 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.
 **********************************************************************/

#pragma once

#include <QWidget>
#include <interfaces/blogique/iprofilewidget.h>
#include "profiletypes.h"
#include "ui_profilewidget.h"

class QStandardItemModel;
class QStandardItem;

namespace LeechCraft
{
namespace Blogique
{
namespace Metida
{
	class LJFriendEntry;
	class LJProfile;
	class FriendsProxyModel;

	class ProfileWidget final : public QWidget
							  , public IProfileWidget
	{
		Q_OBJECT
		Q_INTERFACES (LeechCraft::Blogique::IProfileWidget)

		Ui::ProfileWidget Ui_;

		enum Columns
		{
			Name
		};
		
		LJProfile *Profile_;
		QStandardItemModel *FriendsModel_;
		FriendsProxyModel *FriendsProxyModel_;
		QStandardItemModel *GroupsModel_;
		QStandardItemModel *FriendsInGroupModel_;
		QStandardItemModel *FriendsNotInGroupModel_;
		QStandardItemModel *CommunitiesModel_;
		QHash<QStandardItem*, LJFriendGroup> Item2FriendGroup_;
		QHash<QStandardItem*, LJFriendEntry_ptr> Item2Friend_;
		QMap<LJFriendEntry_ptr, QStandardItem*> Friend2Item_;
		QHash<QString, LJFriendEntry_ptr> Username2Friend_;

	public:
		ProfileWidget (LJProfile *profile, QWidget *parent = 0);
	private:
		void RereadProfileData ();
		void FillFriends (const QList<LJFriendEntry_ptr>& friends);
		void FillGroups (const QList<LJFriendGroup>& friendGroups);
		void FillCommunities (const QStringList& communities);
		void ReFillModels ();

	public slots:
		void updateProfile ();
	private slots:
		void on_ColoringFriendsList__toggled (bool toggle);
		void on_Add__released ();
		void on_Edit__released ();
		void on_Delete__released ();
		void on_UpdateProfile__released ();
		void on_Groups__clicked (const QModelIndex& index);
		void on_AddUserToGroup__released ();
		void on_RemoveUserFromGroup__released ();
		void on_SendMessage__released ();
		void handleUserGroupChanged (const QString& username,
				const QString& bgColor, const QString& fgColor, int groupId);
		void handleFriendFilterTextChanged (const QString& text);
		void handleReadJournal ();
		void handleSendMessage ();
		void handleReadCommunity ();
		void handleFriendsViewDoubleClicked (const QModelIndex& index);
		void handleCommunitiesViewDoubleClicked (const QModelIndex& index);
		
		void addNewGroup ();
		void deleteGroup ();
		void editGroup ();
		
	signals:
		void coloringItemChanged ();
	};
}
}
}
