// CalculatorUI.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorUI.h"
#include "Calculator.h"
#include "mmsystem.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// operation ids
#define NONE	 (0)
#define PLUS	 (1)
#define MINUS	 (2)
#define MULTIPLY (3)
#define DIVIDE   (4)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorUI dialog

void ringMyBell()
{
	sndPlaySound("ring.wav",SND_ASYNC);
}

void CCalculatorUI::initDataMember()
{
	m_buffer = _T("0");
	m_acummulationBuffer = _T("0");
	m_operationID = NONE;
	m_opKeyPressed = FALSE;
	m_opEqualJustPressed = FALSE;
	m_overflowError = FALSE;
	m_unaryOperand = TRUE;
}

CCalculatorUI::CCalculatorUI(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorUI::IDD, pParent)
{
	initDataMember();
	//{{AFX_DATA_INIT(CCalculatorUI)
	m_view = _T("0");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	engine = new Calculator();
}

CCalculatorUI::~CCalculatorUI()
{
	delete engine;
}

void CCalculatorUI::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorUI)
	DDX_Control(pDX, IDC_VIEW, m_viewControl);
	DDX_Text(pDX, IDC_VIEW, m_view);
	DDV_MaxChars(pDX, m_view, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorUI, CDialog)
	//{{AFX_MSG_MAP(CCalculatorUI)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_0, On0)
	ON_BN_CLICKED(IDC_1, On1)
	ON_BN_CLICKED(IDC_2, On2)
	ON_BN_CLICKED(IDC_3, On3)
	ON_BN_CLICKED(IDC_4, On4)
	ON_BN_CLICKED(IDC_5, On5)
	ON_BN_CLICKED(IDC_6, On6)
	ON_BN_CLICKED(IDC_7, On7)
	ON_BN_CLICKED(IDC_8, On8)
	ON_BN_CLICKED(IDC_9, On9)
	ON_BN_CLICKED(IDC_SIGN, OnSign)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_PLUS, OnPlus)
	ON_BN_CLICKED(IDC_MINUS, OnMinus)
	ON_BN_CLICKED(IDC_EQUALS, OnEquals)
	ON_BN_CLICKED(IDC_MULTIPLY, OnMultiply)
	ON_BN_CLICKED(IDC_DIVIDE, OnDivide)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorUI message handlers

BOOL CCalculatorUI::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCalculatorUI::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCalculatorUI::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCalculatorUI::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCalculatorUI::OnNumberKey(CString s)
{
	if (m_overflowError == TRUE) {
		ringMyBell();
//		Beep(1000,500);
		return;
	}

	m_opKeyPressed = FALSE;
	m_opEqualJustPressed = FALSE;
	m_view = m_view + s;
	m_acummulationBuffer = m_view;

	UpdateData(FALSE);
}

void CCalculatorUI::On0() 
{
	// TODO: Add your control notification handler code here
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("0");
}

void CCalculatorUI::On1() 
{
	// TODO: Add your control notification handler code here
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("1");
}

void CCalculatorUI::On2() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("2");
}

void CCalculatorUI::On3() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("3");
}

void CCalculatorUI::On4() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("4");
}

void CCalculatorUI::On5() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("5");
}

void CCalculatorUI::On6() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("6");
}

void CCalculatorUI::On7() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("7");
}

void CCalculatorUI::On8() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("8");
}

void CCalculatorUI::On9() 
{
	// TODO: Add your control notification handler code here
	
	if (m_view.Compare("0") == 0) m_view = "";
	OnNumberKey("9");
}

