#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <wsqllib.h>

#include "..\sqlbase.h"
#include "..\dbinit.h"
#include "logbook.h"

HINSTANCE hInst;


MENUITEM extraMenuItems[] = {
       {"Insert Start\tCtrl+S", ID_INSERT_START},
       {"Insert End\tCtrl+E", ID_INSERT_END},
       {"Lookup Country\tCtrl+P", ID_LOOKUP_COUNTRY},
       {"Check Country\tCtrl+K", ID_CHECK_COUNTRY},
       {"Show Country Data\tCtrl+R", ID_SHOW_COUNTRY_DATA},
       {NULL, 0}
};


ACCEL accelTable[] = {
       {FVIRTKEY | FCONTROL, (WORD)'I', ID_INIT_FIELDS},
       {FVIRTKEY | FCONTROL, (WORD)'S', ID_INSERT_START},
       {FVIRTKEY | FCONTROL, (WORD)'E', ID_INSERT_END},
       {FVIRTKEY | FCONTROL, (WORD)'P', ID_LOOKUP_COUNTRY},
       {FVIRTKEY | FCONTROL, (WORD)'K', ID_CHECK_COUNTRY},
       {FVIRTKEY | FCONTROL, (WORD)'R', ID_SHOW_COUNTRY_DATA},
       {FVIRTKEY | FCONTROL, VK_RETURN, ID_CTRL_RETURN}
};


int GetAccelTableSize(void)
{
       return sizeof(accelTable);
}


BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
       static HWND listBox;
       int i, n;
       static DLG_DATA dd, *pdd;

       switch (msg) {
               case WM_INITDIALOG:
                       pdd = (DLG_DATA*)lParam;
                       dd = *pdd;
                       listBox = GetDlgItem(hDlg, ID_LISTBOX);
                       SetWindowText(hDlg, dd.lbTitle);
                       for (i = 0; dd.lbData[i]; i++) {
                               SendMessage(listBox, LB_ADDSTRING, (WPARAM)0,
                                               (LPARAM)dd.lbData[i]);
                       }
                       SetFocus(listBox);
                       return FALSE;

               case WM_COMMAND :
                       switch (LOWORD (wParam)) {
                               case IDOK:
                                       n = SendMessage(listBox, LB_GETCURSEL, (WPARAM)0,
                                               (LPARAM)0);
                                       EndDialog(hDlg, n + 1);
                                       return TRUE;
                               case IDCANCEL:
                                       EndDialog(hDlg, 0);
                                       return TRUE ;
                       }
                       break;
       }
       return FALSE;
}


void InitFields(VARS *v)
{
       int i;

       for (i = 0; i < v->nFields; i++)
               SetWindowText(v->hwndEdit[i], "");

       SetWindowText(v->hwndEdit[8], "CW");
       SetWindowText(v->hwndEdit[9], "150");

       SetWindowText(v->hwndEdit[12], "No");
       SetWindowText(v->hwndEdit[13], "No");
}


void InsertStart(VARS *v)
{
       time_t t;
       struct tm *gmt;
       char s[80];

       t = time(NULL);
       gmt = gmtime(&t);

       strftime(s, 80, "%Y-%m-%d", gmt);
       SetWindowText(v->hwndEdit[0], s);

       strftime(s, 80, "%H%M", gmt);
       SetWindowText(v->hwndEdit[1], s);
}


void InsertEnd(VARS *v)
{
       time_t t;
       struct tm *gmt;
       char s[80];

       t = time(NULL);
       gmt = gmtime(&t);

       strftime(s, 80, "%H%M", gmt);
       SetWindowText(v->hwndEdit[2], s);
}


