/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Module configuration.
 *//*
 * LEGAL:   COPYRIGHT (C) 2007 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define PROGRAM_CONF_CC 1
#include <fstream>
#include "base/module.hh"
#include "base/conf.hh"
#include "base/conf_template_macros.hh"
#include "base/misc.hh"
#include "base/stream.hh"
using namespace base;
#include "program/module.hh"
#include "program/defs.hh"
#include "program/conf.hh"

namespace program {

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////  Conf  ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

namespace global { Conf gConf; }

/*****************************************************************************
 * ctor/dtor.
 *****************************************************************************/
Conf::Conf( void )
:   mValid(false),
    // cmd-line args 
    mPrintJoystick(false),
    // .conf file
    mTimerTickFreq(DEFAULT_TICK_FREQ),
    mSplashFontName(),
    mSplashFontSize(0),
    mSplashFadeMilliseconds(0),
    mSplashEndMilliseconds(0),
    mAircraftRotationList(),
    mCarrierModelName(),
    mCarrierRotationList()
{
}

/*****************************************************************************
 * Initialize/read configuration.
 *****************************************************************************/
void
Conf::Init( const base::InitArgs& initArgs, const string& confTxtFilename )
{
    CONF_TEMPLATE_Init_ParseCmdline_ReadConfTxt;
}

/*****************************************************************************
 * Parse cmdline args.
 *****************************************************************************/
void
Conf::ParseCmdlineArgs( int argc, char** argv )
{
    // NOP
}

void
Conf::ParseCmdlineArgs2( int argc, char** argv )
{
    // Parse args.
    for ( int i = 1; i < argc; ++i )
    {
        // Print joystick values (to help user to calibrate)?
        if ( STREQ( argv[i], "-h" )
          or STREQ( argv[i], "--help" )
          or STREQ( argv[i], "/?" ) )
        {
            COUT << "Help: Press F1 to start GUI that shows manual.html" << std::endl
                 << "http://www.palomino3d.org" << std::endl;
            CmdargDone( i );
        }
        // Print joystick values (to help user to calibrate)?
        else if ( STREQ( argv[i], "-jp" ) )
        {
            PROGRAM_CONF.mPrintJoystick = true;
            CmdargDone( i );
        }
    }
}

/*****************************************************************************
 * Read from .conf file.
 *****************************************************************************/
void
Conf::ReadConfTxt( const string& confTxtFilename )
{
    CONF_TEMPLATE_ReadConfTxt;
}

void
Conf::ModuleConfTxt::ProcessField( string& field, std::ifstream& conf )
{
    bool error = false;

    if ( field == "TimerTickFreq" )
    {
        conf >> PROGRAM_CONF.mTimerTickFreq;
        if ( (PROGRAM_CONF.mTimerTickFreq <= Milliseconds(0))
          or (PROGRAM_CONF.mTimerTickFreq >= Milliseconds(1000)) )
            error = true;
    }
    else if ( field == "Splash" )
    {
        conf >> PROGRAM_CONF.mSplashFontName;
        conf >> PROGRAM_CONF.mSplashFontSize;
        conf >> PROGRAM_CONF.mSplashFadeMilliseconds;
        conf >> PROGRAM_CONF.mSplashEndMilliseconds;
    }
    else if ( field == "AircraftRotationList" )
    {
        PROGRAM_CONF.mAircraftRotationList.Parse( conf );
    }
    else if ( field == "CarrierModel" )
    {
        conf >> PROGRAM_CONF.mCarrierModelName;
    }
    else if ( field == "CarrierRotationList" )
    {
        PROGRAM_CONF.mCarrierRotationList.Parse( conf );
    }

    // Errors?
    if ( error )
    {
        CERROR << "ERROR[conf]: parameter for " << field << " is invalid." << std::endl;
        EXIT( 1 );
    }
}

/*****************************************************************************
 * Print Conf configuration.
 *****************************************************************************/
std::ostream& operator<<( std::ostream& strm, const Conf& conf )
{
    return strm;
}

} // namespace program
