#pragma once

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

namespace xtr::dialog {

//! Dialog: Create RSTerm from RSTerm-Function
class Func2Term final : public CDialog {
  struct ValuesHeader;
  struct ArgsHeader;

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

private:
  doc::RSFacade& editor;

  EntityUID activeUID;
  CString activeName;
  CString inputExpr;

  ui::RSEdit inputCtrl{};
  ui::RSEdit expressionCtrl{};
  ui::RSEdit definitionCtrl{};
  ui::RSListCtrl argsCtrl{};
  ui::RSListCtrl valuesCtrl{};
  CComboBox namesCB{};

  BOOL doInline;

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

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 OnChangeFunction();
  afx_msg void OnBnClickedUpdateExp();
  afx_msg void OnBnClickedLeft();
  afx_msg void OnBnClickedApply();
  afx_msg void OnDblClickArgument(NMHDR* pNMHDR, LRESULT* pResult);
  afx_msg void OnSelectArg(NMHDR* pNMHDR, LRESULT* pResult);

private:
  std::optional<EntityUID> AddTerm();

  void UpdateValueList();
  void UpdateUI();
  void Clear();
  void InputArgs();
};

} // namespace xtr::dialog