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

#include <osgEarth/Common>
#include <osgEarth/FeatureCursor>
#include <osgEarth/Threading>

namespace osgEarth
{
    /**
     * Place name geocoding service
     */
    class OSGEARTH_EXPORT Geocoder
    {
    public:
        //! Base class for custom geocoder implementations
        class OSGEARTH_EXPORT Implementation : public osg::Referenced
        {
        public:
            virtual Status search(const std::string& input, osg::ref_ptr<FeatureCursor>& output) = 0;

            virtual void setServiceOption(const std::string& name, const std::string& value) =0;
        };

        //! Internal - do not use
        struct OutputData
        {
            OutputData() { }
            OutputData(const Status& status, FeatureCursor* cursor) : _status(status), _cursor(cursor) { }
            Status _status;
            osg::ref_ptr<FeatureCursor> _cursor;
        };

        //! Result object returned from a geocoding attempt
        class OSGEARTH_EXPORT Results
        {
        public:
            bool isReady() const { return _result.available(); }
            bool isWorking() const { return _result.working(); }

            //! Status of geocode operation -- check this first
            Status getStatus();

            //! Cursor for iterating over the geocoding results
            FeatureCursor* getFeatures();

            //! Internal constructors
            Results(const Status& status, FeatureCursor* cursor);
            Results(jobs::future<OutputData> future);
            Results(const OutputData& results);

        private:
            jobs::future<OutputData> _result;
        };

    public:
        //! Constructor
        Geocoder();

        //! Sets an implementation-specific option
        //! (See https://tinyurl.com/vacattl for OGR options)
        void setServiceOption(const std::string& key, const std::string& value);

        //! Geocode a search string
        //! If the options contains a JobArena, will run asynchronously
        Results search(const std::string& input, const osgDB::Options* ioOptions =NULL);

        //! Set the underlying implementation to use.
        void setImplementation(Implementation* impl);

    protected:
        osg::ref_ptr<Implementation> _impl;
    };
}

#endif // OSGEARTH_GEOCODER_H
