# -*- 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$'

import os

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

from qgis.PyQt.QtGui import QIcon

from .logic.disjoint_check import disjoint_check


class disjointCheckAlgorithm(QgsProcessingAlgorithm):

    INPUT = 'INPUT'
    CHECK_SELECTED = 'CHECK_SELECTED'
    OUTPUT = 'OUTPUT'

    def initAlgorithm(self, config):

        # Input line layer
        self.addParameter(
            QgsProcessingParameterVectorLayer(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorLine]
            )
        )

        self.addParameter(
            QgsProcessingParameterBoolean(
                'CHECK_SELECTED',           # internal parameter name
                self.tr('Disjoint Check Selected Feature?'),  # label shown in GUI
                defaultValue=False         # optional, default True or False
            )
        )


    def processAlgorithm(self, parameters, context, feedback):
        # Use the actual project layer
        layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

        # Retrieve ID field
        check_selected_feature = self.parameterAsBool(parameters, self.CHECK_SELECTED, context)

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

        # Run the selection function
        disjoint_check(layer=layer, selected_features=check_selected_feature)

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


    def name(self):
        return 'disjoint_check'

    def displayName(self):
        return self.tr('Disjoint Check')

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

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

    def shortHelpString(self):
        return self.tr("""
    This algorithm selects all lines that are connected to one another.
                       
    A starter line (or lines) can be selected to begin this process or the algorithm can choose a line at random within the network.
                       
    """)

    def createInstance(self):
        return disjointCheckAlgorithm()