
//==== Size of a file in bytes, or -1 if it can not be opened ====//
int FileSize( const string & in fname )
{
    file f;
    if ( f.open( fname, "r" ) < 0 )
    {
        return -1;
    }
    int sz = f.getSize();
    f.close();
    return sz;
}

//==== Every exported file should exist and have content in it ====//
int CheckExports( const array< string > & in fnames )
{
    int failure = 0;
    for ( uint i = 0; i < fnames.length(); i++ )
    {
        int sz = FileSize( fnames[i] );
        if ( sz <= 0 )
        {
            Print( "FAIL export " + fnames[i] + " missing or empty, size " + sz );
            failure++;
        }
        else
        {
            Print( "   " + fnames[i] + " " + sz + " bytes" );
        }
    }
    return failure;
}

int main()
{
    int failure = 0;

    Print( string( "Begin SVG Test" ) );
    Print( string( "" ) );
    failure += TestSVG();
    Print( string( "End SVG Test" ) );

    //==== Check For API Errors ====//
    while ( GetNumTotalErrors() > 0 )
    {
        ErrorObj err = PopLastError();
        Print( err.GetErrorString() );
        failure++;
    }

    return failure;
}

int TestSVG()
{
    int failure = 0;

    Print( string( "--> Generating Geometries and Testing SVG Export" ) );
    Print( string( "" ) );

    //==== Add Fuselage ====//
    string fid = AddGeom( "FUSELAGE", "" );
    SetParmVal( fid, "X_Rel_Location", "XForm", -9.0 );
    SetParmVal( fid, "Z_Rel_Location", "XForm", -1.0 );
    Update();

    //==== Add Wing ====//
    string wid = AddGeom( "WING", "" );

    //===== Insert A Couple More Sections =====//
    InsertXSec( wid, 1, XS_FOUR_SERIES );
    InsertXSec( wid, 1, XS_FOUR_SERIES );
    InsertXSec( wid, 1, XS_FOUR_SERIES );

    //===== Cut The Original Section =====//
    CutXSec( wid, 1 );

    //===== Change Driver =====//
    SetDriverGroup( wid, 1, AREA_WSECT_DRIVER, ROOTC_WSECT_DRIVER, TIPC_WSECT_DRIVER );

    SetParmVal( wid, "RotateAirfoilMatchDideralFlag", "WingGeom", 1.0 );

    //===== Change Some Parameters 1st Section ====//
    SetParmVal( wid, "Root_Chord", "XSec_1", 7.0 );
    SetParmVal( wid, "Tip_Chord", "XSec_1", 3.0 );
    SetParmVal( wid, "Area", "XSec_1", 45.0 );
    SetParmVal( wid, "Sweep", "XSec_1", 40.0 );
    Update();

    //===== Change Some Parameters 2nd Section ====//
    SetParmVal( wid, "Tip_Chord", "XSec_2", 2.0 );
    SetParmVal( wid, "Sweep", "XSec_2", 60.0 );
    SetParmVal( wid, "Dihedral", "XSec_2", 30.0 );
    Update();

    //===== Change Some Parameters 3rd Section ====//
    SetParmVal( wid, "Sweep", "XSec_3", 60.0 );
    SetParmVal( wid, "Dihedral", "XSec_3", 80.0 );
    Update();

    //==== Change Airfoil ====//
    SetParmVal( wid, "Camber", "XSecCurve_0", 0.02 );

    Update();

    string geom_id = FindContainer( "Vehicle", 0 );

    //==== Set Units ====//
    SetParmVal( FindParm( geom_id, "LenUnit", "SVGSettings" ), LEN_FT );

    //==== Default 4 View SVG Export ====//

    Print( string( "---> Test Write 4 View" ) );
    ExportFile( "TestSVG_4View.svg", SET_ALL, EXPORT_SVG );
    Print( string( "---> Default 4 View Export Success: Saved as TestSVG_4View.svg" ) );
    Print( string( "" ) );

    //==== Default 2 View SVG Export ====//
    SetParmVal( FindParm( geom_id, "ViewType", "SVGSettings" ), VIEW_2HOR );
    SetParmVal( FindParm( geom_id, "TopLeftView", "SVGSettings" ), VIEW_RIGHT );
    SetParmVal( FindParm( geom_id, "TopRightView", "SVGSettings" ), VIEW_LEFT );
    SetParmVal( FindParm( geom_id, "TopLeftRotation", "SVGSettings" ), ROT_0 );
    Update();

    Print( string( "---> Test Write 2 Horizontal View" ) );
    Print( string( "     Left: Right View, Right: Left View" ) );
    ExportFile( "TestSVG_2HView.svg", SET_ALL, EXPORT_SVG );
    Print( string( "---> 2 Horizontal View Export Success: Saved as TestSVG_2HView.svg" ) );
    Print( string( "" ) );

    //==== 1 View SVG Export ====//
    SetParmVal( FindParm( geom_id, "ViewType", "SVGSettings" ), VIEW_1 );
    SetParmVal( FindParm( geom_id, "TopLeftView", "SVGSettings" ), VIEW_TOP );
    SetParmVal( FindParm( geom_id, "TopLeftRotation", "SVGSettings" ), ROT_90 );
    Update();

    Print( string( "---> Test Write 1 View" ) );
    Print( string( "     Top view rotated 90 deg" ) );
    ExportFile( "TestSVG_1View.svg", SET_ALL, EXPORT_SVG );
    Print( string( "---> 2D 1 View Export Success: Saved as TestSVG_1View.svg" ) );
    Print( string( "" ) );

    //==== 2 Vertical View SVG Export ====//
    SetParmVal( FindParm( geom_id, "ViewType", "SVGSettings" ), VIEW_2VER );
    SetParmVal( FindParm( geom_id, "BottomLeftView", "SVGSettings" ), VIEW_REAR );
    SetParmVal( FindParm( geom_id, "BottomLeftRotation", "SVGSettings" ), ROT_0 );
    Update();

    Print( string( "---> Test Write 2D 2 Vertical View" ) );
    Print( string( "     Top: Top view rotated 90 deg, Bottom: Rear view rotated 0 deg" ) );
    ExportFile( "TestSVG_2VView.svg", SET_ALL, EXPORT_SVG );
    Print( string( "---> 2 Vertical View Export Success: Saved as TestSVG_2VView.svg" ) );
    Print( string( "" ) );

    //==== Open Each SVG File In A Viewer To Verify ====//
    Print( string( "-> Open Each SVG File In A SVG Viewer To Verify" ) );
    Print( string( "" ) );


    //==== Confirm each export actually landed on disk ====//
    array< string > exports = { "TestSVG_4View.svg",
                              "TestSVG_2HView.svg",
                              "TestSVG_1View.svg",
                              "TestSVG_2VView.svg" };
    failure += CheckExports( exports );

    return failure;
}
