#pragma once

// Benano grabber parameters dialog
class BenanoParamDialog : public CDialog
{
public:
  BenanoParamDialog(benano_scansdk::eExposureMode exposureMode, benano_scansdk::U8 brightness, benano_scansdk::U8 cameraGain, bool getNormals, bool getColors) : CDialog(IDD_BENANO_PARAMETERS),
    exposureMode_(exposureMode), brightness_(brightness), cameraGain_(cameraGain), getNormals_(getNormals), getColors_(getColors)
  {}

protected:
  DECLARE_MESSAGE_MAP()

  virtual BOOL OnInitDialog()
  {
    CDialog::OnInitDialog();

    int offset = 0;
    switch (exposureMode_)
    {
    case benano_scansdk::eExposureMode_Shortest: offset = 0; break;
    case benano_scansdk::eExposureMode_Short: offset = 1; break;
    case benano_scansdk::eExposureMode_Middle: offset = 2; break;
    case benano_scansdk::eExposureMode_Long: offset = 3; break;
    }
    ((CComboBox*)GetDlgItem(IDC_BENANO_EXPOSURE))->SetCurSel(offset);

    ((CEdit*)GetDlgItem(IDC_BENANO_BRIGHTNESS))->SetWindowText(std::to_wstring(brightness_).c_str());

    ((CEdit*)GetDlgItem(IDC_BENANO_GAIN))->SetWindowText(std::to_wstring(cameraGain_).c_str());

    ((CButton*)GetDlgItem(IDC_BENANO_NORMALS))->SetCheck(getNormals_);

    ((CButton*)GetDlgItem(IDC_BENANO_COLORS))->SetCheck(getColors_);

    return TRUE;
  }

  void OnOK()
  {
    CString brightnessStr;
    ((CEdit*)GetDlgItem(IDC_BENANO_BRIGHTNESS))->GetWindowText(brightnessStr);
    wchar_t* p;
    const wchar_t* brightnessStrPtr = (const wchar_t *)brightnessStr;
    int brightness = _tcstol(brightnessStrPtr, &p, 10);
    if (brightnessStrPtr == p || brightness < 0 || brightness > 100)
    {
      ::MessageBox(NULL, _T("brightness should be in range [0, 100]"), _T("brightness"), MB_OK | MB_ICONSTOP);
      return;
    }

    CString gainStr;
    ((CEdit*)GetDlgItem(IDC_BENANO_GAIN))->GetWindowText(gainStr);
    const wchar_t* gainStrPtr = (const wchar_t *)gainStr;
    int cameraGain = _tcstol(gainStrPtr, &p, 10);
    if (gainStrPtr == p || cameraGain < 0 || cameraGain > 100)
    {
      ::MessageBox(NULL, _T("camera gain should be in range [0, 100]"), _T("camera gain"), MB_OK | MB_ICONSTOP);
      return;
    }

    brightness_ = brightness;
    cameraGain_ = cameraGain;

    switch (((CComboBox*)GetDlgItem(IDC_BENANO_EXPOSURE))->GetCurSel())
    {
    case 0: exposureMode_ = benano_scansdk::eExposureMode_Shortest; break;
    case 1: exposureMode_ = benano_scansdk::eExposureMode_Short; break;
    case 2: exposureMode_ = benano_scansdk::eExposureMode_Middle; break;
    case 3: exposureMode_ = benano_scansdk::eExposureMode_Long; break;
    }

    getNormals_ = ((CButton*)GetDlgItem(IDC_BENANO_NORMALS))->GetCheck();

    getColors_ = ((CButton*)GetDlgItem(IDC_BENANO_COLORS))->GetCheck();

    CDialog::EndDialog(1);
  }

public:
  benano_scansdk::eExposureMode exposureMode_;
  benano_scansdk::U8 brightness_;
  benano_scansdk::U8 cameraGain_;
  bool getNormals_;
  bool getColors_;
};

BEGIN_MESSAGE_MAP(BenanoParamDialog, CDialog)
END_MESSAGE_MAP()
