/*
	Copyright (C) 2013-2026 Mark Tyler

	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 in the file COPYING.
*/

#include "private.h"



int mtQEX::dialogTextLine (
	QWidget		* const	par,
	QString		const	title,
	QString		const	label,
	QString		const	text,
	int		const	maxLength,
	QString			&result
	)
{
	privDialogText	dialog ( 0, par, title, label, text, maxLength );


	if ( dialog.exec () == QDialog::Accepted )
	{
		result = dialog.getText ();
		return 0;
	}

	return 1;
}

int mtQEX::dialogText (
	QWidget		* const	par,
	QString		const	title,
	QString		const	label,
	QString		const	text,
	QString			&result
	)
{
	privDialogText	dialog ( 1, par, title, label, text );


	if ( dialog.exec () == QDialog::Accepted )
	{
		result = dialog.getText ();
		return 0;
	}

	return 1;
}

void mtQEX::set_minimum_width (
	QLineEdit	* const	edit,
	int		const	length
	)
{
	if ( ! edit )
	{
		return;
	}

	QString txt;

	int const len = MIN ( length, 100 );

	for ( int i = 0; i < len; i++ )
	{
		txt.append ('0');
	}

	QFontMetrics fm = edit->fontMetrics ();
	edit->setMinimumSize ( fm.boundingRect (txt).width (), 0 );
}

privDialogText::privDialogText (
	int		const	textType,
	QWidget		* const	par,
	QString		const	title,
	QString		const	label,
	QString		const	text,
	int		const	maxLength
	)
	:
	QDialog		( par ),
	textLineEdit	(),
	textEdit	()
{
	QVBoxLayout	* vLayout;
	QDialogButtonBox * buttonBox;


	vLayout = new QVBoxLayout;

	vLayout->addWidget ( new QLabel ( label ) );

	if ( textType == 0 )
	{
		textLineEdit = new QLineEdit ( text );
		vLayout->addWidget ( textLineEdit );

		if ( maxLength > 0 )
		{
			textLineEdit->setMaxLength ( maxLength );
		}

		mtQEX::set_minimum_width ( textLineEdit, 25 );
	}
	else
	{
		textEdit = new QTextEdit ();
		textEdit->setPlainText ( text );
		vLayout->addWidget ( textEdit );
	}

	buttonBox = new QDialogButtonBox (
		QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
	vLayout->addWidget ( buttonBox );

	connect ( buttonBox, SIGNAL ( accepted () ), this, SLOT ( accept () ) );
	connect ( buttonBox, SIGNAL ( rejected () ), this, SLOT ( reject () ) );

	setLayout ( vLayout );

	setWindowTitle ( title );
}

QString privDialogText::getText ()
{
	if ( textLineEdit )
	{
		return textLineEdit->text ();
	}

	if ( textEdit )
	{
		return textEdit->toPlainText ();
	}

	return QString ();
}

void mtQEX::DialogAbout::add_info (
	char	const * const	title,
	char	const * const	message
	)
{
	QTextEdit * const text = new QTextEdit;

	m_tab_widget->addTab ( text, title );

	text->setAcceptRichText ( false );
	text->setReadOnly ( true );
	text->setText ( message );
}

mtQEX::DialogAbout::DialogAbout (
	QWidget		* const	parent,
	char	const * const	title,
	bool		const	modal
	)
	:
	QDialog		( parent )
{
	setWindowTitle ( title );

	if ( modal )
	{
		setModal ( true );
	}
	else
	{
		setModal ( false );
		setAttribute ( Qt::WA_DeleteOnClose );
	}

	resize ( 600, 600 );

	QVBoxLayout * vbox_main = new QVBoxLayout;
	setLayout ( vbox_main );

	m_tab_widget = new QTabWidget;
	vbox_main->addWidget ( m_tab_widget );

/// ----------------------------------------------------------------------------

	QDialogButtonBox * button_box = new QDialogButtonBox (
		QDialogButtonBox::Close );

	vbox_main->addWidget ( button_box );

	connect ( button_box, SIGNAL ( rejected () ), this, SLOT ( reject () ));
}

mtQEX::HelpDialog::HelpDialog (
	QWidget		* const	parent,
	mtKit::UserPrefs	& uprefs,
	char	const * const	html_path,
	char	const * const	title
	)
	:
	QDialog		( parent ),
	m_uprefs	( uprefs )
{
	setWindowTitle ( title );
	setAttribute ( Qt::WA_DeleteOnClose );

	setGeometry (
		m_uprefs.get_int ( MTKIT_HELP_HTML_WINDOW_X ),
		m_uprefs.get_int ( MTKIT_HELP_HTML_WINDOW_Y ),
		m_uprefs.get_int ( MTKIT_HELP_HTML_WINDOW_W ),
		m_uprefs.get_int ( MTKIT_HELP_HTML_WINDOW_H )
		);

	QTextBrowser * browser = new QTextBrowser ( this );
	browser->setOpenExternalLinks ( true );
	       // Opens http/https links in system browser
	browser->setOpenLinks ( true );
		// Follows internal links

	// Load directly from local file
	QString docPath ( html_path );
	browser->setSource ( QUrl::fromLocalFile(docPath) );

	QAction * backAction = new QAction ( QIcon::fromTheme("go-previous"),
		"Back", this);
	connect ( backAction, &QAction::triggered, browser,
		&QTextBrowser::backward );

	QAction * forwardAction = new QAction ( QIcon::fromTheme("go-next"),
		"Forward", this );
	connect ( forwardAction, &QAction::triggered, browser,
		&QTextBrowser::forward );

	QAction * homeAction = new QAction( QIcon::fromTheme("go-home"), "Home",
		this );
	connect( homeAction, &QAction::triggered, browser, &QTextBrowser::home);

	QToolBar *toolBar = new QToolBar(this);
	toolBar->setMovable ( false );
	toolBar->setToolButtonStyle ( Qt::ToolButtonTextBesideIcon );
	toolBar->addAction ( backAction );
	toolBar->addAction ( forwardAction );
	toolBar->addAction ( homeAction );

	auto updateNavigation = [backAction, forwardAction, browser]()
	{
		backAction->setEnabled(browser->isBackwardAvailable());
		forwardAction->setEnabled(browser->isForwardAvailable());
	};

	connect ( browser, &QTextBrowser::sourceChanged, this, [this,
		updateNavigation]()
	{
		// Delay by 0 ms : runs after current event processing
		QTimer::singleShot ( 0, this, updateNavigation );
	});

	updateNavigation();

	// Layout: toolbar on top, browser filling the rest
	QVBoxLayout * layout = new QVBoxLayout ( this );
	layout->setContentsMargins ( 0, 0, 0, 0 );
	layout->setSpacing ( 0 );
	layout->addWidget ( toolBar );
	layout->addWidget ( browser );
	setLayout ( layout );

	show();
}

mtQEX::HelpDialog::~HelpDialog()
{
	m_uprefs.set ( MTKIT_HELP_HTML_WINDOW_X, geometry().x () );
	m_uprefs.set ( MTKIT_HELP_HTML_WINDOW_Y, geometry().y () );
	m_uprefs.set ( MTKIT_HELP_HTML_WINDOW_W, geometry().width () );
	m_uprefs.set ( MTKIT_HELP_HTML_WINDOW_H, geometry().height () );
}

