#include "stdafx.h"

#include "xtr/dialog/NewDocument.h"

#include "resource.h"

#include "xtr/Exteor.h"

namespace xtr::dialog {

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

BEGIN_MESSAGE_MAP(NewDocument, CDialog)
  ON_BN_CLICKED(IXTRC_OSS, &ThisClass::OnOSS)
  ON_BN_CLICKED(IXTRC_RSFORM, &ThisClass::OnRSForm)
  ON_BN_CLICKED(IXTRC_RSMODEL, &ThisClass::OnRSModel)
END_MESSAGE_MAP()

#pragma warning( pop )

NewDocument::NewDocument() 
  : CDialog(IXTRD_NEW_FILE) {}

void NewDocument::OnOSS() {
  if (ExteorApplication::Docs().NewOSSDoc() != nullptr) {
    OnOK();
  }
  OnCancel();
}

void NewDocument::OnRSModel() {
  if (ExteorApplication::Docs().NewRSModelDoc() != nullptr) {
    OnOK();
  }
  OnCancel();
}

void NewDocument::OnRSForm() {
  if (ExteorApplication::Docs().NewRSFormDoc() != nullptr) {
    OnOK();
  }
  OnCancel();
}

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

BEGIN_MESSAGE_MAP(NewRSForm, CDialog)
  ON_CBN_EDITCHANGE(IXTRC_RS_TITLE, &ThisClass::OnChange)
  ON_CBN_EDITCHANGE(IXTRC_RS_ALIAS, &ThisClass::OnChange)
  ON_CBN_SELCHANGE(IXTRC_RS_TITLE, &ThisClass::OnChange)
  ON_CBN_SELCHANGE(IXTRC_RS_ALIAS, &ThisClass::OnChange)
END_MESSAGE_MAP()

#pragma warning( pop )

NewRSForm::NewRSForm(const CString title, CWnd* pParent)
  : CDialog(IXTRD_NEW_TRSDOC, pParent), title{ title } {}

BOOL NewRSForm::OnInitDialog() {
  CDialog::OnInitDialog();
  titleCB.Init(ui::CstPicker::Mode::TITLE, title);
  aliasCB.Init(ui::CstPicker::Mode::ALIAS, title);
  return TRUE;
}

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

  ::DDX_Control(pDX, IXTRC_RS_ALIAS, aliasCB);
  ::DDX_Control(pDX, IXTRC_RS_TITLE, titleCB);

  ::DDX_CBString(pDX, IXTRC_RS_TITLE, newTitle);
  ::DDX_CBString(pDX, IXTRC_RS_ALIAS, newAlias);

  DDV_Names();
}

void NewRSForm::OnChange() {
  UpdateData();
}

void NewRSForm::DDV_Names() {
  GetDlgItem(LOWORD(GetDefID()))->EnableWindow(titleCB.IsValid() && aliasCB.IsValid());
}

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

BEGIN_MESSAGE_MAP(NewOSS, CDialog)
  ON_BN_CLICKED(IXTRC_BROWSE, &ThisClass::OnBrowse)
  ON_BN_CLICKED(IDOK, &ThisClass::OnAccept)
  ON_EN_CHANGE(IXTRC_OSS_NAME, &ThisClass::OnChangeOssName)
  ON_EN_CHANGE(IXTRC_OSS_PATH, &ThisClass::OnChangeOssPath)
  ON_BN_CLICKED(IDCANCEL, &ThisClass::OnBnClickedCancel)
END_MESSAGE_MAP()

#pragma warning( pop )

NewOSS::NewOSS(CWnd* pParent)
  : CDialog(IXTRD_NEW_OSSDOC, pParent) {
  if (XTROptions::App().pathSaveAs.Right(1) != '\\') {
    XTROptions::App().pathSaveAs += '\\';
  }
  fullPath = XTROptions::App().pathSaveAs;
}

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

  ::DDX_Text(pDX, IXTRC_OSS_NAME, ossName);
  ::DDX_Text(pDX, IXTRC_OSS_PATH, fullPath);
}

void NewOSS::OnBrowse() {
  UpdateData();
  ShowBrowseDlg();
  UpdateData(FALSE);
  pathModified = FALSE;
}

void NewOSS::ShowBrowseDlg() {
  if (::_wchdir(fullPath) != 0) {
    TRACE0("Cannot setup browse default folder");
  }
  IFileDialog* fileDlg{ nullptr };
  if (SUCCEEDED(::CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fileDlg)))) {
    FILEOPENDIALOGOPTIONS dwOptions{};
    if (SUCCEEDED(fileDlg->GetOptions(&dwOptions))) {
      fileDlg->SetOptions(dwOptions | FOS_PICKFOLDERS);
      fileDlg->SetTitle(mfc::LoadSID(IXTRS_BROWSE_TITLE));
    }

    if (SUCCEEDED(fileDlg->Show(nullptr))) {
      IShellItem *selectedItem{ nullptr };
      if (SUCCEEDED(fileDlg->GetResult(&selectedItem))) {
        LPWSTR pName{};
        if (!SUCCEEDED(selectedItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pName))) {
          ::AfxMessageBox(IXTRE_BROWSE_DLG, MB_ICONERROR);
        } else {
          fullPath = pName;
        }
        selectedItem->Release();
      }
    }
    fileDlg->Release();
  }
}

void NewOSS::OnAccept() {
  UpdateData();

  pathModified = TRUE;
  if (_wmkdir(fullPath) == 0) {
    CDialog::OnOK();
  } else {
    CString msg{};
    switch (errno) {
    case EEXIST: CDialog::OnOK();  return;
    case EACCES: msg = mfc::FormatSID(IXTRE_OSS_DIR_EXISTS, fullPath.GetString()); break;
    case ENOENT: msg = mfc::FormatSID(IXTRE_OSS_DIR_NOT_FOUND, fullPath.GetString()); break;
    default: msg = mfc::FormatSID(IXTRE_FOLDER_CREATE_FAIL, fullPath.GetString());  break;
    }
    ::AfxMessageBox(msg, MB_ICONERROR);
  }
}

void NewOSS::OnChangeOssName() {
  UpdateData();
  GetDlgItem(IDOK)->EnableWindow(!ossName.IsEmpty());
  if (pathModified) {
    fullPath = XTROptions::App().pathSaveAs + ossName;
    UpdateData(FALSE);
  }
}

void NewOSS::OnChangeOssPath() {
  pathModified = FALSE;
}

void NewOSS::OnBnClickedCancel() {
  OnCancel();
  pathModified = TRUE;
}

} // namespace xtr::dialog