from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.translation import gettext_lazy

from devices.enums import EndpointRotationType, EndpointRotationTime
from devices.models.device import Device


def _get_default_white_list():
    """Django raise a warning if this list is set as list"""
    return [
        '%HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRoot%',
        '%HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir%'
    ]


class EndpointModel(Device):
    """Endpoint device model"""

    # White List
    whitelist_enabled = models.BooleanField(gettext_lazy('Enable white list'), default=False)
    whitelist_admin = models.BooleanField(gettext_lazy('Local admin ignores white list'), default=True)
    white_list_paths = models.JSONField(
        gettext_lazy('White List paths'),
        max_length=1024,
        help_text=gettext_lazy('Paths for white list'),
        default=_get_default_white_list, null=True, blank=True
    )

    # Integrity control
    integrity_control_enabled = models.BooleanField(gettext_lazy('Enable integrity control'), default=False)
    integrity_control_timeout = models.IntegerField(
        gettext_lazy('Event creation timeout'),
        help_text=gettext_lazy('How often we can get integrity control events. Value in seconds'),
        validators=[MinValueValidator(0), MaxValueValidator(60 * 60 * 24)],  # 60*60*24 = 1 day
        default=3
    )
    scan_paths = models.JSONField(
        gettext_lazy('Scan paths for integrity control'),
        max_length=1024,
        help_text=gettext_lazy('Folder for integrity control'),
        null=True, blank=True
    )

    # Antivirus
    antivirus_enabled = models.BooleanField(
        gettext_lazy('Enable antivirus'),
        help_text=gettext_lazy('Detailed Antivirus control should be configured on host machine'),
        default=False)
    antivirus_remove_infected_files = models.BooleanField(gettext_lazy('Remove infected files'), default=False)
    antivirus_paths = models.JSONField(
        gettext_lazy('Antivirus paths'),
        max_length=1024,
        help_text=gettext_lazy('Path for scanning'),
        default=list, null=True, blank=True
    )
    antivirus_start_scan = models.BooleanField(verbose_name=gettext_lazy('Initiate antivirus scan'), default=False)
    antivirus_update_db = models.BooleanField(verbose_name=gettext_lazy("Initiate antivirus db update"), default=True)

    # Access device USB, CD/DVD
    device_control_enabled = models.BooleanField(gettext_lazy('Enable device control'), default=False)
    prohibit_cd_access = models.BooleanField(
        gettext_lazy('Prohibit CD/DVD access'),
        help_text=gettext_lazy('To apply this change you need to restart host machine locally'),
        default=False)
    usb_control_enabled = models.BooleanField(
        verbose_name=gettext_lazy('Enable USB control'),
        help_text=gettext_lazy('To apply this change you need to restart host machine locally'),
        default=False)

    #  Service data
    settings_changed = models.BooleanField(
        gettext_lazy('Flag to mark if Endpoint logs has been changed'),
        help_text=gettext_lazy('Check if you want to upload settings to Endpoint'),
        default=True,
    )
    incorrect_settings = models.BooleanField(
        gettext_lazy('Flag to mark if config in current console has errors'),
        help_text=gettext_lazy('Checked if config in console for Endpoint contains errors'),
        default=False,
    )
    config_errors = models.JSONField(
        gettext_lazy('Endpoint config errors'),
        help_text=gettext_lazy('Full list of Endpoint config errors'),
        null=True,
        blank=True
    )
    request_config = models.BooleanField(
        gettext_lazy('Flag to show if user wants to upload config from endpoint'),
        help_text=gettext_lazy('If set to True, uploads config from endpoint, saves it'),
        default=True,
    )

    is_requested_config_correct = models.BooleanField(
        gettext_lazy('Flag to show if config, downloaded from endpoint, has correct format'),
        help_text=gettext_lazy('If set to False, means that last attempt to download and set up config from Endpoint '
                               'has failed'),
        default=True,
    )
    # Rotation fields
    event_rotation_type = models.IntegerField(choices=EndpointRotationType.choices,
                                              verbose_name=gettext_lazy("Event rotation type"),
                                              help_text=gettext_lazy("Select the rotation type"),
                                              default=1)

    event_rotation_size = models.IntegerField(verbose_name=gettext_lazy("Event rotation size"),
                                              help_text=gettext_lazy("Select the rotation size in KB"),
                                              default=100,
                                              validators=[MinValueValidator(100)])

    event_rotation_period = models.IntegerField(choices=EndpointRotationTime.choices,
                                                verbose_name=gettext_lazy("Event rotation period"),
                                                help_text=gettext_lazy("Select the rotation period"),
                                                default=1)

    event_rotation_time = models.CharField(max_length=1024,
                                           verbose_name=gettext_lazy("Event rotation time"),
                                           help_text=gettext_lazy("Select the event rotation time"),
                                           default='00:00:00', null=True, blank=True)
