#include <common/logowin.hpp>

char LogoWindow::smszClassName[]={"NepalNight"};

LogoWindow::LogoWindow(HINSTANCE hInstance,HINSTANCE hLibrary)
: mhInstance(hInstance), mhLibrary(hLibrary), mIsDestroyed(FALSE), mhBitmap(0)
{
	mCreateHandler.setCallback(this,&LogoWindow::createHandler);
	mTimerHandler.setCallback(this,&LogoWindow::timerHandler);
	mPaintHandler.setCallback(this,&LogoWindow::paintHandler);
	mDestroyHandler.setCallback(this,&LogoWindow::destroyHandler);
	mCloseHandler.setCallback(this,&LogoWindow::closeHandler);
	insertHandler(VectorHandler::CreateHandler,&mCreateHandler);
	insertHandler(VectorHandler::TimerHandler,&mTimerHandler);
	insertHandler(VectorHandler::PaintHandler,&mPaintHandler);
	insertHandler(VectorHandler::DestroyHandler,&mDestroyHandler);
	insertHandler(VectorHandler::CloseHandler,&mCloseHandler);
	registerClass();
}

LogoWindow::~LogoWindow()
{
	if(mhBitmap)::DeleteObject(mhBitmap);
	if(::IsWindow(*this))::DestroyWindow(*this);
	removeHandlers();
}

void LogoWindow::removeHandlers(void)
{
	removeHandler(VectorHandler::CreateHandler,&mCreateHandler);
	removeHandler(VectorHandler::TimerHandler,&mTimerHandler);
	removeHandler(VectorHandler::PaintHandler,&mPaintHandler);
	removeHandler(VectorHandler::DestroyHandler,&mDestroyHandler);
	removeHandler(VectorHandler::CloseHandler,&mCloseHandler);
}

WORD LogoWindow::showLogo(String &bitmapName)
{
	RECT windowRect;
	HWND hCaptureWindow(::GetCapture());

	if(mhLibrary)mhBitmap=::LoadBitmap(mhLibrary,bitmapName);
	else mhBitmap=::LoadBitmap(mhInstance,bitmapName);
	if(!mhBitmap)
	{
		mIsDestroyed=TRUE;
    return FALSE;
	}
	centerRect(mhBitmap,windowRect);
	::CreateWindow((LPSTR)smszClassName,0,WS_POPUP|WS_BORDER,
		windowRect.left,windowRect.top,windowRect.right,windowRect.bottom,
		0x00,0x00,mhInstance,(LPSTR)(Window*)this);
	show(SW_SHOW);
	update();
	::SetCapture(*this);
	waitCursor(TRUE);
	while(!mIsDestroyed)messageLoop();
	waitCursor(FALSE);
	::SetCapture(hCaptureWindow);
	return TRUE;
}

void LogoWindow::registerClass(void)
{
	WNDCLASS wndClass;

	if(::GetClassInfo(mhInstance,smszClassName,(WNDCLASS FAR *)&wndClass))return;
	wndClass.style	       =CS_HREDRAW|CS_VREDRAW;
	wndClass.lpfnWndProc	 =(WNDPROC)Window::WndProc;
	wndClass.cbClsExtra	   =0;
	wndClass.cbWndExtra	   =sizeof(LogoWindow*);
	wndClass.hInstance	   =mhInstance;
	wndClass.hIcon	       =0;
	wndClass.hCursor	     =::LoadCursor(NULL,IDC_ARROW);
	wndClass.hbrBackground =(HBRUSH)::GetStockObject(LTGRAY_BRUSH);
	wndClass.lpszMenuName	 =0;
	wndClass.lpszClassName =smszClassName;
	::RegisterClass(&wndClass);
}

void LogoWindow::messageLoop(void)
{
  MSG msg;

	while(!mIsDestroyed)
	{
		while(::PeekMessage(&msg,0,0,0,PM_REMOVE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
	}
}

CallbackData::ReturnType LogoWindow::createHandler(CallbackData &/*someCallbackData*/)
{
	setTimer(TimerID,TimeOut);
	return (CallbackData::ReturnType)TRUE;
}

CallbackData::ReturnType LogoWindow::timerHandler(CallbackData &/*someCallbackData*/)
{
	killTimer(TimerID);
	::PostMessage(*this,WM_CLOSE,0,0L);
	return (CallbackData::ReturnType)0;
}

CallbackData::ReturnType LogoWindow::paintHandler(CallbackData &/*someCallbackData*/)
{
	paint();
	return (CallbackData::ReturnType)FALSE;
}

CallbackData::ReturnType LogoWindow::destroyHandler(CallbackData &/*someCallbackData*/)
{
	mIsDestroyed=TRUE;
	return (CallbackData::ReturnType)FALSE;
}

CallbackData::ReturnType LogoWindow::closeHandler(CallbackData &/*someCallbackData*/)
{
	::DestroyWindow(*this);
	mIsDestroyed=TRUE;
	return (CallbackData::ReturnType)FALSE;
}

void LogoWindow::paint()const
{
	drawBitmap(*this,mhBitmap);
}
