// EngraveSetup.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "MachDevice.h"
#include "probeconfig.h"
#include "XMLProfile.h"
#include "MenuHandler.h"
#include "Mach4View.h"
#include "Engine.h"
#include "TrajectoryControl.h"
#include ".\probeconfig.h"

extern TrajectoryControl *MainPlanner;
extern CMach4View *MachView;
extern TrajBuffer *Engine;

double m_ImageX;
double m_ImageY;
double m_StepsX;
double m_StepsY;
int m_TriggerLen;
double m_YStepOver;
double m_length;
double m_height;
double m_feedrate;

extern double estartx;
extern double estarty;
extern bool edir;

extern EngraveImage import;


//Monitor thread

EngraveSetup *monitor = NULL;
extern EngraveThread* pThread;

IMPLEMENT_DYNCREATE(EngraveThread, CWinThread)

EngraveThread::EngraveThread(  )
{

}

EngraveThread::~EngraveThread()
{
	if( monitor != NULL )
	{
		monitor->CloseWindow();
		monitor = NULL;
	}
}

BOOL EngraveThread::InitInstance()
{
	if( monitor != NULL ) return false;
	monitor = new EngraveSetup();
	monitor->DoModal();
	delete monitor;
	monitor = NULL;
	return TRUE;
}

int EngraveThread::ExitInstance()
{

	pThread = NULL;
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(EngraveThread, CWinThread)
END_MESSAGE_MAP()



extern CXMLProfile *DevProf;
extern MenuHandler *Menus;


// EngraveSetup dialog

IMPLEMENT_DYNAMIC(EngraveSetup, CDialog)
EngraveSetup::EngraveSetup(CWnd* pParent /*=NULL*/)
	: CDialog(EngraveSetup::IDD, pParent)
	
	, m_40MS(FALSE)
{
	Loaded = false;
}

EngraveSetup::~EngraveSetup()
{

}

void EngraveSetup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_LENGTH, m_length);
	DDX_Text(pDX, IDC_WIDTH, m_height);
	DDX_Text(pDX, IDC_EFEEDRATE, m_feedrate);
	DDX_Control(pDX, IDC_PICTURE, m_Pic);
	DDX_Control(pDX, IDC_LENGTH, m_LengthBox);
	DDX_Text(pDX, IDC_XSIZE, m_ImageX);
	DDX_Text(pDX, IDC_YSIZE, m_ImageY);
	DDX_Text(pDX, IDC_XPIXEL, m_StepsX);
	DDX_Text(pDX, IDC_YPIXEL, m_StepsY);
	DDX_Text(pDX, IDC_TRIGGERLEN, m_TriggerLen);
	DDX_Text(pDX, IDC_YSTEPOVER, m_YStepOver);
	DDX_Check(pDX, IDC_40MS, m_40MS);
}


BEGIN_MESSAGE_MAP(EngraveSetup, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_ECANCEL, OnBnClickedCancel)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_CALC, OnBnClickedCalc)
	ON_WM_TIMER()
	ON_WM_DRAWITEM()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// EngraveSetup message handlers

BOOL EngraveSetup::OnInitDialog()
{
	m_Type = 0;
	CDialog::OnInitDialog();
	UpdateData(false);

	m_40MS = Engine->TriggerBits;
	static const TCHAR szFilters[] =
	{
		TEXT("Jpeg\0")                                       TEXT("*.jpg\0")
			TEXT("Bitmap\0")                                     TEXT("*.bmp\0")
			TEXT("Png files\0")                                  TEXT("*.png\0")
	};
	CFileDialog *dlg = new CFileDialog(true, "JPegs","*.jpg", OFN_OVERWRITEPROMPT, _T("Jpg FIles (*.JPG)\0*.jpg\0") ); 
	dlg->m_ofn.lpstrFilter = szFilters;
	if( dlg->DoModal() == IDCANCEL) 
	{
		delete dlg;
		return -1;
	}
	lpszPathName = dlg->GetPathName();
	delete dlg;
	OnBnClickedLoadimage();

	if( !Loaded )
	{
		MessageBox( "Image load error!");
		return -1;
	}
	m_ImageX = import.image->GetWidth();
	m_ImageY = import.image->GetHeight();
	//convert image to grey scale..
	import.image->GrayScale();
	UpdateData(false);
	//do Calcs
	OnBnClickedCalc();


	UpdateData(false);
	//SetTimer(1,100,NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void EngraveSetup::OnBnClickedOk()
{
	UpdateData(true);
	Engine->TriggerBits = m_40MS;
	if(m_height < 0 || m_length < 0)
	{
		AfxMessageBox("ERROR... Width and Length must set be > 0.00");
		return;
	}
	if(m_feedrate < 0)
	{
		AfxMessageBox("ERROR... Rate must set be > 0.00");
		return;
	}
	if( m_TriggerLen > 1024000)
	{
		AfxMessageBox("ERROR... Must be fewer than 1 million trigger bits.");
		return;
	}
    if( !Menus->OKtoStart() ) OnOK();
	else
	{
	  Menus->SEQUENCE = SEQ_START;
	  estartx = 0;
	  estarty = 0;
	  edir = true;
	  Menus->SetMachError("Press Cycle Start to begin scanning Seq.");
	  OnOK();
	}
}

void EngraveSetup::OnBnClickedCancel()
{
	OnCancel();
}


void EngraveSetup::OnBnClickedLoadimage()
{
	
	if( import.LoadImage( lpszPathName ))
	{
		
		Loaded = true;
	}
	else
		Loaded = false;
}

void EngraveSetup::OnPaint()
{
	CDialog::OnPaint();
	if( Loaded) 
	{
		CRect sz;
		CPaintDC dc( &m_Pic ); // device context for painting
		m_Pic.GetClientRect(&sz);
		import.image->Stretch(dc, sz );
	}
 
    
}

void EngraveSetup::OnBnClickedCalc()
{
	// calculate the image run data
    UpdateData(true); //get any new data
	//first, the size of the Y output pixel in steps. 
	m_StepsX = (MainPlanner->StepsPerAxis[0] * m_height) / m_ImageX;
	m_StepsY = (MainPlanner->StepsPerAxis[1] * m_length) / m_ImageY;
    m_TriggerLen = (int)(MainPlanner->StepsPerAxis[0] * m_height);
	UpdateData(false);

}

void EngraveSetup::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnTimer(nIDEvent);
}

void EngraveSetup::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	// TODO: Add your message handler code here and/or call default

	 
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

HBRUSH EngraveSetup::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
	
	return hbr;
}
