#include "stdafx.h"

#include "xtr/dialog/OptionsDialog.h"

#include "resource.h"

#include "xtr/ExteorOptions.h"
#include "xtr/ui/HelpHandler.h"

namespace xtr::dialog {

ExteorOptionsSheet::ExteorOptionsSheet(CWnd* pWndParent)
  : CPropertySheet(IXTRS_APP_PROPERTIES, pWndParent) {
  AddPage(&m_App);
  AddPage(&m_OSS);
  AddPage(&parsing);
  AddPage(&m_RSModel);
}

BOOL ExteorOptionsSheet::PreTranslateMessage(MSG* pMsg) {
  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F1) {
    ui::HelpHandler::RunHelp(ui::HelpID::options);
    return TRUE;
  } else {
    return CPropertySheet::PreTranslateMessage(pMsg);
  }
}

#pragma warning( push )
#pragma warning( disable : 26440 26436 26454 )

BEGIN_MESSAGE_MAP(GeneralPage, CDialog)
  ON_BN_CLICKED(IXTRC_FILE_VERSION_PROMT, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_START_PAGE, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_LOAD_PYTHON, &ThisClass::OnChange)
  ON_EN_CHANGE(IXTRC_PYTHONPATH, &ThisClass::OnChange)
END_MESSAGE_MAP()

#pragma warning( pop )

GeneralPage::GeneralPage()
  : CPropertyPage(IXTRD_OPTION_APPLICATION) {
  promptVersion =  XTROptions::App().promptVersion;
  showStartPage =  XTROptions::App().showStartPage;
  disableAutoKeyboard = XTROptions::App().disableAutoKeyboard;
  enablePython =  XTROptions::App().enablePython;
  pythonPath = XTROptions::App().pythonPath;
}

void GeneralPage::DoDataExchange(CDataExchange* pDX) {
  CDialog::DoDataExchange(pDX);

  ::DDX_Check(pDX, IXTRC_FILE_VERSION_PROMT, promptVersion);
  ::DDX_Check(pDX, IXTRC_START_PAGE, showStartPage);
  ::DDX_Check(pDX, IXTRC_LOAD_PYTHON, enablePython);
  ::DDX_Check(pDX, IXTRC_DISABLE_AUTOLANG, disableAutoKeyboard);
  ::DDX_Text(pDX, IXTRC_PYTHONPATH, pythonPath);
}

void GeneralPage::OnOK() {
  XTROptions::App().promptVersion = promptVersion;
  XTROptions::App().showStartPage = showStartPage;
  XTROptions::App().enablePython = enablePython;
  XTROptions::App().disableAutoKeyboard = disableAutoKeyboard;
  XTROptions::App().pythonPath = pythonPath;

  CPropertyPage::OnOK();
}

#pragma warning( push )
#pragma warning( disable : 26440 26436 26454 )

BEGIN_MESSAGE_MAP(OSSPage, CDialog)
  ON_BN_CLICKED(IXTRC_OSS_RUN_AFTER_CLK_OK, &ThisClass::OnChange)
  ON_EN_CHANGE(IXTRC_ZOOM_INT, &ThisClass::OnChange)
  ON_EN_CHANGE(IXTRC_OSS_GRID_ENABLED, &ThisClass::OnChange)
END_MESSAGE_MAP()

#pragma warning( pop )

OSSPage::OSSPage() : CPropertyPage(IXTRD_OPTION_OSS) {
  m_RunOperation = XTROptions::OSS().runOpAfterEdit;
  zoom = XTROptions::OSS().zoom;
  m_EnableGrid = XTROptions::OSS().showGrid;
}

void OSSPage::DoDataExchange(CDataExchange* pDX) {
  CDialog::DoDataExchange(pDX);

  ::DDX_Check(pDX, IXTRC_OSS_RUN_AFTER_CLK_OK, m_RunOperation);
  ::DDX_Check(pDX, IXTRC_OSS_GRID_ENABLED, m_EnableGrid);

  ::DDX_Text(pDX, IXTRC_ZOOM_INT, zoom);

  ::DDV_MinMaxUInt(pDX, zoom, ossOptions::zoomMin, ossOptions::zoomMax);
}

