//
// Copyright (C) 1999-2002 Toshikaz Hirabayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// TOSHIKAZ HIRABAYASHI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Except as contained in this notice, the name of Toshikaz Hirabayashi shall
// not be used in advertising or otherwise to promote the sale, use or other
// dealings in this Software without prior written authorization from
// Toshikaz Hirabayashi.

#include <win/WSDwinAppDev.h>
#include <win/WSDwinColor.h>
#include <win/WSDwinMouse.h>
#include <WSCcolorSet.h>
#include <win/WSDwindraw.h>
#include <WSDdev.h>

HINSTANCE hInst;
char szAppName[] = "Wide Studio Application";
char szTitle[]= "Wide Studio Application";

extern LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM uParam,LPARAM lParam);
//extern "C" {
//int WINAPI _cygwin_dll_entry(HANDLE h,DWORD reason,void* foo){
//  return 1;
//};
//};
BOOL InitApplication(HINSTANCE hInstance){
  WNDCLASS wc;
//  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.style = CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS;
  wc.lpfnWndProc = (WNDPROC)WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
WSMFtrace("XXZZim1 WinMan1 icon=0x%x  hinst=0x%x\n",wc.hIcon,hInstance);
  wc.hIcon = LoadIcon(hInstance,(char*)101);
  if (wc.hIcon == NULL){
    wc.hIcon = (HICON)::LoadImage(NULL,"main.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
WSMFtrace("XXZZim1 WinMan2 icon=0x%x\n",wc.hIcon);
//    wc.hIcon = LoadIcon(hInstance,szAppName);
//    wc.hIcon = (HICON)::LoadImage(hInstance,(char*)101,IMAGE_ICON,0,0,LR_DEFAULTCOLOR);
  }
  wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
  wc.lpszMenuName = szAppName;
  wc.lpszClassName = szAppName;
  return RegisterClass(&wc);
}

HWND hWnd;
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow){
  hInst = hInstance;
  hWnd = CreateWindow(szAppName,szTitle,WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,0,
                 CW_USEDEFAULT,0,
                 NULL,NULL,
                 hInstance,NULL);
  if (!hWnd) return FALSE;
//  ShowWindow(hWnd,nCmdShow);
//  UpdateWindow(hWnd);
  return TRUE;
}


int _win_main2(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){

  WSDdev::setDraw(new WSDwindraw());

WSMFtrace("main start:\n");
dbprintf("_win_main2 %s:%d start\n",__FILE__,__LINE__);
  if (!hPrevInstance){
    if (!InitApplication(hInstance)){
      if (WSGIappDev()->getMode() == WS_MODE_NORMAL){
dbprintf("_win_main2 %s:%d done1\n",__FILE__,__LINE__);
//TEST
//        return (FALSE);
      }
    }
  }
  if (!InitInstance(hInstance,nCmdShow)){
dbprintf("_win_main2 %s:%d done2\n",__FILE__,__LINE__);
    return FALSE;
  }

  WSDwinAppDev* winApp = WSGIwinAppDev();
  winApp->setInitPrm(lpCmdLine);
  winApp->setWnd(hWnd);
  winApp->setInstanceHandle(hInstance);
  winApp->initialize();


   WSGIwinAppMouse()->initialize();
WSMFtrace("main 1:\n");


//printf("cmd=#%s#",GetCommandLine());
   char* cmd = WSGFstrdup(GetCommandLine());
   long len = strlen(cmd)+1;
   long i;
   WSClistData plist;
   long qfl = 0;
   long ptr = 0;
   long word = 0;
   for(i=0; i<len; i++){
     if (qfl == 0 && cmd[i] == '\"'){
       qfl =1;
       word = 1;
       ptr = i+1;
     }else
     if (qfl == 1 && cmd[i] == '\"'){
       qfl =0;
       cmd[i] = 0;
       char* pr = WSGFstrdup(&cmd[ptr]);
       plist.add((void*)pr);
       word = 0;
     }else
     if (qfl == 0 && (cmd[i] == ' ' || cmd[i] == 0 || cmd[i] == '\t' || i == len -1) && word != 0){
       cmd[i] = 0;
       char* pr = WSGFstrdup(&cmd[ptr]);
       plist.add((void*)pr);
       word = 0;
     }else
     if (word == 0 && cmd[i] != ' ' && cmd[i] != 0 && cmd[i] != '\t'){
       ptr = i;
       word = 1;
     }
   }
   long lines = plist.getNum();
   char** cprm = new char*[lines+1];
   for(i=0; i<lines; i++){
     cprm[i] = (char*)plist[i];
//printf("#%s#\n",cprm[i]);
   } 
   cprm[lines] = NULL;
   winApp->setInitPrm(lines,cprm);
   delete cmd;
 
WSMFtrace("main 2:\n");

dbprintf("_win_main2 %s:%d done\n",__FILE__,__LINE__);
   return 0;
}

int WSGFwsInitialize(){
static long cnt = 0;
  HINSTANCE hInst = GetModuleHandle(NULL);
dbprintf("WSGFwsInitialize() %s:%d start hInst=0x%x cnt=%d\n",__FILE__,__LINE__,hInst,cnt);
cnt++;
  LPSTR cmd = GetCommandLine();
  _win_main2(hInst,NULL,cmd,TRUE);
  
  return 0;
}

extern "C" {
int _win_main(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
  _dbprintf_init();
  _win_main2(hInstance,hPrevInstance,lpCmdLine,nCmdShow);


  WSDwinAppDev* winApp = WSGIwinAppDev();
  winApp->execInitHandler();
  winApp->handleEvents();
  return 0;
}
};
