#pragma once

#include "xtr/core/rslIndex.hpp"

namespace xtr::io {

//! MFC serialization adapter for CCL
class CArchiveAdapter {
public:
  CArchiveAdapter(CArchive& ar) noexcept 
    : archive{ ar } {}

private:
  CArchive& archive;

public:
  CArchive& get() noexcept { return archive; }
  bool IsStoring() const {
    return archive.IsStoring();
  }

  template<typename Object>
  CArchiveAdapter& operator>>(Object& obj) {
    archive >> obj;
    return *this;
  }

  template<>
  CArchiveAdapter& operator>>(std::string& obj) {
    CString str{};
    archive >> str;
    index::FixLegacy(str);
    obj = mfc::ToSTL(str);
    return *this;
  }

  CArchiveAdapter& operator>>(std::u8string& obj) {
    CString str{};
    archive >> str;
    index::FixLegacy(str);
    obj = mfc::ToUTF8(str);
    return *this;
  }

  template<typename Object>
  void Skip() {
    Object discard{};
    *this >> discard;
  }

  template<typename Object>
  void SkipN(uint32_t count) {
    while (count--) {
      Skip<Object>();
    }
  }

  template<typename Object>
  Object Load() {
    Object obj{};
    *this >> obj;
    return obj;
  }
};

} // namespace xtr::io