#include "stdafx.h"

#include "xtr/dialog/BasicDlg.h"

#include "resource.h"

#include "xtr/ui/KeyboardHandler.h"

namespace xtr::dialog {

EditComment::EditComment(const CString comment, const CString title, CWnd* pParent)
  : CDialog(IXTRD_EDIT_COMMENT, pParent), comment{ comment }, title{ title } {}

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

  SetWindowTextW(title);

  textControl.SetAutoURLDetect();
  textControl.InitRE(ui::Keyboard::Layout::RUS);

  // Note: Перезаливаем коммент, чтобы обновить URL'ы
  textControl.SetWindowTextW(CString{});
  textControl.SetWindowTextW(comment); 

  return TRUE;
}

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

  ::DDX_Control(pDX, IXTRC_COMMENT_EC, textControl);
  ::DDX_Text(pDX, IXTRC_COMMENT_EC, comment);
}

EditString::EditString(const CString initData, const CString caption, CWnd* pParent)
  : CDialog(IXTRD_EDIT_FILENAME, pParent), resultStr{ initData }, m_Caption{ caption } {}

BOOL EditString::OnInitDialog() {
  CDialog::OnInitDialog();
  if (!m_Caption.IsEmpty()) {
    SetWindowTextW(m_Caption);
  }
  return TRUE;
}

void EditString::DoDataExchange(CDataExchange* pDX) {
  CDialog::DoDataExchange(pDX);
  ::DDX_Text(pDX, IXTRC_FILENAME, resultStr);
}

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

BEGIN_MESSAGE_MAP(EditHyperlink, CDialog)
  ON_BN_CLICKED(IXTRC_BROWSE, &ThisClass::OnBrowse)
END_MESSAGE_MAP()

#pragma warning( pop )

EditHyperlink::EditHyperlink(const ccl::oss::MediaLink& lnk, CWnd* pParent /*= nullptr*/)
  : CDialog(IXTRD_EDIT_HYPERLINK, pParent), 
  address{ mfc::ToMFC(lnk.address) }, subAddr{ mfc::ToMFC(lnk.subAddr) }{}

BOOL EditHyperlink::OnInitDialog() {
  CDialog::OnInitDialog();
  return TRUE;
}

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

  ::DDX_Text(pDX, IXTRC_ADDRESS, address);
  ::DDX_Text(pDX, IXTRC_SUBADDRESS, subAddr);
}

void EditHyperlink::OnBrowse() {
  UpdateData(TRUE);
  CFileDialog dialog(TRUE, LR"(*.*)", LR"(*.*)", OFN_HIDEREADONLY | OFN_EXPLORER, 
                     mfc::LoadSID(IXTRS_FILTER_DEFAULT));
  dialog.m_ofn.lpstrTitle = mfc::LoadSID(IXTRS_CAPTION_DEFAULT_FILE);
  if (dialog.DoModal() == IDOK) {
    address = dialog.GetPathName();
    UpdateData(FALSE);
  }
}

} // namespace xtr::dialog