# -*- coding: utf-8 -*-
"""
rem_plugin.py
Main plugin class: registers the Processing provider and a custom toolbar
with two buttons — one per REM tool.
"""

import os
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QToolBar
from qgis.PyQt.QtCore import QSize
from qgis.core import QgsApplication
from qgis.utils import iface
import processing

from .rem_provider import REMProvider


class REMPlugin:

    def __init__(self, iface):
        self.plugin_dir = os.path.dirname(__file__)
        self.iface = iface
        self.provider = None
        self.toolbar = None
        self.actions = []

    # ------------------------------------------------------------------ #
    #  QGIS interface                                                      #
    # ------------------------------------------------------------------ #

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

    def initGui(self):
        icon_path = os.path.join(self.plugin_dir, "remtool.png")
        self.initProcessing()

        # Create custom toolbar
        self.toolbar = self.iface.mainWindow().addToolBar("REM Tools")
        self.toolbar.setObjectName("REMToolsToolbar")
        self.toolbar.setToolTip("Relative Elevation Model Tools")

        # ---- Button 1: Create River Line -------------------------------
        action_river = QAction(
            self.iface.mainWindow()
        )
        action_river.setText("〰 Create River Line")
        action_river.setToolTip(
            "〰 Create River Line from DEM\n"
            "Auto-extract a river network using flow accumulation"
        )
        action_river.triggered.connect(self._open_create_river_line)
        self.toolbar.addAction(action_river)
        self.iface.addPluginToRasterMenu("&REM Tools", action_river)
        self.actions.append(action_river)

        # ---- Button 2: Generate REM ------------------------------------
        action_rem = QAction(
            self.iface.mainWindow()
        )
        action_rem.setText("🌄 Generate REM")
        action_rem.setToolTip(
            "🌄 Generate Relative Elevation Model\n"
            "Produce a REM from a DEM and a clean river line"
        )
        action_rem.triggered.connect(self._open_generate_rem)
        self.toolbar.addAction(action_rem)
        self.iface.addPluginToRasterMenu("&REM Tools", action_rem)
        self.actions.append(action_rem)

    def unload(self):
        # Remove menu items
        for action in self.actions:
            self.iface.removePluginRasterMenu("&REM Tools", action)

        # Remove toolbar
        if self.toolbar:
            self.toolbar.deleteLater()
            self.toolbar = None

        # Remove processing provider
        if self.provider:
            QgsApplication.processingRegistry().removeProvider(self.provider)
            self.provider = None

    # ------------------------------------------------------------------ #
    #  Button callbacks                                                    #
    # ------------------------------------------------------------------ #

    def _open_create_river_line(self):
        """Open the Create River Line algorithm dialog."""
        processing.execAlgorithmDialog("rem:createriverline")

    def _open_generate_rem(self):
        """Open the Generate REM algorithm dialog."""
        processing.execAlgorithmDialog("rem:generaterem")
