//------------- main.cpp ----------------------------------

#include <windows.h>
#include "cartelle.rh"
#include "cartelle.h"
//------------------------------------------------------------------------------
BOOL CALLBACK WndDlgProc(HWND, UINT, WPARAM, LPARAM);
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define SET_TEXT_(a, b) ::SetDlgItemText(hwnd, a, b)
#define SET_(a, b)      ::SetDlgItemInt(hwnd, a, b, false)
#define SET_CB(a, b)    ::CheckDlgButton(hwnd, a, b ? BST_CHECKED : BST_UNCHECKED)
//------------------------------------------------------------------------------
#define GET_(a, b)  b =  ::GetDlgItemInt(hwnd, a, &dummy, false)
#define GET_CB(a, b) b = ::IsDlgButtonChecked(hwnd, a) == BST_CHECKED
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
class winGest : public Gest_Cart
{
  public:
    winGest(HINSTANCE hInstance);
    void stop() { End = true; }
    void setHWND(HWND hw) { hwnd = hw; }
  private:
    bool End;
    HWND hwnd;
    virtual bool get_break();
    virtual void showCurr(int curr);
};
//------------------------------------------------------------------------------
winGest::winGest(HINSTANCE hInstance) : End(false), hwnd(0)
{
  int _ok = DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_MAIN), 0, (DLGPROC)WndDlgProc, (LPARAM)this);
  switch(_ok) {
    case -1:
      MessageBox(0, "Apertura dialogo", "Errore", MB_OK);
      break;
//    case 0:
//      break;
//    case 1:
//      break;
    }
}
//------------------------------------------------------------------------------
bool winGest::get_break()
{
  if(End)
    return true;
  MSG msg;
  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    if(WM_QUIT == msg.message) {
      End = true;
      break;
      }
    if(!IsDialogMessage(msg.hwnd, &msg)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
    }
  return End;
}
//------------------------------------------------------------------------------
void winGest::showCurr(int curr)
{
  SET_(IDC_STATICTEXT_CURR, curr);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL CALLBACK WndDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  static bool OK = false;
  switch(message) {
    case WM_INITDIALOG:
      do {
        winGest* wGest = reinterpret_cast<winGest*>(lParam);
        wGest->setHWND(hwnd);
        SetWindowLong(hwnd, DWL_USER, lParam);
        SET_TEXT_(IDC_STATICTEXT_CURR, "");
        SET_TEXT_(IDC_EDIT_SRAND, "");

        SET_CB(IDC_RADIOBUTTON_TAB, 1);
        SET_CB(IDC_RADIOBUTTON_NORM, 0);

        SET_TEXT_(IDC_EDIT_NUM, "");
        return TRUE;
        } while(false);
      break;

    case WM_COMMAND:
      switch(LOWORD(wParam) ){
        case IDOK:
          if(OK)
            break;
          OK = true;
          do {
            int dummy;
            int NumOfCart;
            GET_(IDC_EDIT_NUM, NumOfCart);
            if(!NumOfCart) {
              MessageBox(hwnd, "Immettere numero di cartelle desiderate","Attenzione",
                  MB_ICONINFORMATION);
              OK = false;
              break;
              }
            int SRand;
            GET_(IDC_EDIT_SRAND, SRand);
            bool tabbed;
            GET_CB(IDC_RADIOBUTTON_TAB, tabbed);

            DWORD tmp = GetWindowLong(hwnd, DWL_USER);
            winGest* wGest = reinterpret_cast<winGest*>(tmp);

            SET_TEXT_(IDCANCEL, "Stop");
            wGest->run(NumOfCart, tabbed, SRand);
            SET_TEXT_(IDCANCEL, "Esci");

            } while(false);
          OK = false;
          return TRUE;
        case IDCANCEL:
          if(OK) {  // è stato premuto durante l'elaborazione
            int result = MessageBox(hwnd,
              "Chiusura del programma, confermi?", "Attenzione",
                  MB_ICONINFORMATION | MB_YESNOCANCEL);
            switch(result) {
              case IDYES: // ferma elaborazione ed esce dal programma
                OK = false;
                break;
              case IDNO: // ferma solo l'elaborazione
                break;
              case IDCANCEL: // continua con l'elaborazione
                return true;
              }
            }
          do {
            DWORD tmp = GetWindowLong(hwnd, DWL_USER);
            winGest* wGest = reinterpret_cast<winGest*>(tmp);
            wGest->stop();
            } while(false);
          if(!OK)
            EndDialog(hwnd, 0);
          return TRUE;
        }
    }
  return FALSE;
}
//---------------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE /*hPrevInstance*/,
                     LPSTR     /*lpCmdLine*/,
                     int       /*nCmdShow*/)
{
  winGest g(hInstance);
  return 0;
}