#pragma once

#include "xtr/ui/RSEdit.h"
#include "xtr/ui/RSListCtrl.h"
#include "xtr/doc/RSFacade.h"

namespace xtr::dialog {

//! Dialog: Create RSTerm-Function from RSTerm
class Term2Function final : public CDialog {
  struct Header;

public:
  CString expression{};
  CString textRef{};
  VectorOfEntities addedCst{};

private:
  doc::RSFacade& editor;

  ui::RSEdit inputCtrl{};
  ui::RSEdit expressionCtrl{};
  ui::RSEdit definitionCtrl{};

  ui::RSListCtrl globalsCtrl{};
  ui::RSListCtrl argsCtrl{};

  CComboBox cstPicker{};

  CString cstName{};
  CString inputExpr{};

public:
  Term2Function(doc::RSFacade& editor, EntityUID startID, CWnd* pParent = nullptr);

private:
  void DoDataExchange(CDataExchange* pDX) override;
  BOOL OnInitDialog() override;
  void InitControls();
  void InitHeaders();
  void InitCstNameList();
  void OnOK() override;
  BOOL PreTranslateMessage(MSG* pMsg) override;

DECLARE_MESSAGE_MAP()
  afx_msg void OnSelchangeTermCb();
  afx_msg void OnBnClickedUpdateExp();
  afx_msg void OnBnClickedRight();
  afx_msg void OnBnClickedLeft();
  afx_msg void OnBnClickedApply();
  afx_msg void OnDblClickGlobal(NMHDR* pNMHDR, LRESULT* pResult);
  afx_msg void OnDblClickArg(NMHDR* pNMHDR, LRESULT* pResult);

private:
  void UpdateUI();
  void UpdateArgs();
  void InputGlobals();
  std::optional<EntityUID> AddFunction();
};

} // namespace xtr::dialog