/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2018 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_IMGUI_ANNOTATIONS_GUI
#define OSGEARTH_IMGUI_ANNOTATIONS_GUI

#include "ImGui"
#include <osgEarth/AnnotationData>
#include <osgEarth/ViewFitter>
#include <osg/NodeVisitor>

namespace
{
    // Recursively set a node mask on all nodes with an AnnotationData
    struct SetAnnotationNodeMask : public osg::NodeVisitor
    {
        unsigned mask;

        SetAnnotationNodeMask(unsigned value) : mask(value)
        {
            setTraversalMode(TRAVERSE_ALL_CHILDREN);
            setNodeMaskOverride(~0);
        }

        void apply(osg::Node& node) override
        {
            auto data = dynamic_cast<osgEarth::AnnotationData*>(node.getUserData());
            if (data)
            {
                node.setNodeMask(mask);
            }

            traverse(node);
        }
    };

    struct GetAnnotations : public osg::NodeVisitor
    {
        osgEarth::EarthManipulator* manip = nullptr;
        const osgEarth::SpatialReference* srs = nullptr;
        osg::Camera* camera = nullptr;

        GetAnnotations()
        {
            setTraversalMode(TRAVERSE_ALL_CHILDREN);
            setNodeMaskOverride(~0);
        }

        void apply(osg::Node& node) override
        {
            auto anode = dynamic_cast<osgEarth::AnnotationNode*>(&node);
            auto data = dynamic_cast<osgEarth::AnnotationData*>(node.getUserData());

            if (data)
            {
                ImGui::PushID((std::uintptr_t)data);

                auto name = data->getName();
                auto vp = data->getViewpoint();
                auto desc = data->getDescription();

                bool visible = node.getNodeMask() != 0;
                if (ImGui::Checkbox("", &visible))
                {
                    SetAnnotationNodeMask set(visible ? ~0 : 0);
                    node.accept(set);
                }
                ImGui::SameLine();
                bool is_selected = false;
                if (ImGui::Selectable(name.c_str(), &is_selected) && manip && vp)
                {
                    manip->setViewpoint(*vp);
                }

                ImGui::PopID();

                ImGui::Indent();
            }
            else if (anode)
            {
                ImGui::PushID((std::uintptr_t)data);

                auto name = anode->getName();
                if (name.empty()) name = "[" + std::string(anode->className()) + "]";

                bool visible = node.getNodeMask() != 0;
                if (ImGui::Checkbox("", &visible))
                {
                    SetAnnotationNodeMask set(visible ? ~0 : 0);
                    node.accept(set);
                }
                ImGui::SameLine();
                bool is_selected = false;
                if (ImGui::Selectable(name.c_str(), &is_selected) && manip)
                {
                    ViewFitter fitter(srs, camera);
                    Viewpoint vp;
                    fitter.createViewpoint(anode, vp);
                    manip->setViewpoint(vp);
                }

                ImGui::PopID();

                ImGui::Indent();

            }

            traverse(node);

            if (data || anode)
            {
                ImGui::Unindent();
            }
        }
    };
}

namespace osgEarth
{
    namespace GUI
    {
        using namespace osgEarth;

        class AnnotationsGUI : public BaseGUI
        {
        private:
            osg::observer_ptr<MapNode> _mapNode;

        public:
            AnnotationsGUI() : BaseGUI("Annotations") { }

            void load(const Config& conf) override
            {
            }

            void save(Config& conf) override
            {
            }

            void draw(osg::RenderInfo& ri) override
            {
                if (!isVisible())
                    return;

                if (!findNodeOrHide(_mapNode, ri))
                    return;

                ImGui::Begin(name(), visible());
                {
                    GetAnnotations getannos;
                    getannos.manip = dynamic_cast<osgEarth::EarthManipulator*>(view(ri)->getCameraManipulator());
                    getannos.srs = _mapNode->getMapSRS();
                    getannos.camera = camera(ri);
                    camera(ri)->accept(getannos);
                }
                ImGui::End();
            }
        };
    }
}

#endif // OSGEARTH_IMGUI_ANNOTATIONS_GUI
