#include "msgwidget.h"
#include "ui_msgwidget.h"

MsgWidget::MsgWidget(QString avatar, AligneAvatar aligneAvatar, int width, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MsgWidget),
    widthEdit(100)
{
    ui->setupUi(this);

    setAvatar(avatar);

    if(aligneAvatar == AligneAvatar::Left)
        setAligneAvatarLeft();
    else
        setAligneAvatarRight();

    setWidth(width);
}

MsgWidget::~MsgWidget()
{
    delete ui;
}

void MsgWidget::setAligneAvatarLeft()
{
    ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Avatar);
    ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Text);
    ui->horizontalLayout_Main->removeItem(ui->horizontalLayout_Add);

    ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Avatar);
    ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Text);
    ui->horizontalLayout_Main->addLayout(ui->horizontalLayout_Add);

    ui->editText->setObjectName("MsgWidgetEditTextLocal");
    ui->lblTime->setObjectName("MsgWidgetLblTimeLocal");

    ui->lblTime->setAlignment(Qt::AlignLeft);
}

void MsgWidget::setAligneAvatarRight()
{
    ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Avatar);
    ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Text);
    ui->horizontalLayout_Main->removeItem(ui->horizontalLayout_Add);

    ui->horizontalLayout_Main->addLayout(ui->horizontalLayout_Add);
    ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Text);
    ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Avatar);

    ui->editText->setObjectName("MsgWidgetEditTextRemote");
    ui->lblTime->setObjectName("MsgWidgetLblTimeRemote");

    ui->lblTime->setAlignment(Qt::AlignRight);
}


void MsgWidget::setAvatar(QString avatar)
{
    QPixmap pix(avatar);
    ui->lblAvatar->setPixmap( pix.scaled(32,32) );
}

void MsgWidget::setWidth(int width)
{
    this->setFixedWidth(width);
    widthEdit = width - 200;
}

void MsgWidget::setText(QString text)
{
    ui->editText->setText(text);
}

void MsgWidget::setTime(QString timeStr)
{
    ui->lblTime->setText(timeStr);
}

void MsgWidget::on_editText_textChanged()
{
    QString text = ui->editText->toPlainText();
    QFontMetrics metricsFont(ui->editText->font()); //метрики шрифта

    //геометрические параметры текста (высота/ширина в пикселях). В одну строку
    QRect textRect = metricsFont.boundingRect(QRect(0, 0, 0, 0), 0, text);

    int X = 10; // отступы

    if(textRect.width() > widthEdit)
    {//Не помещается в одну строку
        textRect = metricsFont.boundingRect(QRect(0, 0, widthEdit, 1000), 0, text);


        if(textRect.width() > widthEdit)
        {
            QRect textRect1str = metricsFont.boundingRect(QRect(0, 0, widthEdit, 1000), 0, "string");
            int ost = textRect.width() % widthEdit;
            int cnt = textRect.width() / widthEdit;
            if(ost > 0)
                cnt += 1;

            this->setFixedHeight(textRect1str.height() * cnt + (ost? textRect1str.height() : 0) + X + 20 + 30);
            ui->editText->setFixedHeight(textRect1str.height() * cnt + (ost? textRect1str.height() : 0) + X);

        }
        else
        {
            this->setFixedHeight(textRect.height() + X + 20 + 30);
            ui->editText->setFixedHeight(textRect.height() + X);

        }
    }
    else
    {//В одну строку
        ui->editText->setFixedWidth(textRect.width() + X);
        ui->editText->setFixedHeight(textRect.height() + X);
        this->setFixedHeight(textRect.height() + X + 20 + 30);
    }
}
