#include "src/ui/core-finder-dialog.hpp"

#include <QMessageBox>
#include "src/core/file-controller.hpp"

ParallelCoreFinderDialog::ParallelCoreFinderDialog() :
	QProgressDialog( nullptr ),
	m_dialogOpen( new bool( false ) )
{
	setLabelText( tr( "Searching for latest core version..." ) );
	setMinimum( 0 );
	setMaximum( 0 );
}

void ParallelCoreFinderDialog::closeEvent( QCloseEvent *event ) {
	*m_dialogOpen = false;
	QProgressDialog::closeEvent( event );
}

void ParallelCoreFinderDialog::showEvent( QShowEvent *event ) {
	std::shared_ptr<bool> dialogOpen = m_dialogOpen;
	*dialogOpen = true;

	m_build.reset();
	ParallelCoreBuilds::getLatest(
		[this,dialogOpen](const ParallelCoreVersion &version) {
			if( *dialogOpen ) {
				this->m_build = version;
				this->close();
			}
		},
		[this,dialogOpen]() {
			if( *dialogOpen ) this->close();
		}
	);

	QProgressDialog::showEvent( event );
}
