#include "messangercontroller.h"

MessangerController::MessangerController(ConnectorToServer* connectorToServer, QObject *parent) :
    QObject(parent),
    connectorToServer(connectorToServer),
    userLocalGUI_ID(0)
{
    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->initUserLocalGUI(connectorToServer->getInstructor(userLocalGUI_ID));

    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(QString::number(userLocalGUI_ID), QString::number(id_user), msg.text, msg.time.toString("hh:mm")));
                else
                    msgWdgt->showReceivedMessage(ClientMessage(QString::number(id_user), QString::number(userLocalGUI_ID), 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();

    mapAlldialogsWithClients.clear();
}

void MessangerController::setUserLocalGUI_ID(int id)
{
    userLocalGUI_ID = id;

    for(MessangerWidget* widget : listWidgets)
    {
        widget->initUserLocalGUI(connectorToServer->getInstructor(userLocalGUI_ID));
    }
}

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

    showMessageInAllWidgets(clientMessage);
}

void MessangerController::slot_receiveMessage(ClientMessage clientMessage)
{
    showMessageInAllWidgets(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;
            }
        }
    }
}

void MessangerController::showMessageInAllWidgets(ClientMessage clientMessage)
{
    int id_user = 0;
    MessageOfMessanger msg;

    if(clientMessage.fromId == QString::number(userLocalGUI_ID))
    {//Send
        id_user = clientMessage.toId.toInt();
        msg.flLocal = true;
    }
    else
    {//Receive
        id_user = clientMessage.fromId.toInt();
        msg.flLocal = false;
    }


    QList<MessageOfMessanger>* list = nullptr;
    if(mapAlldialogsWithClients.contains(id_user))
    {//Уже есть
        list = mapAlldialogsWithClients.value(id_user);
    }
    else
    {//Новый
        list = new QList<MessageOfMessanger>();
        mapAlldialogsWithClients.insert(id_user, list);
    }

    msg.text = clientMessage.Text;
    msg.time = QTime::currentTime();
    list->append(msg);

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

    for(MessangerWidget* widget : listWidgets)
    {
        if(widget->getIDuserRemote() == id_user)
        {
            if(msg.flLocal)
                widget->showSendedMessage(clientMessage);
            else
                widget->showReceivedMessage(clientMessage);
        }
    }
}
