"""
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 _
from konova.utils.quality import AbstractQualityChecker


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

        Returns:

        """

        self._check_responsible_data()
        self._check_legal_data()
        self._check_compensations()
        self._check_geometry()
        self._check_payment_documents()
        self.valid = len(self.messages) == 0

    def _check_payment_documents(self):
        """ Checks existence of documents in case of payments

        There should be at least one legal document which defines the payment's total amount.

        Returns:

        """
        has_payment_without_document = self.obj.payments.exists() and not self.obj.get_documents()[1].exists()
        if has_payment_without_document:
            self._add_missing_attr_name(_("Documents"))

    def _check_responsible_data(self):
        """ Checks data quality of related Responsibility

        Args:
            self.messages (dict): Holds error messages

        Returns:

        """
        try:
            resp = self.obj.responsible
            # Check for file numbers
            if not resp.registration_file_number or len(resp.registration_file_number) == 0:
                self._add_missing_attr_name(_("Registration office file number"))

            if not resp.conservation_file_number or len(resp.conservation_file_number) == 0:
                self._add_missing_attr_name(_("Conservation office file number"))

            # Check for selected offices
            if resp.registration_office is None:
                self._add_missing_attr_name(_("Registration office"))

            if resp.conservation_office is None:
                self._add_missing_attr_name(_("Conservation office"))

            if resp.handler is None:
                self._add_missing_attr_name(_("Intervention handler"))
        except AttributeError:
            # responsible data not found
            self._add_missing_attr_name(_("Responsible data"))

    def _check_legal_data(self):
        """ Checks data quality of related Legal

        Args:
            self.messages (dict): Holds error messages

        Returns:

        """
        try:
            legal = self.obj.legal
            # Check for a revocation
            if legal.revocations.exists():
                self.messages.append(_("Revocations exists"))

            if legal.registration_date is None:
                self._add_missing_attr_name(_("Registration date"))

            if legal.binding_date is None:
                self._add_missing_attr_name(_("Binding date"))

            if legal.laws.count() == 0:
                self._add_missing_attr_name(_("Laws"))

            if legal.process_type is None:
                self._add_missing_attr_name(_("Process type"))
        except AttributeError:
            self._add_missing_attr_name(_("Legal data"))

    def _check_compensations(self):
        """ Checks for compensation, deduction or payment

        Returns:

        """
        comps = self.obj.compensations.filter(
            deleted=None
        )
        c_comps = comps.count()
        c_pays = self.obj.payments.count()
        c_deducs = self.obj.deductions.count()
        c_all = c_comps + c_pays + c_deducs
        if c_all == 0:
            self.messages.append(
                _("No compensation of any type found (Compensation, Payment, Deduction)")
            )
