#include "textinputdialog.h"
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>

TextInputDialog::TextInputDialog(QWidget *parent)
    : QDialog(parent), m_lineEdit(new QLineEdit(this)), m_sendButton(new QPushButton("Send", this))
{
  setWindowTitle("Send Text Message");

  auto *layout = new QVBoxLayout(this);
  layout->addWidget(m_lineEdit);
  layout->addWidget(m_sendButton);

  connect(m_lineEdit, &QLineEdit::returnPressed, this, &TextInputDialog::onSubmit);
  connect(m_sendButton, &QPushButton::clicked, this, &TextInputDialog::onSubmit);
}

void TextInputDialog::onSubmit()
{
  QString text = m_lineEdit->text().trimmed();
  if (text.isEmpty())
  {
    return;
  }

  emit textSubmitted(text);
  m_lineEdit->clear();
  hide();
}
