#include <QScrollBar>
#include "tabdialogmessenger.h"
#include "msgwidget.h"
#include "user.h"
#include "instructor.h"


OneDialogMessenger::OneDialogMessenger(User userLocalGUI, User userRemote, QWidget *parent):
    QListWidget(parent)
{
    this->setObjectName("oneDialogMessenger");

    this->userLocalGUI = userLocalGUI;
    this->userRemote = userRemote;

    setWordWrap(true);
    setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
}

OneDialogMessenger::~OneDialogMessenger()
{

}

void OneDialogMessenger::addMsgWidgetLocal(QString text, QString timeStr, QString name)
{
    addMsgWidget(text, timeStr, MsgWidget::DirectionMsg::Outgoing, name);
}

void OneDialogMessenger::addMsgWidgetRemote(QString text, QString timeStr, QString name)
{
    addMsgWidget(text, timeStr, MsgWidget::DirectionMsg::Incoming, name);
}

void OneDialogMessenger::addMsgWidget(QString text, QString timeStr, MsgWidget::DirectionMsg direction, QString name)
{
    QListWidgetItem *listWidgetItem = new QListWidgetItem();

    QString avatar = "";
    MsgWidget::AligneAvatar aligneAvatar = MsgWidget::AligneAvatar::Left;

    if(direction == MsgWidget::DirectionMsg::Outgoing)
    {//Исходящее
        if(static_cast<Instructor*>(&userLocalGUI)->getIsAdmin())
            avatar = ":/resources/icons/admin.png";
        else
            avatar = ":/resources/icons/instructor.png";

        aligneAvatar = MsgWidget::AligneAvatar::Right;
    }
    else
    {//Входящее
        if(userRemote.getTypeUserDB() == User::TypeUserDBTrainee)
            avatar = ":/resources/icons/trainee.png";
        else
        {
            if(static_cast<Instructor*>(&userRemote)->getIsAdmin())
                avatar = ":/resources/icons/admin.png";
            else
                avatar = ":/resources/icons/instructor.png";
        }

        aligneAvatar = MsgWidget::AligneAvatar::Left;
    }

    int widthW = width();
    int widthSB = verticalScrollBar()->size().width();
    //TODO Времянка
    widthW = 450;
    widthSB = 17;

    MsgWidget *msgWidget = new MsgWidget(avatar, aligneAvatar, widthW - widthSB);

    addItem(listWidgetItem);
    setItemWidget(listWidgetItem, msgWidget);

    msgWidget->setText(text);
    msgWidget->setTime(timeStr);
    msgWidget->setName(name);

    //Корректировка высоты item
    listWidgetItem->setSizeHint (QSize(10, msgWidget->height()));

    scrollToItem(listWidgetItem);
}
