#include "wx/wx.h" 
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

class MyApp: public wxApp
{
    virtual bool OnInit();
};

 
class MyFrame: public wxFrame
{
private:
    wxStaticText *landed;
    wxStaticText *winner;
    wxBoxSizer *sizer;
    int bet;
public:
 
    MyFrame(const wxString& title, 
           const wxPoint& pos, const wxSize& size);

    void OnQuit(wxCommandEvent& event);
    void OnHeads(wxCommandEvent& event);
    void OnTails(wxCommandEvent& event);
    void OnFlip(wxCommandEvent& event);
 
    DECLARE_EVENT_TABLE()
};

enum
{
    ID_FLIP = 1,
    ID_HEADS,
    ID_TAILS

};
 
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    EVT_BUTTON(ID_FLIP, MyFrame::OnFlip)
    EVT_BUTTON(ID_HEADS, MyFrame::OnHeads)
    EVT_BUTTON(ID_TAILS, MyFrame::OnTails)
END_EVENT_TABLE()
 
IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	srand(time(0));
    MyFrame *frame = new MyFrame( wxT("Coin Flip"), 
         wxPoint(50,50), wxSize(450,340) );
    frame->Show(TRUE);
    SetTopWindow(frame);
    return TRUE;
} 
 
MyFrame::MyFrame(const wxString& title, 
       const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append( wxID_EXIT, wxT("E&xit") );
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, wxT("&File") );
    SetMenuBar( menuBar );

    CreateStatusBar();
 	bet = 2;   
	
	sizer = new wxBoxSizer(wxVERTICAL);
	
	wxSizer *landedSizer = new wxBoxSizer(wxHORIZONTAL);
	landedSizer->Add(new wxStaticText(this,wxID_ANY,wxT("Landed on:")),0,wxALL,2);
	landed = new wxStaticText(this,wxID_ANY,wxT(""));
	landedSizer->Add(landed,0,wxALL,2);
	sizer->Add(landedSizer,0);
	
	wxSizer *betSizer = new wxBoxSizer(wxHORIZONTAL);
	betSizer->Add(new wxButton(this,ID_HEADS,wxT("Heads")),0,wxALL,2);
	betSizer->Add(new wxButton(this,ID_TAILS,wxT("Tails")),0,wxALL,2);
	sizer->Add(betSizer);
	
	winner = new wxStaticText(this,wxID_ANY,wxT("You haven't even played yet!"));
	sizer->Add(winner,1,wxALL,2);
	
	sizer->Add(new wxButton(this,ID_FLIP,wxT("Flip")),0,wxALL|wxEXPAND,2);


	wxTextCtrl* control = new wxTextCtrl(this,wxID_ANY,wxT("Edit me!"),
    wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY );
    sizer->Add(control);

	
	(*control) << 123.456 << wxT(" some text\n");

	
	
	SetSizer(sizer);
    sizer->Fit(this);
    
    
}

  
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close(TRUE);
}
 
void MyFrame::OnHeads(wxCommandEvent& WXUNUSED(event))
{
	bet = 0;
	SetStatusText(wxT("You bet on Heads!"));
}

void MyFrame::OnFlip(wxCommandEvent& WXUNUSED(event))
{
	int was = rand()%2;
	if (was==0) landed->SetLabel(wxT("Heads"));
	else landed->SetLabel(wxT("Tails"));
	
	if (was==bet)
		winner->SetLabel(wxT("You Won!"));
	else
		winner->SetLabel(wxT("You lost!"));
}

void MyFrame::OnTails(wxCommandEvent& WXUNUSED(event))
{
	bet = 1;
	SetStatusText(wxT("You bet on Tails!"));
}
