# -*- coding: utf-8 -*-
"""
/***************************************************************************
 Limfjordssekretariatet_tools
                                 A QGIS plugin
 Limfjordssekretariatet forskellige tools
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2025-11-05
        git sha              : $Format:%H$
        copyright            : (C) 2025 by Henrik Rosenskjold, 
                                Limfjordssekretariatet
        email                : henrik.rosenskjold@aalborg.dk
 ***************************************************************************/

"""
import os
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction

# Initialize Qt resources from file resources.py
from .resources import *

# Import the code for the dialog
from .Limf_tools_dialog import Limfjordssekretariatet_toolsDialog


class Limfjordssekretariatet_tools:
    """QGIS Plugin Implementation."""

    def __init__(self, iface):
        """Constructor.
        :param iface: An interface instance that will be passed to this class
                      which provides the hook by which you can manipulate the QGIS application.
        :type iface: QgisInterface
        """
        self.iface = iface
        self.plugin_dir = None

        # Initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)

        # Initialize locale
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'Limfjordssekretariatet_tools_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)
            QCoreApplication.installTranslator(self.translator)

        # Declare instance attributes
        self.actions = []
        self.menu = self.tr('&Limfjordssekretariatet tools')
        self.first_start = None

    def tr(self, message):
        return QCoreApplication.translate('Limfjordssekretariatet_tools', message)

    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):

        icon = QIcon(icon_path)
        action = QAction(icon, text, parent)
        action.triggered.connect(callback)
        action.setEnabled(enabled_flag)

        if status_tip is not None:
            action.setStatusTip(status_tip)
        if whats_this is not None:
            action.setWhatsThis(whats_this)

        # Add to Toolbar
        if add_to_toolbar:
            self.iface.addToolBarIcon(action)

        # Add to Plugin Menu
        if add_to_menu:
            self.iface.addPluginToMenu(self.menu, action)

        self.actions.append(action)
        return action

    def initGui(self):
        """Create the menu entries and toolbar icon inside QGIS."""
        icon_path = ':/plugins/limf_tools/icon.png'   # IMPORTANT: case-sensitive
        self.add_action(
            icon_path,
            text=self.tr(u'Tools til Limfjordssekretariatet'),
            callback=self.run,
            parent=self.iface.mainWindow())

        self.first_start = True

    def unload(self):
        """Removes the plugin menu item and icon."""
        for action in self.actions:
            self.iface.removePluginMenu(self.menu, action)
            self.iface.removeToolBarIcon(action)

    def run(self):
        """Run whenever user clicks icon/menu."""
        if self.first_start:
            self.first_start = False
            self.dlg = Limfjordssekretariatet_toolsDialog()

        self.dlg.show()
        self.dlg.exec_()
