/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2016 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/>
 */
#pragma once

#include "Common"
#include "Elevation"
#include "Zoning"
#include <vector>
#include <osgEarth/Geometry>

namespace osgEarth { namespace Buildings
{
    using namespace osgEarth;

    class BuildingVisitor;
    class BuildContext;

    /**
     * Building represents one complete structure, comprised of
     * at least one elevation and a roof.
     */
    class OSGEARTHBUILDINGS_EXPORT Building
    {
    public:
        /** Construct a new Building */
        Building();

        /** Copy constructor */
        Building(const Building& rhs);

        /** Move constructor */
        Building(Building&& rhs) noexcept;

        /** Copy assignment */
        Building& operator=(const Building& rhs);

        /** Move assignment */
        Building& operator=(Building&& rhs) noexcept;

        /** Destructor */
        ~Building() = default;

        /** Clone this building */
        Building clone() const;

        //! Valid
        operator bool() const {
            return !_elevations.empty() ||
                _externalModelURI.isSet() ||
                _instancedModelSymbol.valid();
        }

        /**
         * Tag methods (inlined from Taggable)
         */
        void addTag(const std::string& tag);
        void addTags(const TagVector& tags);
        void addTags(const std::string& tagString);
        void removeTag(const std::string& tag);
        bool containsTag(const std::string& tag) const;
        bool containsTags(const TagSet& tags) const;
        bool containsTags(const TagVector& tags) const;
        const TagSet& tags() const { return _tags; }
        std::string tagString() const;
        static std::string tagString(const TagSet& tags);
        static std::string tagString(const TagVector& tags);

        /**
         * Building's unique identifier.
         */
        void setUID(UID uid) { _uid = uid; }
        UID getUID() const   { return _uid; }

        /**
         * Reference frame for all coordinates used to define the building.
         */
        void setReferenceFrame(const osg::Matrix& local2world) { _local2world = local2world; }
        const osg::Matrix& getReferenceFrame() const           { return _local2world; }

        /**
         * Sets the overall height of the building in meters.
         * This method will traverse all the elevations and resolve any heights
         * that are set as percentages into actual height values.
         */
        void setHeight(float value);

        /**
         * One or more elevations that comprise the facade of the building.
         */
        std::vector<Elevation>& getElevations()             { return _elevations; }
        const std::vector<Elevation>& getElevations() const { return _elevations; }

        /**
         * Zoning for this building
         */
        void setZoning(const Zoning::Type& zoning) { _zoning = zoning; }
        const Zoning::Type& getZoning() const      { return _zoning; }

        /**
         * Minimum height for which to use this building.
         */
        void setMinHeight(float value) { _minHeight = value; }
        float getMinHeight() const     { return _minHeight; }

        /**
         * Maximum height for which to use this building.
         */
        void setMaxHeight(float value) { _maxHeight = value; }
        float getMaxHeight() const     { return _maxHeight; }

        /**
         * Minimum are for which to use this building as a template (sqm)
         */
        void setMinArea(float value) { _minArea = value; }
        float getMinArea() const     { return _minArea; }

        /**
         * Maximum area for which to use this building as a template (sqm)
         */
        void setMaxArea(float value) { _maxArea = value; }
        float getMaxArea() const     { return _maxArea; }

        /**
         * Whether to load an instanced model for this building instead of
         * generating geometry
         */
        void setInstanced(bool value) { _instanced = value; }
        bool getInstanced() const     { return _instanced; }

        /**
         * URI of an external model to use instead of creating geometry or
         * using an instanced model.
         */
        void setExternalModelURI(const URI& uri)      { _externalModelURI = uri; }
        const optional<URI>& externalModelURI() const { return _externalModelURI; }
        const URI& getExternalModelURI() const        { return _externalModelURI.get(); }

        /**
         * Symbol for instanced model replacement instead of creating geometry.
         * An instanced model differs from an extern model (see setModelURI) in
         * that an instanced model comes from the ResourceLibrary and is scaled
         * and rotated to match the footprint's bounding box.
         */
        void setInstancedModelSymbol(ModelSymbol* symbol) { _instancedModelSymbol = symbol; }
        ModelSymbol* getInstancedModelSymbol() const      { return _instancedModelSymbol.get(); }

        /**
         * Model resource resolved from the instanced model symbol above.
         */
        void setInstancedModelResource(ModelResource* res) { _instancedModelResource = res; }
        ModelResource* getInstancedModelResource() const   { return _instancedModelResource.get(); }

        /**
         * Build the internal structure of the building and its components.
         * You must call setFootprint() before calling this function.
         */
        bool build(
            const Polygon*,
            BuildContext& bi,
            ProgressCallback* progress);

        /**
         * Visit this building.
         */
        void accept(BuildingVisitor& bv);

        Config getConfig() const;

    private:
        TagSet                  _tags;
        UID                     _uid;
        optional<URI>           _externalModelURI;
        std::vector<Elevation>  _elevations;
        Zoning::Type            _zoning;
        osg::Matrix             _local2world;
        float                   _minHeight;
        float                   _maxHeight;
        float                   _minArea;
        float                   _maxArea;
        bool                    _instanced;

        osg::ref_ptr<ModelSymbol>   _instancedModelSymbol;
        osg::ref_ptr<ModelResource> _instancedModelResource;

        void resolveInstancedModel(BuildContext&, ProgressCallback* progress);

        std::string normalize(const std::string& input) const;
    };

    using BuildingVector = std::vector<Building>;
} }
