// SystemConfig.cpp: implementation of the CSystemConfig class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "6046dryend.h"
#include "SystemConfig.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CSystemConfig class implementation.

CSystemConfig::CSystemConfig( void )
                 :tagSYSTEMCONFIG()
{
    m_fpFile = NULL;
}

CSystemConfig::~CSystemConfig( void )
{
    Close();

    m_fpFile = NULL;
}

bool CSystemConfig::Open( const EACCESS   &reAccess )
{
    bool bSuccess = false;

    CString FileName;

    FileName.Format( "%sDryEndConfig.cfg",   static_cast<LPCTSTR>( CSystemConfig::GetAppPath()) );

    LPCTSTR pszFileName = static_cast<LPCTSTR>( FileName );

    // Ensure any existing file is closed, first.

    Close();

    // Open the newly specified file with the appropriate access rights.

    switch ( reAccess )
    {
        case accessWrite:

            bSuccess = (( m_fpFile = ::fopen( pszFileName, "wb" ) ) != NULL );
            break;

        case accessRead:
            
            // Open the file for read access. If it fails, it probably doesn't exist so try to create
            // it then re-open with read access rights.

            if ( ( m_fpFile = ::fopen( pszFileName, "rb" ) ) == NULL )
            {
                if ( ( m_fpFile = ::fopen( pszFileName, "wb" ) ) != NULL )
                {
					SetDefault();
                    Save();
                    Close();

                    bSuccess = ( ( m_fpFile = ::fopen( pszFileName, "rb" ) ) != NULL );
                }
            }

            bSuccess = ( m_fpFile != NULL );

            break;
    }

    return bSuccess;
}

bool CSystemConfig::Close( void )
{
    if ( m_fpFile != NULL )
    {
        fclose( m_fpFile );
        m_fpFile = NULL;
    }

    return ( m_fpFile == NULL );
}

bool CSystemConfig::IsOpen( void ) const
{
    return ( m_fpFile != NULL );
}

bool CSystemConfig::Save( void )
{
    if ( IsOpen() )
    {
        return ( ::fwrite( this, tagSYSTEMCONFIG::Size(), 1, m_fpFile ) == 1 );
    }

    return false;
}

bool CSystemConfig::Read( void )
{
    bool bSuccess = false;

    if ( IsOpen() )
    {
        bSuccess = ( ::fread( this, tagSYSTEMCONFIG::Size(), 1, m_fpFile ) == 1 );
    }

    if ( ! bSuccess )
    {
        tagSYSTEMCONFIG::Reset();
    }

    return bSuccess;
}

CSystemConfig::operator SYSTEMCONFIG * ( void )
{
    return static_cast<::SYSTEMCONFIG *>( this );
}

CString CSystemConfig::GetAppPath( void )
{
    CString Path;

    LPSTR pszAppPath = Path.GetBuffer( _MAX_PATH );

    ::GetModuleFileName( NULL, pszAppPath, _MAX_PATH );

    int iLength;

    if ( ( iLength = strlen( pszAppPath ) ) > 0 )
    {
        while ( --iLength )
        {
            if ( ( pszAppPath[ iLength ] == '\\' ) ||
                 ( pszAppPath[ iLength ] == ':' )   )
            {
                pszAppPath[ iLength + 1 ] = '\0';
                break;
            }
        }
    }

    Path.ReleaseBuffer();

    //// Finally append a backslash if there's not one.
    //
    //if ( ( iLength > 0 ) && ( Path[ iLength - 1 ] != '\\' ) )
    //{
    //    Path += _T( "\\" );
    //}

    return Path;
}

///////////////////////////////////////////////////////////////////////////////
// tagSUBSYSTEMCONFIG helpers.

tagSYSTEMCONFIG::tagSYSTEMCONFIG( void )
{
    
}

tagSYSTEMCONFIG::~tagSYSTEMCONFIG( void )
{

}

void tagSYSTEMCONFIG::Reset( void )
{
    ::memset( this, 0x00, Size() );
}