BOOL OSSPage::OnInitDialog() {
  CPropertyPage::OnInitDialog();
  return TRUE;
}

void OSSPage::OnOK() {
  XTROptions::OSS().runOpAfterEdit = m_RunOperation;
  XTROptions::OSS().zoom = zoom;
  XTROptions::OSS().showGrid = m_EnableGrid;

  CPropertyPage::OnOK();
}

#pragma warning( push )
#pragma warning( disable : 26440 26436 26454 )

BEGIN_MESSAGE_MAP(ParsingPage, CDialog)
  ON_BN_CLICKED(IXTRC_SA_AUTOCHECK, &ThisClass::OnChange)

  ON_BN_CLICKED(IXTRC_INDEXCST0, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXCST1, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXCST2, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXLOCAL0, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXLOCAL1, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXLOCAL2, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXPR0, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXPR1, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_INDEXPR2, &ThisClass::OnChange)

  ON_BN_CLICKED(IXTRC_SA_COLOR_PAR, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_SA_AST, &ThisClass::OnChange)
  ON_BN_CLICKED(IXTRC_SA_RSNAVIGATOR, &ThisClass::OnChange)
END_MESSAGE_MAP()

#pragma warning( pop )

ParsingPage::ParsingPage() : CPropertyPage(IXTRD_OPTION_ANALYZER) {
  autoCheck =  XTROptions::ParseRSL().autoCheck;
  colorBrackets =  XTROptions::ParseRSL().colorBrackets;
  showAST =    XTROptions::ParseRSL().showAST;
  enableNavigator =  XTROptions::ParseRSL().enableNavigator;
  m_IndexCst =  static_cast<int32_t>(XTROptions::ParseRSL().indexing.global);
  m_IndexLocal = static_cast<int32_t>(XTROptions::ParseRSL().indexing.local);
  m_IndexPr = static_cast<int32_t>(XTROptions::ParseRSL().indexing.operators);
}

void ParsingPage::DoDataExchange(CDataExchange* pDX) {
  CDialog::DoDataExchange(pDX);

  ::DDX_Check(pDX, IXTRC_SA_AUTOCHECK, autoCheck);
  ::DDX_Check(pDX, IXTRC_SA_COLOR_PAR, colorBrackets);
  ::DDX_Check(pDX, IXTRC_SA_AST, showAST);
  ::DDX_Check(pDX, IXTRC_SA_RSNAVIGATOR, enableNavigator);

  ::DDX_Radio(pDX, IXTRC_INDEXCST0, m_IndexCst);
  ::DDX_Radio(pDX, IXTRC_INDEXLOCAL0, m_IndexLocal);
  ::DDX_Radio(pDX, IXTRC_INDEXPR0, m_IndexPr);
}

void ParsingPage::OnOK() {
  XTROptions::ParseRSL().autoCheck =  autoCheck;
  XTROptions::ParseRSL().colorBrackets =  colorBrackets;
  XTROptions::ParseRSL().showAST =  showAST;
  XTROptions::ParseRSL().enableNavigator = enableNavigator;
  XTROptions::ParseRSL().indexing.global =  static_cast<index::Digits>(m_IndexCst);
  XTROptions::ParseRSL().indexing.local = static_cast<index::Digits>(m_IndexLocal);
  XTROptions::ParseRSL().indexing.operators = static_cast<index::Digits>(m_IndexPr);

  CPropertyPage::OnOK();
}

#pragma warning( push )
#pragma warning( disable : 26440 26436 26454 )

BEGIN_MESSAGE_MAP(ModelPage, CDialog)
  ON_BN_CLICKED(IXTRC_DATAMODE, &ThisClass::OnChange)
END_MESSAGE_MAP()

#pragma warning( pop )

ModelPage::ModelPage()
  : CPropertyPage(IXTRD_OPTION_MODEL) {
  m_DataMode = XTROptions::Model().m_DataMode;
}

void ModelPage::DoDataExchange(CDataExchange* pDX) {
  CDialog::DoDataExchange(pDX);
  ::DDX_Check(pDX, IXTRC_DATAMODE, m_DataMode);
}

void ModelPage::OnOK() {
  XTROptions::Model().m_DataMode =  m_DataMode;
  CPropertyPage::OnOK();
}

} // namespace xtr::dialog