"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 25.10.21

"""
from django.utils.translation import gettext_lazy as _, pgettext_lazy as _con
from konova.utils.quality import AbstractQualityChecker


class CompensationQualityChecker(AbstractQualityChecker):
    def run_check(self):
        """ Perform all defined data checks

        Returns:

        """
        self._check_states()
        self._check_actions()
        self._check_geometry()
        self._check_deadlines()
        self.valid = len(self.messages) == 0

    def _check_states(self):
        """ Checks data quality for related CompensationState objects

        Returns:

        """
        after_states = self.obj.get_surface_after_states()
        before_states = self.obj.get_surface_before_states()

        if after_states != before_states:
            self.messages.append(
                _("States unequal")
            )
        if before_states == 0:
            self._add_missing_attr_name(_("States before"))
        if after_states == 0:
            self._add_missing_attr_name(_("States after"))

    def _check_actions(self):
        """ Checks data quality for related CompensationState objects

        Returns:

        """
        if not self.obj.actions.all():
            self._add_missing_attr_name(_con("Compensation", "Actions"))

    def _check_deadlines(self):
        """ Checks data quality for related Deadline objects

        Returns:

        """
        finished_deadlines = self.obj.get_finished_deadlines()
        if not finished_deadlines.exists():
            self._add_missing_attr_name(_("Finished deadlines"))


class EcoAccountQualityChecker(CompensationQualityChecker):
    def run_check(self):
        """ Checks on data quality for an EcoAccount

        Returns:

        """
        self._check_deductable_surface()
        self._check_responsible_data()
        self._check_legal_data()
        super().run_check()

    def _check_legal_data(self):
        """ Checks the data quality for Legal

        Returns:

        """
        try:
            legal = self.obj.legal
            if legal.registration_date is None:
                self._add_missing_attr_name(_("Agreement date"))
        except AttributeError:
            self._add_missing_attr_name(_("Legal data"))

    def _check_deductable_surface(self):
        """ Checks the quality of the deductable surface value

        Returns:

        """
        surface = self.obj.deductable_surface or 0
        is_surface_invalid = surface == 0
        if is_surface_invalid:
            self._add_missing_attr_name(_("Available Surface"))
        after_state_surface = self.obj.get_surface_after_states()
        if surface > after_state_surface:
            self.messages.append(
                _("Deductable surface can not be larger than state surface")
            )

    def _check_responsible_data(self):
        """ Checks on responsible data quality

        Returns:

        """
        try:
            resp = self.obj.responsible
            if resp.conservation_office is None:
                self._add_missing_attr_name(_("Conservation office"))
            if resp.conservation_file_number is None or len(resp.conservation_file_number) == 0:
                self._add_missing_attr_name(_("Conservation office file number"))
        except AttributeError:
            self._add_missing_attr_name(_("Responsible data"))
