/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */
#ifndef OSGEARTH_DRIVER_KML_KMZ_ARCHIVE
#define OSGEARTH_DRIVER_KML_KMZ_ARCHIVE 1

#include <osgDB/Archive>
#include <osgEarth/URI>

using namespace osgEarth;


struct KMZArchive : public osgDB::Archive
{
    KMZArchive(const URI& archiveURI, const Options* options);

    virtual ~KMZArchive();

    /** closes the archive */
    void close();

    /** Get the file name which represents the archived file.*/
    std::string getArchiveFileName() const;

    /** Get the file name which represents the master file recorded in the Archive.*/
    std::string getMasterFileName() const;

    /** return true if file exists in archive.*/
    bool fileExists(const std::string& filename) const;

    /** return type of file. */
    osgDB::FileType getFileType(const std::string& filename) const;

    typedef osgDB::DirectoryContents FileNameList;

    /** Get the full list of file names available in the archive.*/
    bool getFileNames(FileNameList& fileNames) const;

    /** return the contents of a directory.
      * returns an empty array on any error.*/
    osgDB::DirectoryContents getDirectoryContents(const std::string& dirName) const;

    /** reads a file from the archive into an io buffer. */
    bool readToBuffer( const std::string& fileInZip, std::ostream& iobuf ) const;

    ReadResult readImage(const std::string& filename, const Options* options =NULL) const;

    ReadResult readNode(const std::string& filename, const Options* options =NULL) const;

    ReadResult readObject(const std::string& filename, const Options* options =NULL) const;

    // stubs
    ReadResult readShader(const std::string&, const Options* =NULL) const { return ReadResult::NOT_IMPLEMENTED; }
    ReadResult readHeightField(const std::string&, const Options* =NULL) const { return ReadResult::NOT_IMPLEMENTED; }
    WriteResult writeObject(const osg::Object&, const std::string&,const Options* =NULL) const { return WriteResult::NOT_IMPLEMENTED; }
    WriteResult writeImage(const osg::Image&, const std::string&,const Options* =NULL) const { return WriteResult::NOT_IMPLEMENTED; }
    WriteResult writeHeightField(const osg::HeightField&,const std::string&,const Options* =NULL) const { return WriteResult::NOT_IMPLEMENTED; }
    WriteResult writeNode(const osg::Node&, const std::string&,const Options* =NULL) const { return WriteResult::NOT_IMPLEMENTED; }
    WriteResult writeShader(const osg::Shader&, const std::string&, const Options* =NULL) const { return WriteResult::NOT_IMPLEMENTED; }

private:
    URI            _archiveURI;

    osg::ref_ptr<osgDB::Archive> _zip;

    std::string resolve(const std::string& filename) const;
};


#endif // OSGEARTH_DRIVER_KML_KMZ_ARCHIVE
