/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2013  Slava Barinov <rayslava@gmail.com>
 *
 * 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 <QScrollBar>
#include <QTimer>
#include <QMenu>
#include <QAction>
#include <interfaces/core/ientitymanager.h>
#include <interfaces/ihavetabs.h>
#include <interfaces/structures.h>
#include <interfaces/ihaverecoverabletabs.h>
#include "twitterinterface.h"
#include "twitdelegate.h"
#include "woodpecker.h"
#include "ui_twitterpage.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Woodpecker
{
	class TwitterPage : public QWidget
						, public ITabWidget
						, public IRecoverableTab
	{
		Q_OBJECT
		Q_INTERFACES (ITabWidget IRecoverableTab)

		const TabClassInfo TC_;

		QToolBar *Toolbar_;
		QMenu *DoctypeMenu_;
		QMenu *RecentFilesMenu_;
		IEntityManager *EntityManager_;
		QString Filename_;
		bool Modified_;
		QMap<QString, QList<QAction*>> WindowMenus_;
		QHash<QString, QString> Extension2Lang_;

		QObject *WrappedObject_;
		bool TemporaryDocument_;

		bool UpdateReady_;			/**< The flag is checked by timer for UI update */
		QTimer *UiUpdateTimer_;		/**< Timer checks UpdateReady_ and updates the UI */
		TwitDelegate *Delegate_;
		Ui::TwitterPage Ui_;
		TwitterInterface *Interface_;
		QTimer *TwitterTimer_;		/**< This timer sends network requests to get new twits */
		QSettings *Settings_;
		QList<Tweet_ptr> ScreenTwits_;

		QAction *ActionRetwit_;
		QAction *ActionReply_;
		QAction *ActionSPAM_;
		QAction *ActionOpenWeb_;
		QAction *ActionCopyText_;
		QAction *ActionDelete_;
		QAction *ActionSearch_;
		QAction *ActionShowFavorites_;
		QAction *ActionShowOwnFavorites_;
		QAction *ActionMakeFavorite_;
		QAction *ActionDeleteFavorite_;

		const KQOAuthParameters PageDefaultParam_;	/**< Default API request parameter set for page */
		const FeedMode PageMode_;					/**< API request mode for the page */

		Plugin *const ParentPlugin_;

	public:
		explicit TwitterPage (const TabClassInfo&, Plugin*,
				const FeedMode mode = FeedMode::HomeTimeline,
				const KQOAuthParameters& params = KQOAuthParameters ());
		~TwitterPage();

		void Remove ();
		QToolBar *GetToolBar () const;
		QObject *ParentMultiTabs ();
		QList<QAction*> GetTabBarContextMenuActions () const;
		QMap<QString, QList<QAction*>> GetWindowMenus () const;
		TabClassInfo GetTabClassInfo () const;

		QByteArray GetTabRecoverData () const;
		QString GetTabRecoverName () const;
		QIcon GetTabRecoverIcon () const;

	public slots:
		void tryToLogin ();
		void updateScreenTwits (QList<Tweet_ptr> twits);
		void recvdAuth (const QString& token, const QString& tokenSecret);
		void twit ();
		void retwit ();
		void reply (QListWidgetItem *index = nullptr);
		void reportSpam ();
		void sendReply ();
		void webOpen ();
		void openUserTimeline ();
		void openSearchTimeline ();
		void scrolledDown (int sliderPos);
		void setUpdateReady ();
		void copyTwitText ();
		void deleteTwit ();
		void makeFavorite ();
		void deleteFavorite ();
		void showFavorites ();
		void showOwnFavorites ();

	private slots:
		void on_TwitList__customContextMenuRequested (const QPoint&);
		void updateTweetList ();
		void requestUpdate ();

	signals:
		void removeTab (QWidget*);
		void changeTabName (QWidget*, const QString&);
		void changeTabIcon (QWidget*, const QIcon&);
		void changeTooltip (QWidget*, QWidget*);
		void statusBarChanged (QWidget*, const QString&);
		void couldHandle (const LeechCraft::Entity&, bool*);
		void delegateEntity (const LeechCraft::Entity&,
							 int*, QObject**);
		void gotEntity (const LeechCraft::Entity&);
		void tabRecoverDataChanged ();
	};
}
}
}

