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

"""
/***************************************************************************
 NetworkCleanup
                                 A QGIS plugin
 A collection of tools to help fix network topology.
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2026-02-12
        copyright            : (C) 2026 by Thomas Raj
        email                : lemungineer@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""

__author__ = 'Thomas Raj'
__date__ = '2026-02-12'
__copyright__ = '(C) 2026 by Thomas Raj'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
    QgsProcessing,
    QgsFeatureSink,
    QgsProcessingAlgorithm,
    QgsProcessingParameterFeatureSource,
    QgsProcessingParameterFeatureSink,
    QgsProcessingParameterField,
    QgsFeatureRequest,
    QgsProcessingParameterBoolean,
    QgsProcessingParameterNumber,
    QgsProcessingParameterVectorLayer
)

from .logic.recursive_selection import *

class recursiveSelectionAlgorithm(QgsProcessingAlgorithm):

    INPUT = 'INPUT'
    OUTPUT = 'OUTPUT'

    def initAlgorithm(self, config):

        # Input line layer
        self.addParameter(
            QgsProcessingParameterVectorLayer(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorLine]
            )
        )
        
    def processAlgorithm(self, parameters, context, feedback):
        # Use the actual project layer
        layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

        #feedback.pushInfo(f"Using ID field: {id_field}")

        # Run the selection function
        recursive_selection(edges=layer)

        # Return the original layer as output (or create a dummy output if needed)
        return {self.OUTPUT: layer.id()}


    def name(self):
        return 'recursive_selection'

    def displayName(self):
        return self.tr('Recursive Selection')

    def group(self):
        return self.tr(self.groupId())

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def shortHelpString(self):
        return self.tr("""
    This purpose of this algorithm is to determine which lines feed into the selected line.
                       
    This is accomplished by find which lines end where the selected line starts.
                       
    This process is repeated until no more lines are no more lines feeding into the selected part of the network are found.
    """)

    def createInstance(self):
        return recursiveSelectionAlgorithm()