#include <windows.h>
#include <gdiplus.h>
#include <math.h>
#include <stdio.h>
#include "resource.h"
#include "Frontend.h"
#include "Splash.h"
#include "OnPaint.h"

#pragma comment( lib , "gdiplus.lib" )
#pragma comment( lib , "comctl32.lib" )
#pragma warning( disable : 4244 ) 

using namespace Gdiplus;

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE 
				   , PSTR, INT iCmdShow)
{

	char *ClassName = "Face Recognition";
	char *WindowName = "Face Recognition";

	// Prevents Multiple Instances ...
	if(FindWindow(ClassName,WindowName) != NULL) 
	{
		// 1st approach
		// Posts Error message and quits 
		/*
		MessageBox(NULL,"Face Recognition Software already running!",
		"Error",16);
		PostQuitMessage(0);
		return -1;
		*/

		// 2nd Approach
		// Brings the currently running instance to focus
		// /*
		HWND CurWin = FindWindow(ClassName,WindowName);
		PostMessage(CurWin,MSG_GETFOCUS,0,0);
		PostQuitMessage(0);
		return 0;
		// */

		// 3rd Approach
		// Does nothing ... allows multiple intsnces to run
	}

	MSG                 msg;
	WNDCLASS            wndClass;
	HINSTANCE hInst;
	HWND  hWnd;

	hInst = hInstance;

	wndClass.style          = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc    = WndProc;
	wndClass.cbClsExtra     = 0;
	wndClass.cbWndExtra     = 0;
	wndClass.hInstance      = hInstance;
	wndClass.hIcon          = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_FD1));
	wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndClass.lpszMenuName   = NULL;
	wndClass.lpszClassName  = ClassName;

	RegisterClass(&wndClass);

	hWnd = CreateWindow(
		ClassName,   // window class name
		WindowName,  // window caption
		WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,      // window style
		0,            // initial x position
		0,            // initial y position
		800,            // initial x size
		570,            // initial y size
		NULL,                     // parent window handle
		NULL,                     // window menu handle
		hInstance,                // program instance handle
		NULL);                    // creation parameters


	Ctrs.Init(hWnd,hInstance);

	ShowWindow(hWnd, iCmdShow);
	UpdateWindow(hWnd);

	Splash Sp;
	Sp.Show(hInst,hWnd);

	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}  // WinMain


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
	WPARAM wParam, LPARAM lParam)
{
	HDC          hdc;
	PAINTSTRUCT  ps;

	switch(message)
	{
	case MSG_GETFOCUS:
		SetForegroundWindow(hWnd);
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case TRAIN_BTN:
			OnTrain ();
			cOnPaint.SetState(OnPnt::Train);
			break;
		case RECOGN_BTN:
			OnRecog();
			cOnPaint.SetState(OnPnt::Recog);
			break;
		case FEXT_BTN:
			OnFext();
			cOnPaint.SetState(OnPnt::Fext);
			break;
		case EXIT_BTN:
			PostQuitMessage(0);
			break;
		}

		break;
   case WM_CREATE:
		OnCreate();
		break;

   case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      OnPaint(hdc);
      EndPaint(hWnd, &ps);
      break;

   case WM_DESTROY:
      PostQuitMessage(0);
      break;

   default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
	return 0;	// All is well that ends well
} // WndProc




