#include "stdafx.h"

#include "xtr/dialog/StartPage.h"

#include "resource.h"

#include "xtr/Exteor.h"

#include "xtr/ui/HelpHandler.h"
#include "xtr/ui/UIHelper.h"

namespace xtr::dialog {

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

BEGIN_MESSAGE_MAP(ExtStartPage, CDialog)
  ON_BN_CLICKED(IXTRC_NEW_DOCUMENT, &ThisClass::OnNewDoc)
  ON_BN_CLICKED(IXTRC_OPEN_DOC, &ThisClass::OnOpenDoc)
  ON_BN_CLICKED(IXTRC_START_HELP, &ThisClass::OnOpenHelp)
  ON_BN_CLICKED(IXTRC_SAMPLES, &ThisClass::OnSamples)
  ON_BN_CLICKED(IXTRC_MEMO, &ThisClass::OnMemo)

  ON_BN_CLICKED(IXTRC_DONT_PROMT, &ThisClass::OnPromt)
END_MESSAGE_MAP()

#pragma warning( pop )

ExtStartPage::~ExtStartPage() {
  ::DestroyIcon(xtrIcon);
}

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

  InitTitle();
  LoadExteorIcon();
  InitControls();

  UpdateWindow();
  return TRUE;
}

void ExtStartPage::LoadExteorIcon() {
  xtrIcon = static_cast<HICON>(LoadImageW(AfxGetResourceHandle(), 
                             MAKEINTRESOURCE(IXTRAD_MENU_MAINFRAME),
                             IMAGE_ICON, 0, 0, 0));
  SetIcon(xtrIcon, TRUE);
}

void ExtStartPage::InitControls() {
  releaseLog.SetFont(&XTROptions::App().fontTooltip);
  releaseLog.SetWindowTextW(LoadLog());
  promtBtn.SetCheck(XTROptions::App().showStartPage);
}

void ExtStartPage::InitTitle() {
  auto title = ui::WindowText(*this);
  title += g_Exteor.version.id;
  SetWindowTextW(title);
}

CString ExtStartPage::LoadLog() {
  const auto fileName = g_Exteor.GetModuleFolder() + mfc::LoadSID(IXTRS_FILE_UPDATELOG);
  FILE* fStream{};
  if (_wfopen_s(&fStream, fileName, LR"(r, ccs=UTF-8)") != 0) {
    return mfc::LoadSID(IXTRE_LOG_NOT_FOUND);
  } else {
    CStdioFile logFile{ fStream };
    CString line{};
    CString text = line;

    logFile.SeekToBegin();
    while (logFile.ReadString(line)) {
      text += line;
      text += L"\r\n";
    }
    logFile.Close();
    return text;
  }
}

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

  ::DDX_Control(pDX, IXTRC_NEW_DOCUMENT, newDocBtn);
  ::DDX_Control(pDX, IXTRC_OPEN_DOC, openDocBtn);
  ::DDX_Control(pDX, IXTRC_START_HELP, helpBtn);
  ::DDX_Control(pDX, IXTRC_CHANGE_LOG, releaseLog);
  ::DDX_Control(pDX, IXTRC_DONT_PROMT, promtBtn);
  ::DDX_Control(pDX, IXTRC_SAMPLES, examplesBtn);
}

void ExtStartPage::OnNewDoc() { // NOLINT(readability-convert-member-functions-to-static)
  EndDialog(IDOK);
  g_Exteor.OnFileNew();
}

void ExtStartPage::OnOpenDoc() { // NOLINT(readability-convert-member-functions-to-static)
  EndDialog(IDOK);
  g_Exteor.OnFileOpen();
}

void ExtStartPage::OnOpenHelp() { // NOLINT(readability-convert-member-functions-to-static)
  ui::HelpHandler::RunHelp();
}

void ExtStartPage::OnMemo() { // NOLINT(readability-convert-member-functions-to-static)
  ::ShellExecuteW(nullptr, L"open", g_Exteor.GetModuleFolder() + mfc::LoadSID(IXTRS_FILE_FAQ), 
                nullptr, nullptr, SW_SHOWNORMAL);
}

void ExtStartPage::OnSamples() { // NOLINT(readability-convert-member-functions-to-static)
  EndDialog(IDOK);
  CString path = g_Exteor.GetModuleFolder() + mfc::LoadSID(IXTRS_FOLDER_SAMPLES);
  ::ShellExecuteW(nullptr, L"open", path, nullptr, nullptr, SW_SHOWNORMAL);
}

void ExtStartPage::OnPromt() { // NOLINT(readability-convert-member-functions-to-static)
  XTROptions::App().showStartPage = promtBtn.GetCheck();
}

} // namespace xtr::dialog