#pragma once

#include "xtr/doc/Documents.h"
#include "xtr/io/SerializationManager.hpp"

namespace xtr::doc {

//! OSS document
class OSSDoc final : public XTRDocument, public ccl::types::Observer, public io::MFCSerializable<OSSVersion> {
  DECLARE_DYNCREATE(OSSDoc)

public:
  ccl::oss::PictID selection{};

private:
  std::unique_ptr<ccl::oss::OSSchema> oss{ nullptr };

public:
  ~OSSDoc() override;

private:
  OSSDoc();

public:
  void OnObserve(const ccl::types::Message& change) override;

  [[nodiscard]] const ccl::oss::OSSchema& OSS() const noexcept { return *oss; }
  [[nodiscard]] ccl::oss::OSSchema& OSS() noexcept { return *oss; }

public:
  void Serialize(CArchive& ar) override;
  BOOL CanCloseFrame(CFrameWnd* pFrame) override;
  BOOL OnNewDocument() override;
  BOOL OnOpenDocument(LPCTSTR path) override;
  BOOL OnSaveDocument(LPCTSTR path) override;

private:
  JSON ExtractData() const override;
  bool LoadData(const JSON& object) override;
  bool LegacyLoad(io::CArchiveAdapter& archive) override;

DECLARE_MESSAGE_MAP()
  afx_msg void OnUpdateFileSaveAs(CCmdUI *pCmdUI);

private:
  void SaveAllDocs();
};

} // namespace xtr::doc