from django.utils.translation import gettext_lazy
from rest_framework import serializers

from assets.models.assets import Asset
from dashboard.models import DashboardLayout
from incident.models import Incident, IncidentCategory
from networkmap.api import DANGER_STATUSES


class DashboardSerializer(serializers.ModelSerializer):
    class Meta:
        model = DashboardLayout
        fields = '__all__'


class TopIdsEventsSerializer(serializers.Serializer):
    top_ids_events = serializers.ListField(label=gettext_lazy("List of top 10 IDS events"))


class SystemInfoSerializer(serializers.Serializer):
    """ System information widget data serializer """
    machine_name = serializers.CharField(label=gettext_lazy('Machine name'))
    operating_system = serializers.CharField(label=gettext_lazy('OS'))
    cpu_model = serializers.CharField(label=gettext_lazy('CPU model name'))
    total_cpu = serializers.FloatField(label=gettext_lazy('Total CPU usage'))
    machine_uptime = serializers.CharField(label=gettext_lazy('Current machine work time'))
    server_time = serializers.CharField(label=gettext_lazy('Server time'))
    mem_total = serializers.IntegerField(label=gettext_lazy('Total memory (MB)'))
    mem_used = serializers.IntegerField(label=gettext_lazy('Used memory (MB)'))
    disk_total = serializers.IntegerField(label=gettext_lazy('Disk total space (GB)'))
    disk_free = serializers.IntegerField(label=gettext_lazy('Disk free space (GB)'))
    disk_used = serializers.IntegerField(label=gettext_lazy('Disk used space (GB)'))
    console_version = serializers.CharField(label=gettext_lazy('Console version'))
    description = serializers.CharField(label=gettext_lazy('Machine description'))
    lc_info = serializers.CharField(label=gettext_lazy('License'))


class SerivcesWidgetSerializer(serializers.Serializer):
    user = serializers.CharField(label=gettext_lazy('User'))
    command = serializers.CharField(label=gettext_lazy('Command'))
    cpu = serializers.CharField(label=gettext_lazy('CPU (%)'))


class OpenedIncidentsSerializer(serializers.Serializer):
    inc_count = serializers.IntegerField()


class IncidentsByImportanceSerializer(serializers.Serializer):
    importance_none = serializers.IntegerField(label=gettext_lazy('Incidents with None importance'))
    importance_info = serializers.IntegerField(label=gettext_lazy('Incidents with Info importance'))
    importance_low = serializers.IntegerField(label=gettext_lazy('Incidents with Low importance'))
    importance_medium = serializers.IntegerField(label=gettext_lazy('Incidents with Medium importance'))
    importance_high = serializers.IntegerField(label=gettext_lazy('Incidents with High importance'))
    importance_critical = serializers.IntegerField(label=gettext_lazy('Incidents with Critical importance'))


class IncidentsByCategorySerializer(serializers.ModelSerializer):
    inc_count = serializers.SerializerMethodField('get_inc_count')

    def get_inc_count(self, obj):
        return Incident.objects.filter(category=obj.pk).count()

    class Meta:
        model = IncidentCategory
        fields = ['name', 'inc_count']


class AssetsByIncidentsSerializer(serializers.ModelSerializer):
    """ Serializer for assets by incs widget """
    inc_count = serializers.SerializerMethodField('get_inc_count')

    def get_inc_count(self, obj):
        """ Method for counting incidents of the asset, excluding incs with RESOLVED status
        :param obj: asset object
        :return: sum of asset incidents
        """
        asset_incs = Asset.objects.get(pk=obj.pk).incidents
        return asset_incs.filter(status__in=DANGER_STATUSES).count()

    class Meta:
        model = Asset
        fields = ['name', 'inc_count']
