"""
@package datacatalog.toolbars

@brief Data Catalog toolbars

Classes:
 - toolbars::DataCatalogToolbar(BaseToolbar)

(C) 2016 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Martin Landa <landa.martin gmail.com>
"""

import wx
from gui_core.toolbars import BaseToolbar
from gui_core.wrap import SearchCtrl
from icons.icon import MetaIcon

icons = {
    "reloadTree": MetaIcon(img="redraw", label=_("Reload GRASS projects")),
    "reloadMapset": MetaIcon(img="reload", label=_("Reload current GRASS mapset only")),
    "addMapset": MetaIcon(
        img="mapset-add", label=_("Create new mapset in current project")
    ),
    "addProject": MetaIcon(
        img="location-add",
        label=_("Add an existing project"),
    ),
    "createProject": MetaIcon(
        img="location-create",
        label=_("Create new project"),
    ),
    "downloadLocation": MetaIcon(
        img="location-download",
        label=_("Download sample project"),
    ),
    "importRaster": MetaIcon(
        img="raster-import", label=_("Import raster data  [r.import]")
    ),
    "importVector": MetaIcon(
        img="vector-import", label=_("Import vector data  [v.import]")
    ),
    "importLayer": MetaIcon(
        img="layer-import", label=_("Select another import option")
    ),
}


class DataCatalogSearch(SearchCtrl):
    def __init__(self, parent, filter_function):
        super().__init__(parent)
        self.filter_function = filter_function
        self.filter_element = None
        self.SetDescriptiveText(_("Search"))
        self.ShowCancelButton(True)
        self.Bind(
            wx.EVT_TEXT,
            lambda event: self.filter_function(self.GetValue(), self.filter_element),
        )
        self.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.filter_function(""))
        filterMenu = wx.Menu()
        item = filterMenu.AppendRadioItem(-1, "All")
        self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
        item = filterMenu.AppendRadioItem(-1, "Raster maps")
        self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
        item = filterMenu.AppendRadioItem(-1, "Vector maps")
        self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
        item = filterMenu.AppendRadioItem(-1, "3D raster maps")
        self.Bind(wx.EVT_MENU, self.OnFilterMenu, item)
        self.SetMenu(filterMenu)
        helpTip = _(
            "Type to search database by map type or name. "
            "Use Python regular expressions to refine your search."
        )
        self.SetToolTip(helpTip)

    def OnFilterMenu(self, event):
        """Decide the element to filter by"""
        filterMenu = self.GetMenu().GetMenuItems()
        self.filter_element = None
        if filterMenu[1].IsChecked():
            self.filter_element = "raster"
        elif filterMenu[2].IsChecked():
            self.filter_element = "vector"
        elif filterMenu[3].IsChecked():
            self.filter_element = "raster_3d"
        # trigger filter on change
        if self.GetValue():
            self.filter_function(self.GetValue(), self.filter_element)


class DataCatalogToolbar(BaseToolbar):
    """Main data catalog toolbar"""

    def __init__(self, parent):
        """Main toolbar constructor"""

        BaseToolbar.__init__(self, parent)

        self.InitToolbar(self._toolbarData())
        # realize the toolbar
        self.Realize()

    def _toolbarData(self):
        """Returns toolbar data (name, icon, handler)"""
        # BaseIcons are a set of often used icons. It is possible
        # to reuse icons in gui/icons/grass or add new ones there.
        return self._getToolbarData(
            (
                (
                    ("reloadTree", icons["reloadTree"].label),
                    icons["reloadTree"],
                    self.parent.OnReloadTree,
                ),
                (
                    ("reloadMapset", icons["reloadMapset"].label),
                    icons["reloadMapset"],
                    self.parent.OnReloadCurrentMapset,
                ),
                (
                    ("createProject", icons["createProject"].label),
                    icons["createProject"],
                    self.parent.OnCreateLocation,
                ),
                (
                    ("addProject", icons["addProject"].label),
                    icons["addProject"],
                    self.parent.OnAddProject,
                ),
                (
                    ("downloadLocation", icons["downloadLocation"].label),
                    icons["downloadLocation"],
                    self.parent.OnDownloadLocation,
                ),
                (
                    ("addMapset", icons["addMapset"].label),
                    icons["addMapset"],
                    self.parent.OnCreateMapset,
                ),
                (
                    ("importRaster", icons["importRaster"].label),
                    icons["importRaster"],
                    self.parent.OnImportGdalLayers,
                ),
                (
                    ("importVector", icons["importVector"].label),
                    icons["importVector"],
                    self.parent.OnImportOgrLayers,
                ),
                (
                    ("importLayer", icons["importLayer"].label),
                    icons["importLayer"],
                    self.parent.OnImportMenu,
                ),
            )
        )
