#include <wx/wx.h>
#include <wx/toolbar.h>

#include "wxg.h"
#include "gplane.h"

#include "../include/leonardo.h"
#include "../include/rectangle.h"
#include "shapefactory.h"
#include "graphicscontext.h"

using namespace goeland_wx;

BEGIN_EVENT_TABLE( wxGFrame, wxFrame )
        EVT_MENU( Menu_File_Quit, wxGFrame::OnQuit )
        EVT_MENU( Menu_File_About, wxGFrame::OnAbout )
        EVT_TOOL( IDT_TOOL_ARROW, wxGFrame::OnToolArrow )
        EVT_TOOL( IDT_TOOL_RECT, wxGFrame::OnToolRect )
        EVT_TOOL( IDT_TOOL_IMG, wxGFrame::OnToolImg )
END_EVENT_TABLE()

IMPLEMENT_APP(wxGapp)


bool
wxGapp::OnInit()
{
        wxGFrame *frame = new wxGFrame( wxT( "Goeland Example" ), wxPoint(50,50), wxSize(450,340) );

        frame->Show(TRUE);
        SetTopWindow(frame);
        return TRUE;
}

wxGFrame::wxGFrame( const wxString& title, const wxPoint& pos, const wxSize& size )
        : wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
        wxMenu *menuFile = new wxMenu;

        menuFile->Append( Menu_File_About, wxT( "&About..." ) );
        menuFile->AppendSeparator();
        menuFile->Append( Menu_File_Quit, wxT( "E&xit" ) );

        wxMenuBar *menuBar = new wxMenuBar;
        menuBar->Append( menuFile, wxT( "&File" ) );

        SetMenuBar( menuBar );
        SetAutoLayout( TRUE );

        toolbar = new wxToolBar( this, -1,
                wxDefaultPosition,
                wxDefaultSize,
                wxTB_FLAT | wxTB_3DBUTTONS );

        wxLayoutConstraints *lc = new wxLayoutConstraints;
        lc->left.SameAs    ( this, wxLeft );
        lc->top.SameAs     ( this, wxTop );
        lc->width.SameAs   ( this, wxWidth );
        lc->height.Absolute( 30 );
        toolbar->SetConstraints( lc );

        plane = new gPlane( this, -1, wxPoint(0,0), GetSize( ));
        lc = new wxLayoutConstraints;
        lc->left.SameAs    ( this, wxLeft );
        lc->top.Below        ( toolbar );
        lc->width.SameAs   ( this, wxWidth );
        lc->bottom.SameAs        ( this, wxBottom );
        plane->SetConstraints( lc );


        wxImage::AddHandler( new wxPNGHandler );
        toolbar->AddTool( IDT_TOOL_ARROW,
                wxBitmap( new wxImage( "/usr/share/goeland/images/arrow.png" )),
                wxNullBitmap,
                TRUE,
                -1, // xPos
                -1, // yPos
                NULL,
                wxString( _T( "Arrow" )),
                "" );
        toolbar->AddTool( IDT_TOOL_RECT,
                wxBitmap( new wxImage( "/usr/share/goeland/images/rectangle.png" )),
                wxNullBitmap,
                TRUE,
                -1, // xPos
                -1, // yPos
                NULL,
                wxString( _T( "Rectangle" )),
                "" );
        toolbar->AddTool( IDT_TOOL_IMG,
                wxBitmap( new wxImage( "/usr/share/goeland/images/rectangle.png" )),
                wxNullBitmap,
                TRUE,
                -1, // xPos
                -1, // yPos
                NULL,
                wxString( _T( "Image" )),
                "" );
        CreateStatusBar();
        SetStatusText( wxT( "Welcome to the Goeland Example !" ) );

        // Initialize Goeland

        using namespace goeland_base;
        leonardo = Leonardo::getInstance( );
        leonardo->setShapeFactory( new goeland_wx::ShapeFactory( ));
        leonardo->setGraphicsContext( new goeland_wx::GraphicsContext( plane ));

}

void
wxGFrame::OnQuit( wxCommandEvent& WXUNUSED( event ) )
{
        Close(TRUE);
}

wxGFrame::~wxGFrame( )
{
        delete leonardo->getGraphicsContext();
        delete leonardo;
}

void
wxGFrame::OnAbout( wxCommandEvent& WXUNUSED( event ) )
{
        wxMessageBox( wxT( "This is the Goeland Example" ),
                        wxT( "About Goeland Example" ), wxOK | wxICON_INFORMATION, this );
}


void wxGFrame::OnToolArrow( wxCommandEvent &event )
{
        printf( "Arrow\n" );
        toolbar->ToggleTool(IDT_TOOL_RECT, false);
        leonardo->selectTool( goeland_base::Leonardo::TOOL_ARROW );
}


void wxGFrame::OnToolRect( wxCommandEvent &event )
{
        printf( "Rect\n" );
        toolbar->ToggleTool(IDT_TOOL_ARROW, false);
        leonardo->selectTool( goeland_base::Leonardo::TOOL_RECTANGLE );
}


void wxGFrame::OnToolImg( wxCommandEvent &event )
{
        printf( "Rect\n" );
        toolbar->ToggleTool(IDT_TOOL_ARROW, false);
        leonardo->selectTool( goeland_base::Leonardo::TOOL_IMAGE );
}