from django.core.validators import RegexValidator
from django.utils.translation import gettext_lazy

from django.db import models
from core.fields import IntegerField
from core.validators import domain_or_ip_validator
from incident_export.enums import ReceiverType, SeverityLevel, SysLogSendProtocol


class Receiver(models.Model):
    """ Model for storing export receiver """

    type = models.CharField(max_length=64,
                            choices=ReceiverType.choices,
                            verbose_name=gettext_lazy("Type"),
                            help_text=gettext_lazy("Type of receiver"),
                            default=ReceiverType.SYSLOG)
    cef_format = models.BooleanField(default=True,
                                     verbose_name=gettext_lazy("CEF formatting"),
                                     help_text=gettext_lazy("Format using CEF standard"))
    message_filter = models.CharField(max_length=255,
                                      validators=[RegexValidator],
                                      verbose_name=gettext_lazy("Message filter"),
                                      blank=True,
                                      help_text=gettext_lazy("Regular expression for message filtering"))
    message_min_severity = models.IntegerField(verbose_name=gettext_lazy("Message minimum severity level"),
                                               choices=SeverityLevel.choices,
                                               default=SeverityLevel.MINIMAL,
                                               help_text=gettext_lazy("Minimum event severity level to send"))
    export_status = models.BooleanField(verbose_name=gettext_lazy("Event receiver export status. Could be on or off"),
                                        default=True,
                                        help_text=gettext_lazy("Set the event receiver status - on or off"))


class SyslogReceiver(models.Model):
    """ Model for receiver if type = SYSLOG """

    receiver = models.OneToOneField(Receiver, on_delete=models.CASCADE)
    protocol = models.CharField(max_length=32,
                                choices=SysLogSendProtocol.choices,
                                verbose_name=gettext_lazy("Send protocol"),
                                help_text=gettext_lazy("Select send protocol"))
    host = models.CharField(verbose_name=gettext_lazy("Recipient's IP address"),
                            max_length=100,
                            help_text=gettext_lazy("Enter the recipient's IP address"),
                            validators=[domain_or_ip_validator])
    port = IntegerField(min_value=0,
                        max_value=65535,
                        verbose_name=gettext_lazy("Destination port"),
                        help_text=gettext_lazy("Enter destination port"))


class OpcuaReceiver(models.Model):
    """ Model for receiver if type = OPCUA """

    receiver = models.OneToOneField(Receiver, on_delete=models.CASCADE)
    host = models.CharField(verbose_name=gettext_lazy("Recipient's IP address"),
                            max_length=100,
                            help_text=gettext_lazy("Enter the recipient's IP address"),
                            validators=[domain_or_ip_validator])
    port = IntegerField(min_value=0,
                        max_value=65535,
                        verbose_name=gettext_lazy("Destination port"),
                        help_text=gettext_lazy("Enter destination port"))
    # TODO Examine how to handle node number, as it can vary on different servers
    node_number = IntegerField(min_value=1,
                               max_value=254,
                               verbose_name=gettext_lazy("OPC UA node number"),
                               help_text=gettext_lazy("Enter OPC UA node number"))
