/* BEGIN software license
 *
 * MsXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright 2009--2026 by Filippo Rusconi
 *
 * http://www.msxpertsuite.org
 *
 * This file is part of the MsXpertSuite project.
 *
 * The MsXpertSuite project is the successor of the massXpert project. This
 * project now includes various independent modules:
 *
 * - massXpert, model polymer chemistries and simulate mass spectrometric data;
 * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * END software license
 */


/////////////////////// Qt includes
#include <QScreen>


/////////////////////// pappsomspp includes


/////////////////////// Local includes
#include "MsXpS/libXpertMassGui/TextDetailsWidget.hpp"

namespace MsXpS
{
namespace libXpertMassGui
{

TextDetailsWidget::TextDetailsWidget(const QFont &font, QWidget *parent)
  : QWidget(parent)
{
  // 1. Window flags: Make it a frameless, always-on-top tooltip
  setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint |
                 Qt::WindowStaysOnTopHint);
  setAttribute(Qt::WA_TranslucentBackground);

  // 2. CRITICAL: Make the widget transparent to mouse events!
  // This ensures that when the tooltip pops up under the cursor,
  // the mouse events still pass through to the QCustomPlot underneath.
  setAttribute(Qt::WA_TransparentForMouseEvents, true);

  // Layout
  auto *layout = new QVBoxLayout(this);
  layout->setContentsMargins(0, 0, 0, 0); // No margins, let QTextEdit handle it

  // 3. The Multiline Edit Widget
  mp_textEdit = new QTextEdit(this);
  mp_textEdit->setReadOnly(true); // Prevent editing, but allow selection
  mp_textEdit->setFrameStyle(QFrame::NoFrame); // Remove default borders


  // Styling: Dark theme, monospace font for aligned numbers
  mp_textEdit->setStyleSheet(
    "QTextEdit {"
    "  background-color: rgba(25, 25, 30, 240);"
    "  color: #E0E0E0;"
    "  border: 1px solid #555;"
    "  border-radius: 4px;"
    "  selection-background-color: #0078D7;"
    "}");

  mp_textEdit->setFont(font);

  layout->addWidget(mp_textEdit);
  hide();
}

void
TextDetailsWidget::showText(const QString &text, const QPoint &globalMousePos)
{
  mp_textEdit->setPlainText(text);

  // QTextEdit doesn't auto-resize height perfectly.
  // We must set a fixed width, then calculate the required height based on the
  // document.
  int targetWidth = 260;
  mp_textEdit->document()->setTextWidth(targetWidth);

  // Force the document to recalculate its layout immediately
  // We use setPageSize with a massive height to force it to wrap and calculate
  mp_textEdit->document()->setPageSize(QSizeF(targetWidth, QWIDGETSIZE_MAX));

  // Calculate height needed for the content + a little padding
  int docHeight = mp_textEdit->document()->size().height();
  mp_textEdit->setFixedHeight(docHeight + 10);
  mp_textEdit->setFixedWidth(targetWidth + 10);

  adjustSize(); // Resize the outer QWidget to match the QTextEdit

  // Position the tooltip (offset so it doesn't cover the exact mouse point)
  QPoint tooltipPos = globalMousePos + QPoint(20, 20);

  // Keep it on screen
  QRect screenGeom = QApplication::primaryScreen()->availableGeometry();
  if(tooltipPos.x() + width() > screenGeom.right())
    {
      tooltipPos.setX(globalMousePos.x() - width() - 20);
    }
  if(tooltipPos.y() + height() > screenGeom.bottom())
    {
      tooltipPos.setY(globalMousePos.y() - height() - 20);
    }

  move(tooltipPos);
  show();
}

void
TextDetailsWidget::showHtml(const QString &text, const QPoint &globalMousePos)
{
  mp_textEdit->setHtml(text);


  // QTextEdit doesn't auto-resize height perfectly.
  // We must set a fixed width, then calculate the required height based on the
  // document.
  int targetWidth = 260;
  mp_textEdit->document()->setTextWidth(targetWidth);

  // Calculate height needed for the content + a little padding
  int docHeight = mp_textEdit->document()->size().height();
  mp_textEdit->setFixedHeight(docHeight + 10);
  mp_textEdit->setFixedWidth(targetWidth + 10);

  adjustSize(); // Resize the outer QWidget to match the QTextEdit

  // Position the tooltip (offset so it doesn't cover the exact mouse point)
  QPoint tooltipPos = globalMousePos + QPoint(20, 20);

  // Keep it on screen
  QRect screenGeom = QApplication::primaryScreen()->availableGeometry();
  if(tooltipPos.x() + width() > screenGeom.right())
    {
      tooltipPos.setX(globalMousePos.x() - width() - 20);
    }
  if(tooltipPos.y() + height() > screenGeom.bottom())
    {
      tooltipPos.setY(globalMousePos.y() - height() - 20);
    }

  move(tooltipPos);
  show();
}


} // namespace libXpertMassGui
} // namespace MsXpS
