#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wsqllib.h>
#include <winlib/winlib.h>

#include "sqlbase.h"
#include "dbinit.h"
#include "dbutil.h"



static BOOL EmptyLine(char *s)
{
       char *p;

       if (! strlen(s))
               return TRUE;

       for (p = s; isspace(*p);) {
               if (! *(++p))
                       return TRUE;
       }

       return FALSE;
}


static BOOL LoadDLL(VARS *v, char *dllName)
{
       HMODULE hDll;
       char fName[256];

       sprintf(fName, "dat/%s", dllName);

       if (! (hDll = LoadLibrary(fName)))
               return FALSE;

       v->CheckExtraAccel =
                       (void (*)(WORD, VARS*))GetProcAddress(hDll,
                       "_CheckExtraAccel");

       v->GetAccelTableSize = (int (*)(void))GetProcAddress(hDll,
                       "_GetAccelTableSize");

       v->accelTable = (ACCEL*)GetProcAddress(hDll,
                       "_accelTable");

       v->extraMenuItems = (MENUITEM *)GetProcAddress(hDll, "_extraMenuItems");

       return TRUE;
}


HACCEL DefineAccel(VARS *v)
{
       HACCEL hAccel;
       ACCEL *accelTable;
       int oldSize, exSize, newSize;

       if (! (hAccel = LoadAccelerators(v->hInst, "Accel")))
               return 0;

       if (v->accelTable) {
               oldSize = CopyAcceleratorTable(hAccel, NULL, 0);
               exSize = v->GetAccelTableSize();
               newSize = oldSize + exSize;
               accelTable = (ACCEL*)malloc(sizeof(ACCEL) * newSize);
               CopyAcceleratorTable(hAccel, accelTable, oldSize);
               memcpy(accelTable + oldSize, v->accelTable,
                               sizeof(ACCEL) * exSize);
               DestroyAcceleratorTable(hAccel);
               if (! (hAccel = CreateAcceleratorTable(accelTable, newSize)))
                       return 0;
       }

       return hAccel;
}


BOOL GetLayout(VARS *v, char *iniName)
{
       SQLFIELD *newField, *prevField = NULL;
       char s[82], *p;
       int i;
       char fldName[20], *dflt = "";

       for (i = 1;; i++) {
               sprintf(fldName, "f%d", i);
               if (! GetPrivateProfileString("layout", fldName, dflt, s,
                               82, iniName)) {
                       if (i == 1)
                               return FALSE;
                       else
                               return TRUE;
               }

               if (! (p = strtok(s, ":")))
                       return FALSE;

               newField = (SQLFIELD*)malloc(sizeof(SQLFIELD));

               if (! strncmp(p, "@", 1))
                       newField->title = NewString("");
               else
                       newField->title = NewString(p);

               if (! (p = strtok(NULL, ":")))
                       return FALSE;
               newField->name = NewString(p);

               if (! (p = strtok(NULL, ":")))
                       return FALSE;
               newField->x = atoi(p);

               if (! (p = strtok(NULL, ":")))
                       return FALSE;
               newField->y = atoi(p);
               if (newField->y > v->yMax)
                       v->yMax = newField->y;

               if (! (p = strtok(NULL, ":")))
                       return FALSE;
               newField->labelWidth = atoi(p);

               if (! (p = strtok(NULL, ":")))
                       return FALSE;
               newField->editWidth = atoi(p);

               newField->next = NULL;

               if (! v->fields)
               v->fields = newField;

               if (prevField)
               prevField->next = newField;

               prevField = newField;
               v->nFields++;
       }
}


BOOL InitDB(VARS *v, LPSTR cmdLine)
{
       char iniName[80], *p, *p2, s[82];
       int i, done = 0;
       char *dflt = "";
       char dllName[256];
       LOGINDATA loginData;

       if (! strlen(cmdLine)) {
               MessageBox(NULL, "Missing parameters", "SQLBase",
                               MB_OK | MB_ICONERROR);
               return FALSE;
       }
       sprintf(iniName, "dat/%s.ini", cmdLine);

       v->fields = NULL;
       v->nFields = 0;
       v->xMax = 530;
       v->yMax = 0;

       v->CheckExtraAccel = NULL;
       v->GetAccelTableSize = NULL;
       v->accelTable = NULL;
       v->extraMenuItems = NULL;

       strcpy(dllName, "");

       if (! GetPrivateProfileString("dbinfo", "title", dflt, v->title,
                       256, iniName))
               goto error;
       if (! GetPrivateProfileString("dbinfo", "database", dflt, v->db,
                       50, iniName))
               goto error;
       if (! GetPrivateProfileString("dbinfo", "table", dflt, v->table,
                       50, iniName))
               goto error;
       if (! GetPrivateProfileString("dbinfo", "order", dflt, v->order,
                       256, iniName))
               goto error;

       if (! GetLayout(v, iniName))
               goto error;

       if (GetPrivateProfileString("dbinfo", "dll", dflt, dllName,
                       256, iniName)) {
               if (! LoadDLL(v, dllName)) {
                       MessageBox(NULL, "Error loading DLL", "SQLBase",
                               MB_OK | MB_ICONERROR);
                       return FALSE;
               }
       }

       if (! Login(&loginData))
               return FALSE;

       if (! SQLConnect(0, loginData.host, loginData.user,
                       loginData.passwd))
               return FALSE;

       if (! SQLSelectDB(0, v->db)) {
               SQLDisconnect(0);
               return FALSE;
       }

       return TRUE;

error:
       MessageBox(NULL, "Invalid data file format", "SQLBase",
                       MB_OK | MB_ICONERROR);
       return FALSE;
}