/**********************************************************************
 * 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.
 **********************************************************************/

#pragma once

#include <memory>
#include <QLineEdit>
#include "tagsconfig.h"
#include "categoryselector.h"

namespace LC
{
namespace Util
{
	class TagsCompleter;
	class LineEditButtonManager;

	/** @brief A line edit class suitable for use with TagsCompleter.
	 *
	 * One would need this extra class because of custom behavior of both
	 * tags completer and line edit semantics.
	 *
	 * @sa TagsCompleter
	 */
	class UTIL_TAGS_API TagsLineEdit : public QLineEdit
	{
		Q_OBJECT

		friend class TagsCompleter;

		std::unique_ptr<CategorySelector> CategorySelector_;
		TagsCompleter *Completer_;

		QString Separator_;
	public:
		/** @brief Constructs the line edit widget.
		 *
		 * Creates the line edit widget.
		 *
		 * @param[in] parent Parent widget.
		 */
		TagsLineEdit (QWidget *parent);

		/** @brief Adds the selector widget to the line edit.
		 *
		 * Because this function uses the completion model, it should be
		 * used after a TagsCompleter has been set on this line edit.
		 *
		 * This function also creates an overlay button to aid user in
		 * selecting tags. The passed \em manager object is used (if it
		 * is not nullptr), otherwise a new LineEditButtonManager is
		 * created internally to manage this line edit.
		 *
		 * @param[in] manager The line edit buttons manager to use, or
		 * nullptr to create one.
		 *
		 * @sa TagsCompleter
		 */
		void AddSelector (LineEditButtonManager *manager = nullptr);

		/** @brief Returns the separator for the tags.
		 *
		 * The default separator is "; ".
		 *
		 * @sa SetSeparator()
		 */
		QString GetSeparator () const;

		/** @brief Sets the separator for the tags.
		 *
		 * This function doesn't update the text in the line edit.
		 *
		 * @sa GetSeparator()
		 */
		void SetSeparator (const QString&);
	public slots:
		/** @brief Completes the string.
		 *
		 * Completes the current text in line edit with completion passed
		 * throught string parameter.
		 *
		 * @param[in] string String with completion.
		 */
		void insertTag (const QString& string);

		/** @brief Sets thew new list of the available tags.
		 *
		 * The list of tags will be passed to the selector if it was
		 * added via AddSelector().
		 *
		 * @param[in] allTags The list of new available tags.
		 */
		void handleTagsUpdated (const QStringList& allTags);

		/** @brief Sets the currently selected tags.
		 *
		 * Sets the line edit text to tags joined by separator. If
		 * tags selector is installed via AddSelector(), the selector
		 * is updated as well.
		 *
		 * @param[in] tags The list of selected tags.
		 */
		void setTags (const QStringList& tags);
	private slots:
		void handleSelectionChanged (const QStringList&);
		void showSelector ();
	protected:
		virtual void keyPressEvent (QKeyEvent*);
		virtual void focusInEvent (QFocusEvent*);
		virtual void contextMenuEvent (QContextMenuEvent*);
		void SetCompleter (TagsCompleter*);
	private:
		QString textUnderCursor () const;
	signals:
		void tagsChosen ();
	};
}
}
