#pragma once

#include "xtr/doc/Documents.h"

#include "xtr/ui/RSEdit.h"
#include "xtr/ui/TextEdit.h"

namespace xtr::dialog {

class EditConstituentaDlg;

//! Edit constituenta attributes
class CstDataPage : public CPropertyPage {
  EditConstituentaDlg& parent;

  CEdit cstNameCtrl;
  ui::RSEdit expressionCtrl;
  ui::TextEdit definitionCtrl;
  ui::TextEdit termCtrl;
  ui::RSEdit conventionCtrl;
  ui::RSEdit typificationCtrl;
  ui::RSEdit logCtrl;

  CString expression{};
  CString convention{};
  CString cstName{};
  CString typification{};
  CString term{};
  CString definition{};

  CMFCButton allowEditCtrl;

  BOOL isModified{ FALSE };
  BOOL allowEdit{ TRUE };
  CComboBox  namePicker;
  CMFCButton evaluateBtn;
  ui::RSEdit languageIndicator;
  ccl::semantic::ParsingStatus  parseStatus{};
  ccl::rslang::ValueClass  valueStatus{};

public:
  explicit CstDataPage(EditConstituentaDlg& parent);

private:
  void DoDataExchange(CDataExchange* pDX) override;
  void LoadDataFrom(EntityUID target);
  void SaveDataFor(EntityUID activeUID);

  BOOL OnInitDialog() override;
  void InitControls();

  BOOL OnApply() override;
  void OnCancel() override;
  void OnOK() override;
  BOOL PreTranslateMessage(MSG* pMsg) override;

DECLARE_MESSAGE_MAP()
  afx_msg void OnDestroy();
  afx_msg BOOL OnInsertBtn(uint32_t command);
  afx_msg void OnChangeExpression();
  afx_msg void OnKillfocusExpression();
  afx_msg void OnAllowEdit();
  afx_msg void OnChange();
  afx_msg void OnChangeInterpretation();
  afx_msg void OnChangeComment();
  afx_msg void OnChangeName();
  afx_msg void OnChangeTerm();
  afx_msg void OnSetfocusName();
  afx_msg void OnSelchangeName();
  afx_msg void OnPrev();
  afx_msg void OnNext();
  afx_msg void OnHelpInput();
  afx_msg void OnAnalyse();
  afx_msg void OnCalculate();
  afx_msg void OnSimpleSyntax();

private:
  std::tuple<std::string, long> GetAnalyseExpr() const;

  void UpdateStatus(ccl::semantic::ParsingStatus status, ccl::rslang::ValueClass vclass);
  void SetModified(BOOL bChanged = TRUE);
  BOOL IsModified() const noexcept;

  void RunAutocheck();

  void ClearSyntaxWindow();
  void UpdateFormat();
  void InitCstNameList();
  void UpdateStatusBar();

  void Analyse();
};

} // namespace xtr::dialog