void CCalculatorUI::OnSign() 
{
	// TODO: Add your control notification handler code here
	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}

	if (m_view.Compare("0") == 0 && m_opEqualJustPressed == FALSE) {
		m_acummulationBuffer = "0";
		UpdateData(FALSE);
		return;
	} else if (m_opEqualJustPressed == TRUE) {
		m_buffer = "0";
		UpdateData(TRUE);
		m_opEqualJustPressed = FALSE;
	}
	if (m_view.GetAt(0) == '-') {
		m_view = m_view.Right(m_view.GetLength()-1);
	} else if (m_view.Compare("0") != 0) {
		m_view = "-" + m_view;
	}
	m_acummulationBuffer = m_view;
	UpdateData(FALSE);
}

void CCalculatorUI::OnClear() 
{
	// TODO: Add your control notification handler code here
	initDataMember();
	m_view = _T("0");
	UpdateData(FALSE);
}

void CCalculatorUI::doCalculation()
{
	CString result = "0";
	BOOL overflow = FALSE;

	if (m_operationID == NONE) return;
	switch(m_operationID) {
	case PLUS:
		engine->plus(m_buffer,m_view,result,overflow);
		break;
	case MINUS:
		engine->minus(m_buffer,m_view,result,overflow);
		break;
	case MULTIPLY:
		engine->multiply(m_buffer,m_view,result,overflow);
		break;
	case DIVIDE:
		engine->divide(m_buffer,m_view,result,overflow);
		break;
	}
	m_view = result;
	if (overflow) {
		m_view = "overflow";
		m_overflowError = TRUE;
	}
	UpdateData(FALSE);
}

void CCalculatorUI::saveBuffer() 
{
	m_buffer = m_view;
	m_view = "0";
	m_opKeyPressed = TRUE;
}

void CCalculatorUI::OnPlus() 
{
	// TODO: Add your control notification handler code here
	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}
	if (m_opKeyPressed || m_opEqualJustPressed) {
		m_operationID = PLUS;
		m_viewControl.GetWindowText(m_acummulationBuffer);
		return;
	}
	m_opEqualJustPressed = FALSE;
	doCalculation();
	m_operationID = PLUS;
	saveBuffer();
//	::MessageBox(NULL,m_view,"ok",0);
}

void CCalculatorUI::OnMinus() 
{
	// TODO: Add your control notification handler code here
	
	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}
	if (m_opKeyPressed || m_opEqualJustPressed) {
		m_operationID = MINUS;
		m_viewControl.GetWindowText(m_acummulationBuffer);
		return;
	}
	m_opEqualJustPressed = FALSE;
	doCalculation();
	m_operationID = MINUS;
	saveBuffer();
}

void CCalculatorUI::OnMultiply() 
{
	// TODO: Add your control notification handler code here
	
	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}
	if (m_opKeyPressed || m_opEqualJustPressed) {
		m_operationID = MULTIPLY;
		m_viewControl.GetWindowText(m_acummulationBuffer);
		return;
	}
	m_opEqualJustPressed = FALSE;
	doCalculation();
	m_operationID = MULTIPLY;
	saveBuffer();
}

void CCalculatorUI::OnDivide() 
{
	// TODO: Add your control notification handler code here
	
	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}
	if (m_opKeyPressed || m_opEqualJustPressed) {
		m_operationID = DIVIDE;
		m_viewControl.GetWindowText(m_acummulationBuffer);
		return;
	}
	m_opEqualJustPressed = FALSE;
	doCalculation();
	m_operationID = DIVIDE;
	saveBuffer();
}

void CCalculatorUI::OnEquals() 
{
	// TODO: Add your control notification handler code here

	if (m_overflowError == TRUE) {
		ringMyBell();
		return;
	}
	if (m_view.GetLength() == 0 && 
		(m_acummulationBuffer.Compare("0") != 0)) {
		m_view = m_acummulationBuffer;
	} else if (m_opEqualJustPressed) {
		m_view = m_acummulationBuffer;
	} else if (m_unaryOperand) {
		m_view = m_acummulationBuffer;
		m_unaryOperand = FALSE;
	}
	doCalculation();
	m_buffer = m_view;
	m_view = "0";
	m_opEqualJustPressed = TRUE;
}

