// Launcher.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "ctcls.h"
#include "ctcl.h"

#define MAX_LOADSTRING 100
#define WM_1ST_RUN (WM_USER + 100)

#pragma data_seg(".SHARED_DATA")
// all data in this section must be INITIALIZED!!!!!!!!!!!!!
BOOL g_bLaunched = FALSE;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED_DATA,RWS")
// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, LPSTR, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

bool _stdcall RunExec(const CStr& strExec);

void Do1stRun()
{
    char sz[1024];
    char szName[64];

    GetPrivateProfileString(SECT_CONFIG, "run", "", szName, sizeof(szName), PROF_INI);
    if (strlen(szName) > 0) {
        GetPrivateProfileString(SECT_GAMES, szName, "", sz, sizeof(sz), PROF_INI);

        CStr strExec(sz);
        strExec.TrimAll();
        //WinExec(sz, SW_SHOWNORMAL);
        RunExec(strExec);
    }
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;

    if (!g_bLaunched) {
        g_bLaunched = TRUE;

        if (CTCL_Start(_ECTCL_Launcher)) {
	        HACCEL hAccelTable;

	        // Initialize global strings
	        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	        LoadString(hInstance, IDC_LAUNCHER, szWindowClass, MAX_LOADSTRING);
	        MyRegisterClass(hInstance);

	        // Perform application initialization:
	        if (InitInstance (hInstance, lpCmdLine, nCmdShow)) {
	            hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_LAUNCHER);

	            // Main message loop:
                do {
                    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
                        if (msg.message == WM_QUIT)
                            break;
		                if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		                {
			                TranslateMessage(&msg);
			                DispatchMessage(&msg);
		                }
                    } else {
                        Sleep(3);
                    }
                    CTCL_Run();
                } while(TRUE);
            }
            CTCL_Stop();
        }

        g_bLaunched = FALSE;
    }

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_LAUNCHER);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
#ifdef _DEBUG
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_LAUNCHER;
#else // !_DEBUG
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszMenuName	= (LPCSTR)NULL;
#endif // _DEBUG
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;

    hInst = hInstance; // Store instance handle in our global variable

    BOOL bNoMax = strstr(lpCmdLine, "-nomax") ? TRUE: FALSE;

#ifdef _DEBUG
    DWORD dwExStyle = 0;
    DWORD dwStyle = WS_OVERLAPPEDWINDOW;
#else // !_DEBUG
    DWORD dwExStyle;
    DWORD dwStyle;
    if (bNoMax) {
        dwExStyle = 0;
        dwStyle = WS_OVERLAPPEDWINDOW;
    } else {
        dwExStyle = /*WS_EX_TOPMOST | */WS_EX_APPWINDOW; // top most & onto task bar
        dwStyle = WS_POPUP | WS_VISIBLE | WS_MAXIMIZE;
    }
#endif // _DEBUG
    hWnd = CreateWindowEx(dwExStyle, szWindowClass, szTitle, dwStyle, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }
    PostMessage(hWnd, WM_1ST_RUN, GetTickCount() + 3000, 0);

#ifdef _DEBUG
    ShowWindow(hWnd, nCmdShow);
#else // !_DEBUG
    if (bNoMax)
        ShowWindow(hWnd, nCmdShow); // SW_MINIMIZE
    else
        ShowWindow(hWnd, SW_MAXIMIZE); // SW_MINIMIZE
#endif // _DEBUG
    UpdateWindow(hWnd);
//#if !defined(_DEBUG)
//    SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
//#endif // !defined(_DEBUG)
    SetFocus(hWnd);
    SetForegroundWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message) 
	{
    case WM_1ST_RUN:
		if (GetTickCount() < wParam)
			PostMessage(hWnd, WM_1ST_RUN, wParam, 0);
		else
			Do1stRun();
        break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
			case IDM_ABOUT:
			   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			   break;
			case IDM_EXIT:
			   DestroyWindow(hWnd);
			   break;
			default:
			   return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
        /*
		RECT rt;
		GetClientRect(hWnd, &rt);
      {
        const char* pcszMsg = "  Mech Warrior 4  ";
		DrawText(hdc, pcszMsg, strlen(pcszMsg), &rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
      }
        */
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

// to avoid linking error
void CTCL_API CTCL_DoTerminateAppl()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoEndMission()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoMainShell()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoCreateGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoJoinGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_StartGame(BOOL bRealLaunch)
{
    &bRealLaunch;
    ASSERT(FALSE);
}

void CTCL_API CTCL_InitMechDatas()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckJoinGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckNetConnectedToServer()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_SetMissionParams()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckMissionParams()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_SetMechs()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckClientReady()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckServerReady()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DefaultHostSetup()
{
    ASSERT(FALSE);
}
