//-----------------------------------------------------------------------------
#include <apps/Common/wxAbstraction.h>
#include "DeviceConfigureFrame.h"
#include "DeviceHandler.h"

//-----------------------------------------------------------------------------
void DeviceHandler::CloseAndReopenDevice( void )
//-----------------------------------------------------------------------------
{
	if( !pDev_ )
	{
		return;
	}

	try
	{
		if( pDev_->isOpen() )
		{
			pDev_->close();
		}
		pDev_->open();
	}
	catch( const ImpactAcquireException& e )
	{
		if( pParent_ )
		{
			pParent_->WriteLogMessage( wxString::Format( wxT("Failed to close and re-open device %s(%s, %s).\n"), 
				ConvertedString(pDev_->serial.read()).c_str(),
				ConvertedString(e.getErrorString()).c_str(),
				ConvertedString(e.getErrorCodeAsString()).c_str() ) );
		}
	}
}

//-----------------------------------------------------------------------------
bool DeviceHandler::MessageToUser( const wxString& caption, const wxString& msg, bool boSilentMode, long style )
//-----------------------------------------------------------------------------
{
	bool boResult = false;
	if( boSilentMode )
	{
		if( pParent_ )
		{
			pParent_->WriteLogMessage( wxString::Format( wxT( "%s: %s\n"), caption.c_str(), msg.c_str() ) );
		}
		boResult = true;
	}
	else
	{
		wxMessageDialog dlg( pParent_, msg, caption, style );
		switch( dlg.ShowModal() )
		{
		case wxID_OK:
		case wxID_YES:
		case wxID_APPLY:
		case wxID_YESTOALL:
			boResult = true;
			break;
		default:
			break;
		}
	}
	return boResult;
}

//-----------------------------------------------------------------------------
int DeviceHandler::UpdateFirmware( bool /*boSilentMode*/ )
//-----------------------------------------------------------------------------
{
	if( pParent_ )
	{
		pParent_->WriteErrorMessage( wxT( "This device doesn't implement firmware updates\n") );
	}
	return urFeatureUnsupported;
}

//-----------------------------------------------------------------------------
int DeviceHandler::UpdateKernelDriver( bool /*boSilentMode*/ )
//-----------------------------------------------------------------------------
{
	if( pParent_ )
	{
		pParent_->WriteErrorMessage( wxT( "This device doesn't implement kernel driver updates\n") );
	}
	return urFeatureUnsupported;
}
