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

using namespace Gdiplus;

void OnSplashPaint(HWND hWnd,HDC hdc) {
		
	int MaxX = 800,MaxY=600;
	
	Graphics graphics(hdc);

	Image InpImg( L"Images\\Splash.jpg");
	int iWidth = InpImg.GetWidth();
	int iHeight = InpImg.GetHeight();

	GetThumbnailImageAbort ImgDs = 0;
	/*Image *	InpImg = 
	b.GetThumbnailImage(iWidth,iHeight,ImgDs,NULL);*/

//	Image * InpImg = b.GetImage();

	graphics.DrawImage(
		&InpImg,
		MaxX / 2 - iWidth / 2,
		MaxY / 2 - iHeight/ 2 - 50,
		iWidth,
		iHeight
	);
		
	
}

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

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		OnSplashPaint(hWnd,hdc);
		EndPaint(hWnd,&ps);
		return 0;
		
	default:
		return DefWindowProc(hWnd,message,wParam,lParam);
	}
} // WndProc

void ShowPrevError() {
	LPVOID lpMsgBuf;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
	);
	
	// Display the string.
	MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
	// Free the buffer.
	LocalFree( lpMsgBuf );
}

void Splash::Create() {

	//HWND hWnd;

	wndClass.style          = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc    = (WNDPROC)SplWndProc;
	wndClass.cbClsExtra     = 0;
	wndClass.cbWndExtra     = 0;
	wndClass.hInstance      = hInst;
	wndClass.hIcon          = LoadIcon(hInst,MAKEINTRESOURCE(IDI_FD1));
	wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground  = (HBRUSH)GetStockObject(NULL_BRUSH);
	wndClass.lpszMenuName   = NULL;
	wndClass.lpszClassName  = "Splash";

	int x = RegisterClass(&wndClass);

	hWnd = CreateWindow(
		"Splash",   // window class name
		"Face Recognition",  // window caption
		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
		hInst,          // program instance handle
		NULL);			// creation parameters

	//ShowPrevError();

	ShowWindow(hWnd,SW_MAXIMIZE);
	//InvalidateRect(hWnd,NULL,TRUE);
}

void Splash::Time() {
	if(hWnd != NULL)
		SetTimer(hWnd,0,/*2000*/0,TIMERPROC(lpfnTimer));
}

void CALLBACK Destroy(  HWND hWnd,UINT ,UINT_PTR ,DWORD dwTime) {
	if(hWnd != NULL)
		DestroyWindow(hWnd);
}

void Splash::Show(HINSTANCE _hInst,HWND _hParenWnd) {
	hParenWnd = _hParenWnd;
	hInst = _hInst;
	lpfnTimer = Destroy;
	Create();
	Time();
}