#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2021    Matthias Kemmer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""Create a heatmap web report."""
# ------------------------------------------------------------------------
#
# Python modules
#
# ------------------------------------------------------------------------
import os


# ------------------------------------------------------------------------
#
# GRAMPS modules
#
# ------------------------------------------------------------------------
from gramps.gen.plug.report import Report, MenuReportOptions
from gramps.gui.dialog import ErrorDialog
from gramps.gen.plug.menu import (
    EnumeratedListOption, PersonOption, DestinationOption, StringOption,
    NumberOption, BooleanOption)
from gramps.gen.filters import GenericFilterFactory, rules, CustomFilters
from gramps.gen.plug.docgen import ParagraphStyle
from gramps.gen.const import GRAMPS_LOCALE as glocale
try:
    _trans = glocale.get_addon_translator(__file__)
except ValueError:
    _trans = glocale.translation
_ = _trans.gettext


# ------------------------------------------------------------------------
#
# Report Options
#
# ------------------------------------------------------------------------
class ReportOptions(MenuReportOptions):
    """Heatmap report options."""

    def __init__(self, name, dbase):
        self.db = dbase
        MenuReportOptions.__init__(self, name, dbase)

    def add_menu_options(self, menu):
        """Create menu options."""

        # -------------------
        # GENERAL options tab
        # -------------------
        self.get_person_filters(menu)
        fltr = EnumeratedListOption(_("Filter"), 0)
        fltr.set_items(menu.filter_list)
        menu.add_option(_("General"), "fltr", fltr)

        pers_id = PersonOption(_("Person"))
        menu.add_option(_("General"), "pers_id", pers_id)

        filter_list2 = [
            (0, _("OpenStreetMap")),
            (1, _("Stamen Terrain")),
            (2, _("Stamen Toner")),
            (3, _("Stamen Watercolor")),
            (4, _("CartoDB Positron")),
            (5, _("CartoDB DarkMatter"))]
        tiles = EnumeratedListOption(_("Map tiles"), 0)
        tiles.set_items(filter_list2)
        menu.add_option(_("General"), "tiles", tiles)

        radius = NumberOption(_("Point size"), 15, 1, 50)
        radius.set_help(_("Set the size of the heatmap points.\nDefault: 15"))
        menu.add_option(_("General"), "radius", radius)

        path = DestinationOption(_("File path"), "")
        path.set_directory_entry(True)
        menu.add_option(_("General"), "path", path)

        txt = _("For the file name only english letters A-Z, a-z and "
                "numbers 0-9 as well as the characters space, underline and "
                "hypen are allowed. The file name also has to "
                "start and end with an alphanumeric character."
                "The file extention '.html' is added by the report.")
        file_name = StringOption(_("File name"), "")
        file_name.set_help(txt)
        menu.add_option(_("General"), "name", file_name)

        # -------------------
        # ADVANCED options tab
        # -------------------
        self.enable_start = BooleanOption(
            _("Enable custom start position"), False)
        self.enable_start.set_help(
            _("Enabling will force the map open at your custom "
            "start position and zoom."))
        menu.add_option(_("Advanced"), "enable_start", self.enable_start)
        self.enable_start.connect('value-changed', self.update_start_options)

        self.start_lat = StringOption(_("Start latitude"), "50.0")
        self.start_lat.set_help(
            _("Set custom start position latitude\nDefault: 50.0"))
        menu.add_option(_("Advanced"), "start_lat", self.start_lat)

        self.start_lon = StringOption(_("Start longitude"), "10.0")
        self.start_lon.set_help(
            _("Set custom start position longitude\nDefault: 10.0"))
        menu.add_option(_("Advanced"), "start_lon", self.start_lon)

        self.start_zoom = NumberOption(_("Start zoom"), 5, 1, 18)
        self.start_zoom.set_help(
            _("Set the value for the starting zoom\nDefault: 5"))
        menu.add_option(_("Advanced"), "start_zoom", self.start_zoom)

        # -------------------
        # LIMITS options tab
        # -------------------
        self.enable_limits = BooleanOption(_("Enable map limits"), False)
        self.enable_limits.set_help(
            _("Enabling map limits forces the user to stay in a predefined part"
            " of the map."))
        menu.add_option(_("Limits"), "enable_limits", self.enable_limits)
        self.enable_limits.connect('value-changed', self.update_limit_options)

        self.min_zoom = NumberOption(_("Min. zoom"), 1, 1, 18)
        self.min_zoom.set_help(_("Set minimal zoom value\nDefault: 1"))
        menu.add_option(_("Limits"), "min_zoom", self.min_zoom)

      
  self.max_zoom = NumberOption(_("Max. zoom"), 18, 1, 18)
        self.max_zoom.set_help(_("Set maximum zoom value.\nDefault: 18"))
        menu.add_option(_("Limits"), "max_zoom", self.max_zoom)

        self.lat1 = StringOption(_("
Upper left corner latitude"), "")
        self.lat1.set_help(
            _("Set latitude for upper left corner map limit."))
        menu.add_option(_("Limits"), "lat1", self.lat1)

        self.lon1 = StringOption(_("Upper left corner longitude"), "")
        self.lon1.set_help(
            _("Set longitude for upper left corner map limit."))
        menu.add_option(_("Limits"), "lon1", self.lon1)

        self.lat2 = StringOption(_("Lower right corner latitude"), "")
        self.lat2.set_help(
            _("Set latitude for lower right corner map limit."))
        menu.add_option(_("Limits"), "lat2", self.lat2)

        self.lon2 = StringOption(_("Lower right corner longitude"), "")
        self.lon2.set_help(
            _("Set longitude for lower right corner map limit."))
        menu.add_option(_("Limits"), "lon2", self.lon2)

        self.render_border = BooleanOption(
            _("Render custom map limit border"), False)
        self.render_border.set_help(
            _("Enabling will render the map limit border"))
        menu.add_option(_("Limits"), "render_border", self.render_border)

    def update_limit_options(self):
        """Update menu options for limit option tab."""
        self.min_zoom.set_available(False)
        self.max_zoom.set_available(False)
        self.lat1.set_available(False)
        self.lon1.set_available(False)
        self.lat2.set_available(False)
        self.lon2.set_available(False)
        self.render_border.set_available(False)
        value = self.enable_limits.get_value()
        if value:
            self.min_zoom.set_available(True)
            self.max_zoom.set_available(True)
            self.lat1.set_available(True)
            self.lon1.set_available(True)
            self.lat2.set_available(True)
            self.lon2.set_available(True)
            self.render_border.set_available(True)

    def update_start_options(self):
        """Update menu options for start option tab."""
        self.start_zoom.set_available(False)
        self.start_lat.set_available(False)
        self.start_lon.set_available(False)
        value = self.enable_start.get_value()
        if value:
            self.start_zoom.set_available(True)
            self.start_lat.set_available(True)
            self.start_lon.