from typing import *

from linter_adapters.common import BaseCategory, Severity, ViolationPrototype
from linter_adapters.parser import BaseViolationParser


try:
    from flake8.plugins.pyflakes import FLAKE8_PYFLAKES_CODES
except ImportError:
    FLAKE8_PYFLAKES_CODES = dict()

class PyflakesCategory(BaseCategory):
    Imports = 400
    FormatStrings = 500
    Misuses = 600
    Statements = 700
    Variables = 800
    CommonMistakes = 900


@BaseViolationParser.register_decorator('F')
class PyflakesViolationParser(BaseViolationParser[PyflakesCategory]):
    provider_name = 'pyflakes'
    ViolationCategory = PyflakesCategory
    severity_mapping = \
    {
        PyflakesCategory.Imports:           Severity.Error,
        PyflakesCategory.FormatStrings:     Severity.Error,
        PyflakesCategory.Misuses:           Severity.Error,
        PyflakesCategory.Statements:        Severity.Error,
        PyflakesCategory.Variables:         Severity.Warning,
        PyflakesCategory.CommonMistakes:    Severity.Error,
    }
    default_severity = Severity.Error
    
    pyflakes_reverse_mapping: Dict[str, str] = { value: key for key, value in FLAKE8_PYFLAKES_CODES.items() }
    
    def parse_violation_extras(self, violation: ViolationPrototype[PyflakesCategory]) -> ViolationPrototype[PyflakesCategory]:
        violation.string_id = self.pyflakes_reverse_mapping.get(violation.error_code)
        return violation


__all__ = \
[
    'PyflakesCategory',
    'PyflakesViolationParser',
]
