from django.core.exceptions import ValidationError
from .helpers import get_all_platform_names_from_DB
from .models import Platform

# Create your validators here.
# https://docs.djangoproject.com/en/2.0/ref/validators/

# The below defined validators are mainly utilized by the application (programmatic) forms (see forms.py).
# The validators of a form field are run in the context of run_validators() method when the clean() method of the field is called. If the field value is empty, i.e. None, '', [], () or {}, run_validators() will NOT run the validators (see https://docs.djangoproject.com/en/2.0/_modules/django/forms/fields/). Therefore, it can be safely assumed that the single arguments of the following validators are NEVER empty.

def platform_is_SLUB_or_exists_in_DB(platform):
    """
    Ensure that the platform (name) is selected among the ones provided in the corresponding form drop-down list (this list includes SLUB
    as well as the platforms that exist in usermergeDB). If it is not (e.g. it is misedited via JavaScript), raise a ValidationError
    exception with the appropriate error message and code.
    """
    if platform != 'SLUB' and not platform in get_all_platform_names_from_DB():
        raise ValidationError('Το όνομα πλατφόρμας πρέπει να επιλέγεται μεταξύ εκείνων\n'
                              'που παρέχονται στην αντίστοιχη αναπτυσσόμενη λίστα!', code = 'platform_is_not_SLUB_and_does_not_exist_in_DB')

def platform_exists_in_DB(platform):
    """
    Ensure that the platform (name) is selected among the ones provided in the corresponding form drop-down list (this list includes the
    platforms that exist in usermergeDB). If it is not (e.g. it is misedited via JavaScript), raise a ValidationError exception with the
    appropriate error message and code.
    """
    if not platform in get_all_platform_names_from_DB():
        raise ValidationError('Το όνομα πλατφόρμας πρέπει να επιλέγεται μεταξύ εκείνων\n'
                              'που παρέχονται στην αντίστοιχη αναπτυσσόμενη λίστα!', code = 'platform_does_not_exist_in_DB')

def ece_id_is_not_031YY000(ece_id):
    """
    Ensure that the ece_id (format) is not 031YY000.
    If it is, raise a ValidationError exception with the appropriate error message and code.
    """
    if ece_id[5:] == '000':
        raise ValidationError('Ο αριθμός μητρώου απαγορεύεται να είναι της μορφής 031YY000 (YY: έτος)!', code = 'ece_id_is_031YY000')

def last_login_is_selected_from_provided_list(last_login):
    """
    Ensure that the last login (search period) is selected among the ones provided in the corresponding form drop-down list.
    If it is not (e.g. it is misedited via JavaScript), raise a ValidationError exception with the appropriate error message and code.
    """
    if not last_login in ['last_six_months', 'last_year', 'all_time']:
        raise ValidationError('Η περίοδος αναζήτησης τελευταίας εισόδου πρέπει να επιλέγεται μεταξύ\n'
                              'εκείνων που παρέχονται στην αντίστοιχη αναπτυσσόμενη λίστα!',
                              code = 'last_login_is_not_selected_from_provided_list')