#include "messangercontroller.h"

MessangerController::MessangerController(ConnectorToServer* connectorToServer, QObject *parent) :
    QObject(parent),
    connectorToServer(connectorToServer),
    GUIclientId("")
{
    connect(connectorToServer, &ConnectorToServer::signal_receiveMessage, this, &MessangerController::slot_receiveMessage);
}

MessangerController::~MessangerController()
{
    deleteAllWidgets();
}

MessangerWidget *MessangerController::newWidget(QWidget *parent, User* user, QVBoxLayout* boxLayout)
{
    MessangerWidget *msgWdgt = new MessangerWidget(parent);

    if(boxLayout)
        boxLayout->addWidget(msgWdgt);

    connect(msgWdgt, &MessangerWidget::signal_sendMessage, this, &MessangerController::slot_sendMessage);

    msgWdgt->initGUIclientId(GUIclientId);

    if(user)
    {
        msgWdgt->initUserRemote(*user);

        //Заполнение предыстории
        int id_user = user->getID();
        QList<MessageOfMessanger>* list = nullptr;
        if(mapAlldialogsWithClients.contains(id_user))
        {//Уже есть
            list = mapAlldialogsWithClients.value(id_user);

            for(MessageOfMessanger msg : *list)
            {
                if(msg.flLocal)
                    msgWdgt->showSendedMessage(ClientMessage(GUIclientId, QString::number(id_user), msg.text, msg.time.toString("hh:mm")));
                else
                    msgWdgt->showReceivedMessage(ClientMessage(QString::number(id_user), GUIclientId, msg.text, msg.time.toString("hh:mm")));
            }
        }
    }

    listWidgets.append(msgWdgt);

    return msgWdgt;
}

void MessangerController::deleteWidget(MessangerWidget *msgWdgt)
{
    for(int i = 0; i < listWidgets.count(); i++)
    {
        if(listWidgets.at(i) == msgWdgt)
        {
            delete msgWdgt;
            listWidgets.removeAt(i);
            break;
        }
    }
}

void MessangerController::deleteAllWidgets()
{
    for(MessangerWidget* widget : listWidgets)
        delete widget;
    listWidgets.clear();
}

void MessangerController::setGUIclientId(QString id_GUIclient)
{
    GUIclientId = id_GUIclient;

    for(MessangerWidget* widget : listWidgets)
    {
        widget->initGUIclientId(GUIclientId);
    }
}

void MessangerController::slot_sendMessage(ClientMessage clientMessage)
{
    connectorToServer->slot_sendMessage(clientMessage);

    int id_user = clientMessage.toId.toInt();
    QList<MessageOfMessanger>* list = nullptr;
    if(mapAlldialogsWithClients.contains(id_user))
    {//Уже есть
        list = mapAlldialogsWithClients.value(id_user);
    }
    else
    {//Новый
        list = new QList<MessageOfMessanger>();
        mapAlldialogsWithClients.insert(id_user, list);
    }
    MessageOfMessanger msg;
    msg.flLocal = true;
    msg.text = clientMessage.Text;
    msg.time = QTime::currentTime();
    list->append(msg);

    clientMessage.timeStr = msg.time.toString("hh:mm");

    for(MessangerWidget* widget : listWidgets)
    {
        widget->showSendedMessage(clientMessage);
    }
}

void MessangerController::slot_receiveMessage(ClientMessage clientMessage)
{
    int id_user = clientMessage.fromId.toInt();
    QList<MessageOfMessanger>* list = nullptr;
    if(mapAlldialogsWithClients.contains(id_user))
    {//Уже есть
        list = mapAlldialogsWithClients.value(id_user);
    }
    else
    {//Новый
        list = new QList<MessageOfMessanger>();
        mapAlldialogsWithClients.insert(id_user, list);
    }
    MessageOfMessanger msg;
    msg.flLocal = false;
    msg.text = clientMessage.Text;
    msg.time = QTime::currentTime();
    list->append(msg);

    clientMessage.timeStr = msg.time.toString("hh:mm");

    for(MessangerWidget* widget : listWidgets)
    {
        widget->showReceivedMessage(clientMessage);
    }
}

void MessangerController::slot_reinitMessangers(QList<User> listUsers)
{
    for(MessangerWidget* widget : listWidgets)
    {
        for(User user : listUsers)
        {
            if(user.getID() == widget->getIDuserRemote())
            {
                widget->reinitMessangerWidget(user);
                break;
            }
        }
    }
}
