import pytz
from django.db.models import Q
from datetime import datetime, time
from django.db.models import Sum
from rest_framework import serializers
from counter.models import DeviceCount, DeviceInfo
from mosquito.models import MosqPostStatistic, DevicePostStatistic


class DeviceCountSerializer(serializers.ModelSerializer):
    mosq_count = serializers.SerializerMethodField()
    signal = serializers.SerializerMethodField()
    energy = serializers.SerializerMethodField()
    calc_time = serializers.SerializerMethodField()

    class Meta:
        model = DeviceCount
        fields = [
            'id',
            'device_id',
            'signal',
            'mosq_count',
            'energy',
            'calc_time',
            'longitude',
            'latitude'
        ]

    def get_mosq_count(self, obj):
        return obj.count

    def get_signal(self, obj):
        return obj.csq

    def get_energy(self, obj):
        return obj.vol

    def get_calc_time(self, obj):
        return obj.data_time.strftime('%Y-%m-%d %H:%M:%S')


class DeviceInfoSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    count = serializers.SerializerMethodField()
    signal = serializers.SerializerMethodField()
    energy = serializers.SerializerMethodField()
    coordinate = serializers.SerializerMethodField()
    time = serializers.SerializerMethodField()

    class Meta:
        model = DeviceInfo
        fields = [
            'device_id',
            'device_name',
            'status',
            'count',
            'signal',
            'energy',
            'coordinate',
            'time',
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.latest = None

    def get_status(self, obj):
        return obj.online

    def get_count(self, obj):
        try:
            self.latest = DeviceCount.objects.filter(device_id=obj.device_id).order_by('-data_time')[0]
        except IndexError:
            self.latest = None
        if self.latest:
            return self.latest.count
        return 0

    def get_signal(self, obj):
        if self.latest:
            return self.latest.csq
        return 0

    def get_energy(self, obj):
        if self.latest:
            return self.latest.vol
        return 0

    def get_coordinate(self, obj):
        query = DeviceCount.objects.filter(~Q(longitude=0), device_id=obj.device_id).order_by('-data_time')
        if query:
            q = query[0]
            return [q.longitude, q.latitude]
        return None

    def get_time(self, obj):
        if self.latest:
            return self.latest.data_time.strftime('%Y-%m-%d %H:%M:%S')
        return None


class LogHistorySerializer(serializers.ModelSerializer):

    class Meta:
        model = MosqPostStatistic
        fields = [
            'date',
            'total',
            'increment',
        ]


class DeviceLogHistorySerializer(serializers.ModelSerializer):

    class Meta:
        model = DevicePostStatistic
        fields = [
            'device_id',
            'total',
            'date'
        ]
