/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2015 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef OSGEARTH_PROGRESS_H
#define OSGEARTH_PROGRESS_H 1

#include <osgEarth/Common>
#include <osgEarth/Containers>

namespace osgEarth
{
    /**
    * ProgressCallback is a general purpose interface for functions that need to report progress.
    */
    class OSGEARTH_EXPORT ProgressCallback : public osg::Referenced
    {
    public:
        /**
        * Creates a new ProgressCallback
        */
        ProgressCallback();
        virtual ~ProgressCallback() { }

        /**
        * Callback function that will be called.
        * @param current
        *        The amount of work done in the current stage
        * @param total
        *        The total amount of work to be done in the current stage
        * @param stage
        *        Stage of the operation we're currently in
        * @param totalStages
        *        Total number of stages in the operation
        * @param msg
        *        Description of what is being done. Useful when total is unknown.
        * @param returns
        *        Returns true if the current task should be cancelled, false otherwise.
        */
        virtual bool reportProgress(
            double             current, 
            double             total, 
            unsigned           currentStage,
            unsigned           totalStages,
            const std::string& msg );

        /**
         * Convenience functions
         */
        bool reportProgress( double current, double total, const std::string& msg ) {
            return reportProgress(current, total, 0, 1, msg );
        }
        bool reportProgress( double current, double total ) {
            return reportProgress(current, total, 0, 1, "" );
        }

        /**
         * called when the process starts
         */
        virtual void onStarted() { }

        /**
         * called when the process completed 
         */
        virtual void onCompleted() { }

        /**
         * Sets the cancelation flag
         */
        virtual void cancel() { _canceled = true; }

        /**
         * Whether cancelation was requested
         */
        virtual bool isCanceled() { return _canceled; }

        std::string& message() { return _message; }

        /**
         * Resets the canceled flag.
         */
        void reset() { _canceled = false; }

        /**
        *Whether or not the task should be retried.
        */
        bool needsRetry() const { return _needsRetry; }

        /**
         * Sets whether or not the task should be retried
         */
        void setNeedsRetry( bool needsRetry ) { _needsRetry = needsRetry; }

        /**
         * Access user stats
         */
        fast_map<std::string,double>& stats() { return _stats; }

    protected:
        volatile unsigned _numStages;
        std::string       _message;
        mutable  bool     _needsRetry;
        mutable  bool     _canceled;
        mutable  fast_map<std::string, double> _stats;
    };


    /**
    * ConsoleProgressCallback is a simple ProgressCallback that reports progress to the console
    */
    class OSGEARTH_EXPORT ConsoleProgressCallback : public ProgressCallback
    {
    public:
        /**
        * Creates a new ConsoleProgressCallback
        */
        ConsoleProgressCallback();
        virtual ~ConsoleProgressCallback() { }

        /**
        * Callback function that will be called.
        * @param current
        *        The amount of work done in the current stage
        * @param total
        *        The total amount of work to be done in the current stage
        * @param stage
        *        Stage of the operation we're currently in
        * @param totalStages
        *        Total number of stages in the operation
        * @param msg
        *        Description of what is being done. Useful when total is unknown.
        * @param returns
        *        Returns true if the current task should be cancelled, false otherwise.
        */
        virtual bool reportProgress(
            double             current, 
            double             total, 
            unsigned           currentStage,
            unsigned           totalStages,
            const std::string& msg );
    };
}

#endif