// Statistics.cpp : implementation file
//

#include "stdafx.h"
#include "Noughts.h"
#include "Statistics.h"
#include "Statdialog.h"
#include "stdlib.h"
#include "GridView.h"
#include "NoughtsDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStatistics dialog


CStatistics::CStatistics(CWnd* pParent /*=NULL*/)
	: CDialog(CStatistics::IDD, pParent)
{
	windowon=FALSE;
	bwin = nwin = cwin = 0;
	player1win = player2win = 0;

	//{{AFX_DATA_INIT(CStatistics)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CStatistics::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStatistics)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

CStatistics::~CStatistics()
{
	endstats();
}

BEGIN_MESSAGE_MAP(CStatistics, CDialog)
	//{{AFX_MSG_MAP(CStatistics)
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON1, Oncleargamestats)
	ON_WM_CTLCOLOR()
	ON_WM_HELPINFO()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatistics message handlers

void CStatistics::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	endstats();
	CDialog::OnClose();
}

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


void CStatistics::dostats()
{
	if (!windowon)
	{
		Create(IDD_STATISTICS);
		Invalidate(TRUE);
		UpdateWindow();
		windowon=TRUE;
	}
	else
	{
		ShowWindow(SW_RESTORE);
		SetActiveWindow();
	}
}

void CStatistics::endstats()
{
	if (windowon) 
	{
		DestroyWindow();
		windowon=FALSE;
	}
}

int CStatistics::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	lpCreateStruct->x=20;
	lpCreateStruct->y=200;

	windowon = true;

	if (CDialog::OnCreate(lpCreateStruct) == -1)
	return -1;

	return 0;
}

void CStatistics::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	if (!windowon) return;	// Not shown yet

	dc.SetBkColor((COLORREF) backcolor);

	CSize textsize = dc.GetTextExtent(statline1);

	RECT textrect;
	textrect.top = 60;
	textrect.left = 20;
	textrect.bottom = 180;
	textrect.right = 140;

	dc.DrawText(statline1, &textrect, DT_RIGHT );

	CString	gamestats;
	char	buffer[33];

	int	total = nwin+cwin+bwin;
	_itoa(total, buffer, 10);
	gamestats = buffer;
	gamestats += "\n";

	_itoa(cwin, buffer, 10);
	gamestats += buffer;

	int	cpercent = 0;
	if (total) cpercent = cwin*100 / total;

	_itoa(cpercent, buffer, 10);

	gamestats += percent1;
	gamestats += buffer;
	gamestats += percent2;

	_itoa((nwin), buffer, 10);

	gamestats += buffer;

	int	npercent = 0;
	if (total) npercent = nwin*100 / total;

	_itoa(npercent, buffer, 10);

	gamestats += percent1;
	gamestats += buffer;
	gamestats += percent2;

	textrect.top = 60;
	textrect.left = 150;
	textrect.bottom = 320;
	textrect.right = 270; 

	dc.DrawText(gamestats, &textrect, DT_LEFT );

	textrect.top = 30;
	textrect.left = 410;
	textrect.bottom = 320;
	textrect.right = 490;

	gamestats = statline21;

	dc.DrawText(gamestats, &textrect, DT_LEFT );

	textrect.top = 30;
	textrect.left = 500;
	textrect.bottom = 55;
	textrect.right = 560;

	gamestats = statline22;

	dc.DrawText(gamestats, &textrect, DT_LEFT );

	textrect.top = 60;
	textrect.left = 320;
	textrect.bottom = 320;
	textrect.right = 380;

	gamestats = statline23 + "1";

	dc.DrawText(gamestats, &textrect, DT_RIGHT );

	textrect.top = 90;
	textrect.left = 320;
	textrect.bottom = 170;
	textrect.right = 380;

	gamestats = statline24;

	dc.DrawText(gamestats, &textrect, DT_RIGHT );

	textrect.top = 180;
	textrect.left = 320;
	textrect.bottom = 205;
	textrect.right = 380;

	gamestats = statline23 + "2";

	dc.DrawText(gamestats, &textrect, DT_RIGHT );

	textrect.top = 130;
	textrect.left = 20;
	textrect.bottom = 250;
	textrect.right = 140;

	gamestats = statline3;

	dc.DrawText(gamestats, &textrect, DT_RIGHT );

	textrect.top = 130;
	textrect.left = 150;
	textrect.bottom = 250;
	textrect.right = 270;

	itoa(player1win, buffer,10);
	gamestats = buffer;
	gamestats += "\n";
	itoa(player2win, buffer,10);
	gamestats += buffer;

	dc.DrawText(gamestats, &textrect, DT_LEFT );

	textrect.top = 210;
	textrect.left = 320;
	textrect.bottom = 320;
	textrect.right = 380;

	gamestats = statline24;

	dc.DrawText(gamestats, &textrect, DT_RIGHT );
}

void CStatistics::Oncleargamestats() 
{
		bwin = nwin = cwin = player1win = player2win = 0;
		Invalidate(TRUE);
		UpdateWindow();
}

void CStatistics::OnCancel() 
{

	CDialog::OnCancel();
}

HBRUSH CStatistics::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    switch (nCtlColor)
    {
        case CTLCOLOR_BTN:
        case CTLCOLOR_STATIC:
        {
            pDC->SetBkMode(TRANSPARENT);
        }
        case CTLCOLOR_DLG:
        {
            CBrush*     back_brush;
            COLORREF    color;
			color = (COLORREF) backcolor;
            back_brush = new CBrush(color);
            return (HBRUSH) (back_brush->m_hObject);
        }
    }
    return(CDialog::OnCtlColor(pDC, pWnd, nCtlColor));
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

BOOL CStatistics::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CButton* pButton = (CButton*) GetDlgItem(IDCANCEL);
	pButton->SetFocus();

	return FALSE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
