#pragma warning (disable:4996)
#pragma comment(lib,"comctl32")
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>

#include "resource.h"
#include "scc.h"
#include "scc_cfg.h"
#include "scc_gvar.h"
#include "scc_gui.h"
#include "scc_thr.h"

LRESULT CALLBACK main_dlgp(HWND hDlg, DWORD msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CLOSE:
            gui_getparm(0);
            DestroyWindow(hMain);
            return 0;
        case WM_QUIT:
            gui_getparm(0);
            DestroyWindow(hMain);
            break;
        case WM_INITDIALOG:
            hMain=hDlg;
            CenterWindow(hMain,GetDesktopWindow());
            initrc();
            return TRUE;

        case WM_COMMAND:
            if ((LOWORD(wParam) == IDC_SCTPSEL) && (HIWORD(wParam) == CBN_SELCHANGE))
            {
                // card type changed
                gui_disen(TRUE);
                if (gui_getparm(IFLG_NOUID | IFLG_NODATA)) gui_setatqasak(scc_dsc[scc_set.tp].atqa, scc_dsc[scc_set.tp].sak, TRUE);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCTPSRCH) && (HIWORD(wParam) == BN_CLICKED))
            {
                // type search
                if ((!gui_getparm(IFLG_NOUID | IFLG_NODATA)) ||
                    (!thread_launcher((LPTHREAD_START_ROUTINE)t_typesrch))) MessageBeep(MB_ICONERROR);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCSETUID) && (HIWORD(wParam) == BN_CLICKED))
            {
                // set new UID for RW-card
                gui_getparm(IFLG_NODATA);
                if (!thread_launcher((LPTHREAD_START_ROUTINE)t_setuid)) MessageBeep(MB_ICONERROR);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCRW) && (HIWORD(wParam) == BN_CLICKED))
            {
                // RW-mode changed
                SendMessage(GetDlgItem(hMain, IDC_SCRW), BM_SETCHECK,
                    (WPARAM)((SendMessage(GetDlgItem(hMain, IDC_SCRW), BM_GETCHECK, 0, 0) == BST_CHECKED) ? BST_UNCHECKED : BST_CHECKED), 0);
                gui_disen(TRUE);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCAKSRCH) && (HIWORD(wParam) == BN_CLICKED))
            {
                // ak search
                if ((!gui_getparm(IFLG_NOUID | IFLG_NODATA)) ||
                    (!thread_launcher((LPTHREAD_START_ROUTINE)t_aksrch))) MessageBeep(MB_ICONERROR);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCREAD) && (HIWORD(wParam) == BN_CLICKED))
            {
                // data read
                if ((!gui_getparm(IFLG_NOUID | IFLG_NODATA)) ||
                    (!thread_launcher((LPTHREAD_START_ROUTINE)t_read))) MessageBeep(MB_ICONERROR);
                break;
            }
            else if ((LOWORD(wParam) == IDC_SCWRITE) && (HIWORD(wParam) == BN_CLICKED))
            {
                // data write
                if ((!gui_getparm(IFLG_NOUID)) ||
                    (!thread_launcher((LPTHREAD_START_ROUTINE)t_write))) MessageBeep(MB_ICONERROR);
                break;
            }
            break;
    default:
        goto defp;
    }
    return TRUE;

defp:
    return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    DWORD                   defCBR[10] = { 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200 };
    HANDLE                  hMutex;
    LPTSTR                  cfgpath;
    char                    aCmd[32];
    DWORD                   ctmp, cnt;

    if (hPrevInstance) goto main_exit;
    hInst = hInstance;
    hMutex = CreateMutex(NULL, TRUE, TEXT(SCC_RCCLASS));
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        CloseHandle(hMutex);
        goto main_exit;
    }
    // connect to library
    InitCommonControls();
    // parse command line
    memset(aCmd, 0, sizeof(aCmd));
    for (cnt = 0; cnt < strlen(lpszCmdParam); cnt++)
        if (lpszCmdParam[cnt] != 0x20) break;
    if ((ctmp = strlen(&lpszCmdParam[cnt])) > 20) ctmp = 20;
    if (ctmp > 8)
    {
        memcpy(aCmd, &lpszCmdParam[cnt], ctmp);
        if (!strnicmp(aCmd, "COM", 3))
        {
            if (isdigit(aCmd[3]))
            {
                sscanf(&aCmd[3], "%u", &cport);
                if ((cport) && (cport < 256))
                {
                    for (cnt = 3; cnt < strlen(aCmd); cnt++)
                        if (!isdigit(aCmd[cnt])) break;
                    for (; cnt < strlen(aCmd); cnt++)
                        if (aCmd[cnt] != 0x20) break;
                    if (isdigit(aCmd[cnt]))
                    {
                        ctmp = 0;
                        sscanf(&aCmd[cnt], "%u", &ctmp);
                        for (cnt = 0; cnt < sizeof(defCBR) / sizeof(defCBR[0]); cnt++)
                        {
                            if (ctmp == defCBR[cnt])
                            {
                                cbr = ctmp;
                                break;
                            }
                        }
                        ctmp = 0;
                    }
                }
            }
        }
    }
    if ((ctmp) && (!cport) && (!cbr))
    {
        MessageBox(NULL, TEXT("Usage: scc.exe COMnn <baudrate>\r\nExample: scc.exe COM12 9600"), TEXT("Info"), MB_OK);
        goto main_exit;
    }

    // load cfg-file
    lstrcat(cfgpath = GetProgramDir(), CFG_FNAME);
    load_cfg(cfgpath, &scc_set);
    // call dialog box
    DialogBox(hInst,MAKEINTRESOURCE(IDD_MAIN),NULL,(DLGPROC)main_dlgp);
    // save cfg
    save_cfg(cfgpath, &scc_set);        // сохранение конфигурации

main_exit:
    return 0;
}
