/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2008-2012 Pelican Mapping
* MIT License
*/
#ifndef OSGEARTH_CESIUM_ASSET_ACCESSOR_H
#define OSGEARTH_CESIUM_ASSET_ACCESSOR_H

#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumAsync/IAssetResponse.h>
#include <osgDB/Options>
#include <osgEarth/Cache>

namespace osgEarth {
    namespace Cesium
    {
        using namespace osgEarth;

        class AssetResponse : public CesiumAsync::IAssetResponse
        {
        public:
            virtual uint16_t statusCode() const override;

            virtual std::string contentType() const override;

            virtual const CesiumAsync::HttpHeaders& headers() const override;

            virtual gsl::span<const std::byte> data() const override;

            uint16_t _statusCode = 0;
            std::string _contentType;
            CesiumAsync::HttpHeaders _headers;
            std::vector<std::byte> _result;
        };

        class AssetRequest : public CesiumAsync::IAssetRequest
        {
        public:

            AssetRequest(const std::string& method, const std::string& url, const std::vector<CesiumAsync::IAssetAccessor::THeader>& headers);

            virtual const std::string& method() const;
            
            virtual const std::string& url() const;

            virtual const CesiumAsync::HttpHeaders& headers() const;
     
            virtual const CesiumAsync::IAssetResponse* response() const;

            void setResponse(std::unique_ptr< AssetResponse > response);

            std::string _method;
            std::string _url;
            CesiumAsync::HttpHeaders _headers;
            std::unique_ptr<AssetResponse> _response;
        };

        class AssetAccessor : public CesiumAsync::IAssetAccessor
        {
        public:
            AssetAccessor();

            virtual CesiumAsync::Future<std::shared_ptr<CesiumAsync::IAssetRequest>>
                get(const CesiumAsync::AsyncSystem& asyncSystem,
                    const std::string& url,
                    const std::vector<CesiumAsync::IAssetAccessor::THeader>& headers)
                override;

            virtual CesiumAsync::Future<std::shared_ptr<CesiumAsync::IAssetRequest>>
                request(
                    const CesiumAsync::AsyncSystem& asyncSystem,
                    const std::string& verb,
                    const std::string& url,
                    const std::vector<CesiumAsync::IAssetAccessor::THeader>& headers,
                    const gsl::span<const std::byte>& contentPayload) override;

            virtual void tick() noexcept override;

        private:
            osg::ref_ptr< osgDB::Options > _options;
            osg::ref_ptr<CacheSettings> _cacheSettings;
        };
    }
}


#endif
