#include "Player.h"
#include "MainFrame.h"
#include "SimpleFramePipeline.h"

MainFrame::MainFrame(void) : MainFrameView(NULL)
{
	cv = new Player(this);
	SetAutoLayout(true);
 	GetSizer()->Add(cv);
 	this->Layout();
 	GetSizer()->Fit(this);
}

MainFrame::~MainFrame(void)
{
	delete cv;
}

void MainFrame::onOpenVideoFileMenuSelected( wxCommandEvent& event )
{
	wxString wildcard;
	wildcard.Printf(wxT("%s (*.avi)|*.avi"),_("AVI files"));
	wxString filename = wxFileSelector(_("Choose a video file"),wxT("."),0,0,wildcard,wxFILE_MUST_EXIST);

	if(!filename.IsEmpty()) {
		shared_ptr<Capture> capture = Capture::createFileCapture(filename.mb_str());
		if(capture->isValid()) {
			cv->setCapture(capture);
			Fit();
		} else {
			wxMessageBox(_("Unable to open this video file. Check if video codec is available."),_("Error"),wxICON_ERROR | wxCENTRE | wxOK, this);
		}
	}
}

void MainFrame::onQuitMenuSelected( wxCommandEvent& event )
{
	Close();
}

void MainFrame::onOpenCameraMenuSelected( wxCommandEvent& event )
{
	shared_ptr<Capture> capture = Capture::createCamCapture();
	if(capture->isValid()) {
		cv->setCapture(capture);
		Fit();
	} else 
		wxMessageBox(_("Unable to open camera."),_("Error"),wxICON_ERROR | wxCENTRE | wxOK, this);
}

void MainFrame::onDrawAxisMenuItemClicked( wxCommandEvent& event )
{
	wxMenuItem * item = sceneMenu->FindItem(ID_DRAW_AXIS);
	 static_cast<SimpleFramePipeline*>(cv->getLeftCanvas()->getPipeLine())->setDrawAxis(item->IsChecked());
}

void MainFrame::onDrawOcclusionWireframeMenuItemClicked( wxCommandEvent& event )
{
	wxMenuItem * item = sceneMenu->FindItem(ID_DRAW_OCCLUSION_WIREFRAME);
	static_cast<SimpleFramePipeline*>(cv->getLeftCanvas()->getPipeLine())->setDrawOcclusionWireframe(item->IsChecked());
}