//-----------------------------------------------------------------------------
#include "DeviceConfigureFrame.h"
#include "DeviceHandlerHYPERION.h"
#include "KernelDriverUpdate.h"
#include "Epcs.h"
#include <sstream>
#include "wx/msgdlg.h"
#include "wx/ffile.h"
#include <apps/Common/wxAbstraction.h>

using namespace std;


//-----------------------------------------------------------------------------
int DeviceHandlerHYPERION::UpdateFirmware( bool boSilentMode )
//-----------------------------------------------------------------------------
{
	int result = urOperationSuccessful;
	MessageToUser( wxT("Information"), wxT("The firmware will now be updated. During this time(approx. 90 sec.) the application will not react. Please be patient.\n"), boSilentMode, wxOK | wxICON_INFORMATION );
	wxFileDialog fileDlg( NULL, wxT("Load an existing raw programming data file"), wxT(""), wxT(""), wxT("raw-programming-data Files (*.rpd)|*.rpd"), wxOPEN | wxFILE_MUST_EXIST );
	if( fileDlg.ShowModal() == wxID_OK )
	{
		wxFFile file(fileDlg.GetPath().c_str(), wxT("rb") );
		if( !file.IsOpened() )
		{
			if( pParent_ )
			{
				pParent_->WriteLogMessage( wxString::Format( wxT("ERROR! Could not open file %s.\n"), fileDlg.GetPath().c_str() ), wxTextAttr(wxColour(255, 0, 0)) );
			}
			return urFileIOError;
		}
		unsigned char *spiData = new unsigned char [file.Length() + 0x1000];
		int bytesread = static_cast<int>(file.Read( spiData, file.Length() ));
		file.Close();
		CEpcs epcs( pDev_ );
		epcs.AttachParent( pParent_ );
		epcs.DeviceConfigure();
		if( epcs.IsProtected() )
		{
			if( pParent_ )
			{
				pParent_->WriteLogMessage( wxT("ERROR! Serial flash memory is write protected, please switch S3 to position 'User' and repeat this process \n") );
			}
			return urDeviceAccessError;
		}
		epcs.BulkErase();
		int writeResult = epcs.Write( spiData, 0, bytesread, false );
		if( writeResult == 0 )
		{
			if( pParent_ )
			{
				pParent_->WriteLogMessage( wxT("Programming completed successfully.\n" ) );
				pParent_->WriteLogMessage( wxT("Please note, that the new software will only be activated after the system has been shutdown, switched off and swichted back on again. A simple reboot will NOT be sufficient.\n"), wxTextAttr(wxColour(255, 0, 0)) );
			}
			if( MessageToUser( wxT("Information"), wxT("The update task has now finished. It's recommended to verify the firmware now. Verify firmware?\n"), boSilentMode, wxOK | wxCANCEL | wxICON_INFORMATION ) )
			{
				int verifyResult = epcs.Verify( spiData, 0, bytesread );
				if( verifyResult != 0 )
				{
					if( pParent_ )
					{
						pParent_->WriteLogMessage( wxString::Format( wxT("ERROR! Verify failed (result: %d).\n"), writeResult ) );
					}
					result =  urDeviceAccessError;
				}
				else if( pParent_ )
				{
					pParent_->WriteLogMessage( wxString::Format( wxT("Verify the firmware finished successfully.\n") ) );
				}
			}
		}
		else
		{
			if( pParent_ )
			{
				pParent_->WriteLogMessage( wxString::Format( wxT("ERROR! Programming failed (result: %d).\n"), writeResult ) );
			}
			result = urDeviceAccessError;
		}
		delete [] spiData;
	}
	return result;
}
