/* $Id: ClientMessages.h 3095 2012-09-17 02:11:22Z IMPOMEZIA $
 * IMPOMEZIA Simple Chat
 * Copyright © 2008-2012 IMPOMEZIA <schat@impomezia.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef CLIENTMESSAGES_H_
#define CLIENTMESSAGES_H_

#include <QObject>
#include <QHash>

#include "net/packets/MessageNotice.h"

class ClientCmd;
class MessageNotice;
class SimpleClient;

namespace Hooks
{
  class Messages;
}

/*!
 * Базовый класс обрабатывающий сообщения пользователя.
 *
 * \sa Hooks::Messages.
 */
class SCHAT_EXPORT ClientMessages : public QObject
{
  Q_OBJECT

public:
  ClientMessages(QObject *parent = 0);
  inline void add(Hooks::Messages *hook)    { if (!m_hooks.contains(hook)) m_hooks.append(hook); }
  inline void remove(Hooks::Messages *hook) { m_hooks.removeAll(hook); }
  QByteArray randomId() const;
  static bool isClientDate(int status);

  bool send(const QByteArray &dest, const QString &text);
  bool sendText(const QByteArray &dest, const QString &text, const QString &command = QString());
  void insert(MessageNotice *notice);

private slots:
  void channels(const QList<QByteArray> &channels);
  void notice(int type);

private:
  bool command(const QByteArray &dest, const ClientCmd &cmd);
  bool command(const QByteArray &dest, const QString &text, const QString &plain);
  void read(MessagePacket packet);
  void readText(MessagePacket packet);
  void sent(MessagePacket packet);

  QByteArray m_destId;                                ///< Текущий получатель сообщения.
  QHash<QByteArray, QList<MessagePacket> > m_pending; ///< Сообщения отображение которых отложена, т.к. не известна информация об отправителе.
  QList<Hooks::Messages*> m_hooks;                    ///< Хуки.
  SimpleClient *m_client;                             ///< Клиент чата.
};

#endif /* CLIENTMESSAGES_H_ */