void tagSYSTEMCONFIG::SetDefault( void )
{
    // Zero everything then set explicit parameters as necessary thereafter.
	dSysMode			= 0;	
	
	bShowHFSS			= FALSE;
	bShowLFSS			= FALSE;
	bShowSBP			= FALSE;
	bShowAttitude		= FALSE;
	bShowDepth			= FALSE;
	bShowHeading		= FALSE;
	bShowASCII			= FALSE;
	bShowNav			= FALSE;
	bShowBathy			= FALSE;

	MAINWindowX			= 100;
	MAINWindowY			= 100;
	MAINToggleState		= 0;
	
	
	//Waterfalls
	//HFSSWindowRect;
	HFSSWindowPalette	= 0;
	HFSSBrightness		= 0;
	HFSSContrast		= 0;
	HFSSScaling			= 0;
	
	//LFSSWindowRect;
	LFSSWindowPalette	= 0;
	LFSSBrightness		= 0;
	LFSSContrast		= 0;
	LFSSScaling			= 0;

	//SBPWindowRect;
	SBPWindowPalette	= 0;
	SBPBrightness		= 0;
	SBPContrast			= 0;
	SBPScaling			= 0;

	//BATHY
	bathyPoints			= RGB(0,0,255);
	

	//TIME SERIES STUFF
	bShowRoll			= FALSE;
	bShowRollRate		= FALSE;
	bShowPitch			= FALSE;
	bShowPitchRate		= FALSE;
	bShowEastRate		= FALSE;
	bShowNorthRate		= FALSE;
	bShowDepthRate		= FALSE;

	clrTS1_Line1		= RGB(255,0,0);
	clrTS1_Line2		= RGB(0,255,0);
	clrTS1_Line3		= RGB(0,0,255);
	clrTS1_Line4		= RGB(0,0,0);
	clrTS2_Line1		= RGB(255,0,0);
	clrTS2_Line2		= RGB(0,255,0);
	clrTS2_Line3		= RGB(0,0,255);
	clrTS1_BK			= RGB(55,55,55);
	clrTS1_AXIS			= RGB(231,226,0);
	clrTS1_PLANE		= RGB(255,255,255);
	clrTS1_FONT			= RGB(255,255,255);
	clrTS2_BK			= RGB(55,55,55);
	clrTS2_AXIS			= RGB(231,226,0);
	clrTS2_PLANE		= RGB(255,255,255);
	clrTS2_FONT			= RGB(255,255,255);
	uiTS1_Scale			= 15;
	uiTS2_Scale			= 15;

	
	TSWindowX			= 0;
	TSWindowY			= 0;
	TSWindowCX			= 400;
	TSWindowCY			= 300;
	
	TSWindowUpdateRate	= 0;

	//Depth
	DEPWindowX			= 200;
	DEPWindowY			= 200;
	DEPWndWater			= RGB(25,25,200);
	DEPWndShip			= RGB(255,0,0);
	DEPWndAxis			= RGB(255,255,255);
	DEPWndText			= RGB(255,255,255);
	DEPWndLines			= RGB(190,190,190);
	DEPWndHistory		= RGB(20,200,130);
	
	//Compass/Yaw
	HEDWindowX			= 0;
	HEDWindowY			= 0;
	
	//NAV DISPLAY
	NAVWindowX			= 0;
	NAVWindowY			= 0;

	//ASCII Display
	ASCIWindowX			= 0;
	ASCIWindowY			= 0;

	//Dlg Popup Report Options
	bQueryOnConnect		= FALSE;
	bAutoHealth			= FALSE;
	ulHealthInterval	= 10;
	
	bSyncInterval		= FALSE;
	uiSyncIntervalTime	= 10;	//seconds

	//Dlg TCP & GENERAL
	dRemoteAddress		= 2130706433;
	dRemotePort			= 6046;
	bSync6046Time		= FALSE;
	bAutoSyncStartup	= FALSE;
	uiMaxFileSize		= 200;
	uiMaxFileOverlap	= 100;
	sprintf(targetDir, "C:\\Datastore6046");

	//Dlg Sensor Setup

	//Dlg params - HFSS
	dHFSSRange			= 0;
	dHFSSPower			= 0;

	//Dlg params2 - LFSS
	dLFSSRange			= 0;
	dLFSSPower			= 0;

	//Dlg SBP params
	fSBPDuration		= 0;
	dSBPPower			= 0;
	dSBPGain			= 0;

	//7K Dialog
	bFlip				= FALSE;
	bPingActive			= TRUE;
	fMinRange			= 0;
	fMaxRange			= 0;
	fMinDepth			= 0;
	fMaxDepth			= 0;

	range =	100;
	rate  = 1;
	power =	pulse = gain = 0;
}

size_t tagSYSTEMCONFIG::Size( void )
{
    return sizeof( struct tagSYSTEMCONFIG );
}

