# -*- coding: utf-8 -*-
"""Redundant vertex checker — simplify over-detailed polygons."""
from .base import BaseChecker


class RedundantChecker(BaseChecker):
    name = "redundant"
    display_name_en = "Redundant Vertices"
    display_name_vi = "Dinh du thua"

    def _count_verts(self, g):
        count = 0
        for v in g.vertices():
            count += 1
        return count

    def check(self, layer, params):
        max_v = params.get("redundant_max_verts", 500)
        errors = []
        for feat in layer.getFeatures():
            g = feat.geometry()
            if g and not g.isNull() and self._count_verts(g) > max_v:
                errors.append(g)
        return {"error_geometries": errors, "error_features": [], "details": []}

    def fix(self, input_path, output_path, params):
        import processing
        tol = params.get("simplify_tolerance", 0.0)
        processing.run(
            "native:simplifygeometries",
            {"INPUT": input_path, "METHOD": 0,
             "TOLERANCE": tol, "OUTPUT": output_path})
        return output_path
