from qgis.PyQt.QtWidgets import QAction, QMenu
from qgis.PyQt.QtGui import QIcon
import os

# Conversion (factores de escala / DN -> Reflectancia)
from .Script.rs_landsat_c2_l1 import RSLandSatC2L1
from .Script.factor_landsat import FactorLandsat
from .Script.factor_sentinel2_l1a import FactorSentinel2L1A
from .Script.factor_sentinel2_l2a import FactorSentinel2L2A

# Procesamiento (analisis avanzado)
from .Script.cbers04a_pansharp import CBERS04APansharp
from .Script.acp_satelite import ACPSatelite
from .Script.indices_espectrales import IndicesEspectrales
from .Script.clasificacion_no_supervisada import ClasificacionNoSupervisada


class GeomaticapePlugin:

    def __init__(self, iface):
        self.iface = iface
        self.plugin_dir = os.path.dirname(__file__)
        self.actions = []
        self.menu = None
        self.menu_conv = None
        self.menu_proc = None

    def initGui(self):

        # Menu raiz Geomaticape
        logo_path = os.path.join(self.plugin_dir, "Icons", "logo_geomatica.png")
        self.menu = QMenu("Geomaticape", self.iface.mainWindow())
        self.menu.setIcon(QIcon(logo_path))
        self.iface.pluginMenu().addMenu(self.menu)

        # Submenu CONVERSION
        self.menu_conv = QMenu("Conversion", self.menu)
        self.menu_conv.setIcon(QIcon(os.path.join(self.plugin_dir, "Icons", "landsat.png")))
        self.menu.addMenu(self.menu_conv)

        self.add_action(self.menu_conv,
                        "RS LandSat C2 L1 (SR + LST + PAN)",
                        "Icons/landsat.png",
                        RSLandSatC2L1)
        self.add_action(self.menu_conv,
                        "Factor escala Landsat C2 L2",
                        "Icons/landsat.png",
                        FactorLandsat)
        self.add_action(self.menu_conv,
                        "Factor escala Sentinel2 L1A",
                        "Icons/sentinel2l1a.png",
                        FactorSentinel2L1A)
        self.add_action(self.menu_conv,
                        "Factor escala Sentinel2 L2A",
                        "Icons/sentinel2l2a.png",
                        FactorSentinel2L2A)

        self.menu.addSeparator()

        # Submenu PROCESAMIENTO
        self.menu_proc = QMenu("Procesamiento", self.menu)
        self.menu_proc.setIcon(QIcon(os.path.join(self.plugin_dir, "Icons", "acp.png")))
        self.menu.addMenu(self.menu_proc)

        self.add_action(self.menu_proc,
                        "CBERS-04A Pansharpening 2m",
                        "Icons/cbers04a.png",
                        CBERS04APansharp)
        self.add_action(self.menu_proc,
                        "ACP Multiespectral (cualquier satelite)",
                        "Icons/acp.png",
                        ACPSatelite)
        self.add_action(self.menu_proc,
                        "Indices espectrales (NDVI, SAVI, EVI, NDWI...)",
                        "Icons/indices.png",
                        IndicesEspectrales)
        self.add_action(self.menu_proc,
                        "Clasificacion no supervisada (K-Means, GMM, ISODATA, Birch)",
                        "Icons/clasificacion.png",
                        ClasificacionNoSupervisada)

    def add_action(self, parent_menu, text, icon_path, tool_class):
        icon = QIcon(os.path.join(self.plugin_dir, icon_path))
        action = QAction(icon, text, self.iface.mainWindow())
        action.triggered.connect(lambda: tool_class().run())
        parent_menu.addAction(action)
        self.actions.append(action)

    def unload(self):
        if self.menu:
            self.iface.pluginMenu().removeAction(self.menu.menuAction())
            self.menu = None
            self.menu_conv = None
            self.menu_proc = None
