#include "stdafx.h"

#include "xtr/dialog/CstRenameDialog.h"

#include "resource.h"

namespace xtr::dialog {

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

BEGIN_MESSAGE_MAP(CstRename, CDialog)
  ON_CBN_SELCHANGE(IXTRC_NAME_CB, &ThisClass::OnCbnSelchangeNameCb)
END_MESSAGE_MAP()

#pragma warning( pop )

CstRename::CstRename(const ccl::semantic::RSCore& core, const EntityUID target, CWnd* pParent)
  : CDialog(IXTRD_CST_RENAME, pParent), target{ target }, oldName{ mfc::ToMFC(core.GetRS(target).alias) }, core{ core } {}

BOOL CstRename::OnInitDialog() {
  CDialog::OnInitDialog();

  InitCstNameList();
  UpdateData(FALSE);
  return TRUE;
}

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

  ::DDX_Control(pDX, IXTRC_NAME_CB, nameCtrl);

  ::DDX_Text(pDX, IXTRC_NAME, newName);
  ::DDX_CBString(pDX, IXTRC_NAME_CB, oldName);
  ::DDX_Check(pDX, IXTRC_CHANGE_EXPR, doUpdateExpr);
}

void CstRename::OnCbnSelchangeNameCb() {
  UpdateData(TRUE);
  newName = oldName;
  UpdateData(FALSE);
}

void CstRename::InitCstNameList() {
  auto sel = nameCtrl.GetCurSel();
  if (sel == CB_ERR) {
    sel = 0;
  }
  nameCtrl.ResetContent();
  for (const auto uid : core.List()) {
    nameCtrl.AddString(mfc::ToMFC(core.GetRS(uid).alias));
  }
  nameCtrl.SetCurSel(sel);
}

} // namespace xtr::dialog