#include "stdafx.h"

#include "xtr/dialog/WordRefDlg.h"

#include "resource.h"

#include "xtr/Exteor.h"

namespace xtr::dialog {

using ccl::lang::CollaborationRef;
using ccl::lang::Reference;

WordRefDlg::WordRefDlg(RECT position, const CString selection, CWnd* pParent)
  : CDialog(IXTRD_CST_SET_CHANGING_WORD_TEXT, pParent), 
  position{ position }, dependantWord{ selection } {}

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

  SetPositionAndSize();
  if (!dependantWord.IsEmpty()) {
    SelectOffset();
  }
  return TRUE;
}

void WordRefDlg::DoDataExchange(CDataExchange* pDX) {
  ::DDX_Control(pDX, IXTRC_SET_WORDREF_EDIT, textControl);
  ::DDX_Text(pDX, IXTRC_SET_WORDREF_EDIT, dependantWord);
}

BOOL WordRefDlg::PreTranslateMessage(MSG* pMsg) {
  if (pMsg->message == WM_KEYDOWN) {
    if (pMsg->hwnd == textControl.GetSafeHwnd() && pMsg->wParam == VK_RETURN) {
      UpdateData(TRUE);
      if (dependantWord.IsEmpty()) {
        ::AfxMessageBox(IXTRS_INPUT_WORD, MB_ICONEXCLAMATION);
      } else {
        SelectOffset();
      }
      return TRUE;
    }
  }
  return CDialog::PreTranslateMessage(pMsg);
}

Reference WordRefDlg::CreateRef() const {
  return Reference{ CollaborationRef{ mfc::ToSTL(dependantWord), offset } };
}

void WordRefDlg::SetPositionAndSize() {
  SetWindowPos(nullptr, position.left, position.top, position.right - position.left, position.bottom - position.top, SWP_SHOWWINDOW);
  textControl.SetWindowPos(&wndTop, 0, 0, position.right - position.left, position.bottom - position.top, SWP_SHOWWINDOW);
}

void WordRefDlg::SelectOffset() {
  constexpr auto DEFAULT_POSITION_OFFSET = 50;
  const auto oldPosition = position;

  position.right = position.left + DEFAULT_POSITION_OFFSET;
  SetPositionAndSize();

  RefOffsetDlg shiftDlg{ position };
  const auto result = shiftDlg.DoModal();

  position = oldPosition;
  SetPositionAndSize();

  if (result == IDOK) {
    offset = shiftDlg.offset;
    OnOK();
  }
}

RefOffsetDlg::RefOffsetDlg(RECT position, CWnd* pParent)
  : CDialog(IXTRD_CST_SET_CHANGING_WORD_REF, pParent), position{ position } {}

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

  SetPositionAndSize();
  return TRUE;
}

void RefOffsetDlg::SetPositionAndSize() {
  SetWindowPos(nullptr, position.left, position.top, position.right - position.left, position.bottom - position.top, SWP_SHOWWINDOW);
  textControl.SetWindowPos(&wndTop, 0, 0, position.right - position.left, position.bottom - position.top, 0);
}

void RefOffsetDlg::DoDataExchange(CDataExchange* pDX) {
  ::DDX_Control(pDX, IXTRC_WORDREF_CONTROL, textControl);
  ::DDX_Text(pDX, IXTRC_WORDREF_CONTROL, offsetText);
}

BOOL RefOffsetDlg::PreTranslateMessage(MSG* pMsg) {
  if (pMsg->message != WM_KEYDOWN) {
    return CDialog::PreTranslateMessage(pMsg);
  } else if (pMsg->wParam == VK_RETURN) {
    OnOK();
    return TRUE;
  } else if (pMsg->wParam != VK_RIGHT && pMsg->wParam != VK_LEFT){
    return CDialog::PreTranslateMessage(pMsg);
  } else {
    UpdateOffset(pMsg->wParam == VK_LEFT);
    return TRUE;
  }
}

void RefOffsetDlg::UpdateOffset(const BOOL decrease) {
  if (decrease) {
    offset = offset == 1 ? -1 : offset - 1;
  } else {
    offset = offset == -1 ? 1 : offset + 1;
  }
  offsetText.Format(LR"(%d)", offset);
  UpdateData(FALSE);
}

void RefOffsetDlg::OnOK() {
  if (offset == 0) {
    ::AfxMessageBox(IXTRS_PICK_REF, MB_ICONINFORMATION);
  } else {
    CDialog::OnOK();
  }
}

} // namespace xtr::dialog