void LookupCountry(VARS *v)
{
       DLG_DATA dd;
       char *countries[20];
       char buf[4096];
       char callsign[50];
       MYSQL_ROW row;
       int i, n, end;

       memset(callsign, 0, 50);
       GetWindowText(v->hwndEdit[3], callsign, 50);
       if (! (end = strlen(callsign)))
               return;

       while (1) {
               sprintf(buf, "SELECT * FROM prefix WHERE prefix LIKE '%%:%s:%%'",
                               callsign);

               if (! SQLQuery(0, buf))
                       return;

               for (i = 0; row = SQLFetchRow(0); i++) {
                       countries[i] = (char*)malloc(strlen(row[0]) + 1);
                       strcpy(countries[i], row[0]);
               }
               if (i)
                       break;
               else
                       SQLEndQuery(0);

               *(callsign + (--end)) = '\0';
               if (! (*callsign)) {
                       MessageBox(NULL, "No match found", "SQLBase",
                               MB_OK | MB_ICONINFORMATION);
                       return;
               }
       }

       SQLEndQuery(0);
       countries[i] = NULL;

       n = 0;
       if (i > 1) {
               dd.lbTitle = "Select Country";
               dd.lbData = countries;
               if (! (n = DialogBoxParam(hInst, "ListBox", v->hwndMain,
                               DlgProc, (LPARAM)&dd)))
                       goto end;
               n--;
       }

       SetWindowText(v->hwndEdit[6], countries[n]);
end:
       for (i = 0; countries[i]; i++)
               free(countries[i]);
}


void CheckCountry(VARS *v)
{
       MYSQL_ROW row;
       char country[50];
       char buf[4096];
       int i;
       BOOL found = FALSE, confirmed = FALSE;

       memset(country, 0, 50);
       GetWindowText(v->hwndEdit[6], country, 50);
       if (! strlen(country))
               return;

       sprintf(buf, "SELECT * FROM %s WHERE country='%s'",
                       v->table, country);

       if (! SQLQuery(0, buf))
               return;

       for (i = 0; row = SQLFetchRow(0); i++) {
               found = TRUE;
               if (strcmp(row[13], "Yes")) {
                       confirmed = TRUE;
                       break;
               }
       }

       SQLEndQuery(0);

       if (! found) {
               sprintf(buf, "'%s' is a new country", country);
   } else if (! confirmed) {
               sprintf(buf, "'%s' worked but not confirmed", country);
   } else {
               sprintf(buf, "'%s' worked and confirmed", country);
   }

       MessageBox(v->hwndMain, buf, "SQLBase",
                       MB_OK | MB_ICONINFORMATION);
}


void ShowCountryData(VARS *v)
{


}


void CheckCtrlReturn(VARS *v)
{
       DLG_DATA dd;
       char *modes[] = {"CW", "LSB", "USB", "AM", "FM", "RY",
               "AMT", "PTR", "PKT", "STV", NULL};
       int n;

       if (GetFocus() == v->hwndEdit[8]) {
               dd.lbTitle = "Select Mode";
               dd.lbData = modes;
               if ((n = DialogBoxParam(hInst, "ListBox", v->hwndMain,
                               DlgProc, (LPARAM)&dd)))
                       SetWindowText(v->hwndEdit[8], modes[n - 1]);
       }
}


void CheckExtraAccel(WORD id, VARS *v)
{
       switch (id ) {
               case ID_INIT_FIELDS:
                       InitFields(v);
                       break;

               case ID_INSERT_START:
                       InsertStart(v);
                       break;

               case ID_INSERT_END:
                       InsertEnd(v);
                       break;

               case ID_LOOKUP_COUNTRY:
                       LookupCountry(v);
                       break;

               case ID_CHECK_COUNTRY:
                       CheckCountry(v);
                       break;

               case ID_SHOW_COUNTRY_DATA:
                       ShowCountryData(v);
                       break;

               case ID_CTRL_RETURN:
                       CheckCtrlReturn(v);
                       break;
       }
}


BOOL WINAPI DllEntryPoint(HINSTANCE hInstance, DWORD fdwReason,
               PVOID pvReserved)
{
       hInst = hInstance;
       return TRUE;
}