#pragma once

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

namespace xtr::view {

//! View: RSForm table
class RSFormView : public CFormView {
  DECLARE_DYNCREATE(RSFormView)

  doc::RSFacade editor{};
  ccl::semantic::RSForm* schema{ nullptr };

  CString alias{};
  CString title{};

  ui::RSEdit titleCtrl{};
  ui::RSEdit aliasCtrl{};
  CRichEditCtrl rsformStatusCtrl{};
  ui::RSEdit searchCtrl{};
  CToolTipCtrl  tooltips{};
  ui::RSTableCtrl cstTable{};

private:
  RSFormView();

private:
  void DoDataExchange(CDataExchange* pDX) override;
  void OnInitialUpdate() override;
  void InitControls();
  void InitTooltips();

private:
  BOOL PreTranslateMessage(MSG* pMsg) override;
  BOOL OnCmdMsg(uint32_t nID, int32_t nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) override;
  void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) override;

DECLARE_MESSAGE_MAP()
  afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
  afx_msg void OnChange();
  afx_msg BOOL OnCopy(uint32_t msgID);
  afx_msg BOOL OnPaste(uint32_t msgID);
  afx_msg void OnEditComment();
  afx_msg void OnUpdateCommandUI(CCmdUI *pCmdUI);
  afx_msg BOOL OnCommandRun(uint32_t command);
  afx_msg BOOL OnInsertCst(uint32_t command);
  afx_msg BOOL OnOperation(uint32_t command);
  afx_msg void OpenHelp();
  afx_msg void OnOpMaxPart();
  afx_msg void OnOpExtractBasis();
  afx_msg void OnUpdateStatus();

private:
  BOOL IsCommandActive(uint32_t command) const;
  BOOL IsContextActive(uint32_t command) const;

  void InsertNew(ccl::semantic::CstType cstType, int32_t nWhere = -1);
  void InsertFree();

  const SetOfEntities& Selection() const noexcept;

  void UpdateStatusBar();

  void RunRelation();
  void OnSearch();
};

} // namespace xtr::view