#include "stdafx.h"

#include "xtr/ui/SplitterFrame.h"

#include "resource.h"

#include "xtr/Exteor.h"

#include "xtr/ui/UIHelper.h"

#include "xtr/view/TermTreeView.h"
#include "xtr/view/OSSView.h"
#include "xtr/view/PictView.h"

namespace xtr::ui {

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

BEGIN_MESSAGE_MAP(Split2ViewFrame, CMDIChildWndEx)
  ON_WM_GETMINMAXINFO()
  ON_WM_DESTROY()
  ON_WM_MDIACTIVATE()
END_MESSAGE_MAP()

#pragma warning( pop )

Split2ViewFrame::Split2ViewFrame(const uint32_t registryID, const SIZE minSize, const uint32_t minPane2Width, int& splitterTracker)
  : registryName{ mfc::LoadSID(registryID) }, minSize{ minSize }, minPane2Width{ minPane2Width }, secondViewWidth{ splitterTracker } {}

BOOL Split2ViewFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) {
  XTROptions::LoadWindowPlacement(*this, registryName);

  const auto area = xtr::ui::ClientRect(*this);
  auto width1 = area.Width() - secondViewWidth;
  width1 = width1 <= 0 ? area.Width() : width1;
  const auto width2 = area.Width() - width1;

  if (!splitterWnd.CreateStatic(this, 1, 2, WS_CHILD | WS_VISIBLE | WS_BORDER) ||
      !splitterWnd.CreateView(0, 0, pContext->m_pNewViewClass, { width1, area.Height() }, pContext) ||
      !splitterWnd.CreateView(0, 1, SecondViewClass(), { width2, area.Height() }, pContext))  {
    TRACE(L"Failed to create Splitter\n");
    return FALSE;
  } else {
    splitterWnd.SetColumnInfo(0, width1, 0);
    splitterWnd.SetColumnInfo(1, width2, minPane2Width);
    splitterWnd.RecalcLayout();
    PostCreateSetup(splitterWnd.GetPane(0, 0), splitterWnd.GetPane(0, 1));
    return TRUE;
  }
}

void Split2ViewFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI) { // NOLINT(readability-make-member-function-const)
  lpMMI->ptMinTrackSize.x = minSize.cx;
  lpMMI->ptMinTrackSize.y = minSize.cy;
}

void Split2ViewFrame::OnDestroy() {
  if ((GetStyle() & (WS_MAXIMIZE | WS_MINIMIZE)) == 0) {
    XTROptions::SaveWindowPlacement(*this, registryName);
    int32_t pos{};
    int32_t minimal{};
    splitterWnd.GetColumnInfo(1, pos, minimal);
    secondViewWidth = pos;
  }
}

void Split2ViewFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd) {
  ExteorApplication::MainFrame().UpdateUI();
  CMDIChildWndEx::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
}

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

IMPLEMENT_DYNCREATE(RSFormFrame, Split2ViewFrame)

#pragma warning( pop )

RSFormFrame::RSFormFrame()
  : Split2ViewFrame(IXTRO_SECTION_TRSVIEW, { 500, 300 }, 150, XTROptions::RS().splitterPos) {} // NOLINT

CRuntimeClass* RSFormFrame::SecondViewClass() {
  return RUNTIME_CLASS(view::TermTreeView);
}

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

IMPLEMENT_DYNCREATE(RSModelFrame, Split2ViewFrame)

RSModelFrame::RSModelFrame()
  : Split2ViewFrame(IXTRO_SECTION_MODEL, { 500, 350 }, 150, XTROptions::Model().splitterPos) {} // NOLINT

CRuntimeClass* RSModelFrame::SecondViewClass() {
  return RUNTIME_CLASS(view::TermTreeView);
}

#pragma warning( push )
#pragma warning( disable : 26440 26436 26454 )
  
IMPLEMENT_DYNCREATE(OSSFrame, Split2ViewFrame)

#pragma warning( pop )

OSSFrame::OSSFrame()
  : Split2ViewFrame(IXTRO_SECTION_OSS, { 800, 500 }, 350, XTROptions::OSS().splitterPos) {} // NOLINT

CRuntimeClass* OSSFrame::SecondViewClass() {
  return RUNTIME_CLASS(view::PictView);
}

void OSSFrame::PostCreateSetup(CWnd* firstView, CWnd* /*secondView*/) {
  auto* ossView = dynamic_cast<view::OSSView*>(firstView);
  ossView->SetScrollSizes(MM_TEXT, SIZE{}); // Note: need to init scroll before any draw
}

} // namespace xtr::ui