# -*- coding: utf-8 -*-
"""
/***************************************************************************
 DsgTools
                                 A QGIS plugin
 Brazilian Army Cartographic Production Tools
                              -------------------
        begin                : 2019-08-19
        git sha              : $Format:%H$
        copyright            : (C) 2019 by João P. Esperidião - Cartographic Engineer @ Brazilian Army
        email                : esperidiao.joao@eb.mil.br
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

import json

from qgis.PyQt.QtCore import QCoreApplication, QMetaType
from qgis.core import (
    QgsProcessingAlgorithm,
    QgsProcessingParameterFeatureSink,
    QgsProcessingParameterEnum,
    QgsFeatureSink,
    QgsCoordinateReferenceSystem,
    QgsFields,
    QgsField,
    QgsWkbTypes,
)

from DsgTools.tests.test_ValidationAlgorithms import Tester
from DsgTools.core.GeometricTools.featureHandler import FeatureHandler


class SingleOutputUnitTestAlgorithm(QgsProcessingAlgorithm):
    __description__ = (
        """Runs unit tests for a set of DSGTools algorithms that"""
        """has single output - in-place modified or otherwise."""
    )
    AVAILABLE_ALGS = [
        # identification algs
        "dsgtools:identifyoutofboundsangles",
        "dsgtools:identifyoutofboundsanglesincoverage",
        "dsgtools:identifygaps",
        "dsgtools:identifyandfixinvalidgeometries",
        "dsgtools:identifyduplicatedfeatures",
        "dsgtools:identifyduplicatedgeometries",
        "dsgtools:identifyduplicatedlinesoncoverage",
        "dsgtools:identifysmalllines",
        "dsgtools:identifyduplicatedpolygonsoncoverage",
        "dsgtools:identifysmallpolygons",
        "dsgtools:identifydangles",
        "dsgtools:identifyduplicatedpointsoncoverage",
        "dsgtools:identifyoverlaps",
        "dsgtools:identifyvertexnearedges",
        # correction algs
        "dsgtools:removeduplicatedfeatures",
        "dsgtools:removeduplicatedgeometries",
        "dsgtools:removesmalllines",
        "dsgtools:removesmallpolygons",
        # manipulation algs
        "dsgtools:lineonlineoverlayer",
        "dsgtools:mergelineswithsameattributeset",
        "dsgtools:overlayelementswithareas",
        "dsgtools:deaggregategeometries",
        "dsgtools:dissolvepolygonswithsameattributes",
        "dsgtools:removeemptyandupdate",
        "dsgtools:snaplayeronlayer",
        # network algs
        "dsgtools:adjustnetworkconnectivity",
        "dsgtools:identifyunsharedvertexonintersectionsalgorithm",
    ]
    INPUT_ALGS = "INPUT_ALGS"
    OUTPUT = "OUTPUT"

    def __init__(self):
        super(SingleOutputUnitTestAlgorithm, self).__init__()

    def initAlgorithm(self, config=None):
        """
        Parameter setting.
        """
        self.addParameter(
            QgsProcessingParameterEnum(
                self.INPUT_ALGS,
                self.tr("Algorithms to be tested"),
                options=self.AVAILABLE_ALGS,
                optional=False,
                allowMultiple=True,
            )
        )
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT, self.tr("DSGTools Single Output Algorithms Unit Tests")
            )
        )

    def name(self):
        """
        Returns the algorithm name, used for identifying the algorithm. This
        string should be fixed for the algorithm, and must not be localised
        (e.g. must be ASCII). The name should be unique within each provider.
        Names should contain lowercase alphanumeric characters only and no
        spaces or other formatting characters.
        """
        return "singleoutputunittest"

    def displayName(self):
        """
        Returns the translated algorithm name, which should be used for any
        user-visible display of the algorithm name.
        """
        return self.tr("Single Output Algorithms Unit Test")

    def group(self):
        """
        Returns the name of the group this algorithm belongs to. This string
        should be localised.
        """
        return self.tr("Utils")

    def groupId(self):
        """
        Returns the unique ID of the group this algorithm belongs to. This
        string should be fixed for the algorithm, and must not be localised.
        The group id should be unique within each provider. Group id should
        contain lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return "DSGTools - Utils"

    def tr(self, string):
        return QCoreApplication.translate("SingleOutputUnitTestAlgorithm", string)

    def createInstance(self):
        """
        Gets a new instance of algotithm object.
        """
        return SingleOutputUnitTestAlgorithm()

    def getFields(self):
        """
        Gets all fields for output layer.
        """
        fields = QgsFields()
        fields.append(QgsField("algorithm", QMetaType.Type.QString))
        fields.append(QgsField("tests_output", QMetaType.Type.QString))
        fields.append(QgsField("status", QMetaType.Type.QString))
        return fields

    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        algsOutput, dest_id = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            self.getFields(),
            QgsWkbTypes.NoGeometry,
            QgsCoordinateReferenceSystem("EPSG:4326"),
        )
        tester = Tester()
        featureHandler = FeatureHandler()
        fields = self.getFields()
        feats = set()
        algIndexes = self.parameterAsEnums(parameters, self.INPUT_ALGS, context)
        size = len(algIndexes)
        failCount = 0
        totalProgress = 100 / size if size else 0
        feedback.setProgress(0)
        for currentStep, algIdx in enumerate(algIndexes):
            if feedback.isCanceled():
                break
            alg = self.AVAILABLE_ALGS[algIdx]
            feedback.pushInfo(self.tr("Testing {alg}'s...").format(alg=alg))
            # decided not to pass feedback to not pollute this alg's log
            msg = tester.testAlg(
                alg, context=context
            )  # , feedback=feedback, context=context)
            status = self.tr("Failed") if msg else self.tr("Passed")
            pushMethod = feedback.reportError if msg else feedback.pushDebugInfo
            failCount += 1 if msg else 0
            msg = msg or self.tr("All tests for {alg} are OK.").format(alg=alg)
            pushMethod("{msg}\n".format(msg=msg))
            feats.add(
                featureHandler.createFeatureFromLayer(
                    algsOutput,
                    attributes={
                        "algorithm": alg,
                        "tests_output": msg,
                        "status": status,
                    },
                    fields=fields,
                )
            )
            feedback.setProgress(currentStep * totalProgress)
        algsOutput.addFeatures(feats, QgsFeatureSink.FastInsert)
        if failCount:
            feedback.reportError(
                self.tr("{0} algorithms failed their unit tests.").format(failCount)
            )
        else:
            feedback.pushDebugInfo(self.tr("All algorithms passed their unit tests."))
        return {self.OUTPUT: dest_id}
