#pragma once

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

namespace xtr::doc {

class RSModelDoc;

//! RSForm document
class RSFormDoc final 
  : public ccl::src::Source, public SelectableDoc, public io::MFCSerializable<TRSVersion>, public ccl::types::Observer {
  DECLARE_DYNCREATE(RSFormDoc)

private:
  ccl::semantic::RSForm schema{};

public:
  ~RSFormDoc() override;

public:
  RSFormDoc();

public:
  ccl::semantic::RSForm& Schema() noexcept { return schema; }
  const ccl::semantic::RSForm& Schema() const noexcept { return schema; }

  [[nodiscard]] ccl::src::SrcType Type() const noexcept override { return ccl::src::SrcType::rsDoc; }
  [[nodiscard]] ccl::change::Hash CoreHash() const override { return schema.CoreHash(); }
  [[nodiscard]] ccl::change::Hash FullHash() const override { return schema.FullHash(); }

  bool WriteData(ccl::meta::UniqueCPPtr<ccl::src::DataStream> data) override;
  [[nodiscard]] const ccl::src::DataStream* ReadData() const noexcept override { return &schema; }
  [[nodiscard]] ccl::src::DataStream* AccessData() override;

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

public:
  void Serialize(CArchive& ar) override;
  BOOL OnNewDocument() override;
  BOOL OnOpenDocument(LPCTSTR path) override;
  BOOL OnSaveDocument(LPCTSTR path) override;
  void OnCloseDocument() override;

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

} // namespace xtr::doc