# -*- coding: utf-8 -*-

__author__ = 'Alexandre Demidov/BRGM'
__date__ = '2024-02-05'
__copyright__ = '(C) 2024 by Alexandre Demidov/BRGM'
__revision__ = '$Format:%H$'

import os
import sys
import inspect

from qgis.core import QgsProcessingAlgorithm, QgsApplication, QgsSettings
from qgis.PyQt.QtCore import QTranslator, QCoreApplication, QLocale
from qgis.PyQt.QtGui import QColor, QIcon, QPixmap
from qgis.PyQt.QtWidgets import QAction
from .topo_provider import TopoProvider
import processing

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]

if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)

class TopoPlugin(object):

    def __init__(self, iface):
        self.provider = None
        self.iface = iface

        locale = QgsSettings().value('locale/userLocale', QLocale.system().name())[0:2]

        if locale != 'fr':
            self.translator = QTranslator()
            qm_path = os.path.join(cmd_folder, 'i18n', 'Topo_en.qm')
            
            if os.path.exists(qm_path):
                self.translator.load(qm_path)
                QCoreApplication.installTranslator(self.translator)

    def initProcessing(self):
        self.provider = TopoProvider()
        QgsApplication.processingRegistry().addProvider(self.provider)

    def initGui(self):
        self.initProcessing()
        icon = QIcon(cmd_folder+"/icon.png")
        self.action = QAction(
            QIcon(icon),
            u"Topo", self.iface.mainWindow())
        self.action.triggered.connect(self.run)
        self.iface.addPluginToMenu(u"&Topo", self.action)
        self.iface.addToolBarIcon(self.action)
        
    def unload(self):
        QgsApplication.processingRegistry().removeProvider(self.provider)
        self.iface.removePluginMenu(u"&Topo", self.action)
        self.iface.removeToolBarIcon(self.action)

    def run(self):
        processing.execAlgorithmDialog("Topo:Topo")

