from prompt_toolkit.validation import Validator, ValidationError


class IntegerValidator(Validator):
    def validate(self, document):
        text = document.text
        if not text.isdigit() and text != '':
            raise ValidationError(message='Please enter a valid integer')


class RangeValidator(Validator):
    def __init__(self, min, max) -> None:
        super().__init__()
        self.min = min
        self.max = max

    def validate(self, document):
        text = document.text
        if (not text.isdigit() or int(text) < self.min or int(text) > self.max) and text != '':
            raise ValidationError(
                message='Please enter a valid integer from range')


class BooleanValidator(Validator):
    def validate(self, document):
        text = document.text
        if text not in ('True', 'False') and text != '':
            raise ValidationError(
                message="Please enter a boolean value (True or False)")


class NonEmptyStringValidator(Validator):
    def validate(self, document):
        text = document.text
        if text == '':
            raise ValidationError(
                message="Please enter a boolean value (True or False)")


class ListValidator(Validator):
    def __init__(self, values) -> None:
        super().__init__()
        self.allowed_values = values

    def validate(self, document):
        text = document.text
        if text not in self.allowed_values and text != '':
            raise ValidationError(
                message='Invalid value selected, try again'
            )


class YesNoValidator(Validator):
    def validate(self, document):
        text = document.text
        if text not in ['yes', 'no'] and text != '':
            raise ValidationError(message='Select yes or no')
