from rest_framework import serializers
from mosquito.models import Mosquito, MosqPost, DeviceTempLog, WeatherLog
from smart.api.serializers import SmartPushListSerializer


class MosqListSerializer(serializers.ModelSerializer):

    class Meta:
        model = Mosquito
        fields = [
            'name',
            'device_id',
            'region'
        ]


class MosqPostListSerializer(serializers.ModelSerializer):
    smart_push = SmartPushListSerializer(read_only=True)
    region = serializers.SerializerMethodField()

    class Meta:
        model = MosqPost
        fields = [
            'id',
            'mosq',
            'led',
            'energy',
            'region',
            'smart_push',
            'time',
        ]

    def get_region(self, obj):
        return obj.mosq.region


class DeviceTempLogSerializer(serializers.ModelSerializer):
    last_time = serializers.SerializerMethodField()
    create_time = serializers.SerializerMethodField()

    class Meta:
        model = DeviceTempLog
        fields = ['device_id', 'temperature', 'humidity', 'last_time', 'create_time']

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

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


class WeatherLogSerializer(serializers.ModelSerializer):
    power = serializers.SerializerMethodField()
    data_time = serializers.SerializerMethodField()
    create_time = serializers.SerializerMethodField()

    class Meta:
        model = WeatherLog
        fields = [
            'data_time',
            'device_id',
            'device_name',
            'env_temp',
            'temperature1', 'temperature2', 'temperature3', 'temperature4', 'temperature5', 'dew_point_temp',
            'env_humi', 'soil_humi1', 'soil_humi2', 'soil_humi3',
            'co2', 'evaporation', 'air_pressure',
            'total_radiation_1_ins', 'scat_radiation_ins', 'direct_radiation_ins', 'total_radiation_2_ins',
            'net_radiation_ins', 'pho_radiation_ins', 'uv_radiation_ins',
            'wind_direction', 'wind_speed_ins', 'wind_speed_2mins', 'wind_speed_10mins',
            'rainfall_interval_cum', 'sunshine_time_interval_cum', 'total_radiation_1_interval_cum',
            'scat_radiation_interval_cum', 'direct_radiation_interval_cum', 'total_radiation_2_interval_cum',
            'net_radiation_interval_cum', 'pho_radiation_interval_cum', 'uv_radiation_interval_cum',
            'rainfall_daily_cum', 'sunshine_time_daily_cum', 'total_radiation_1_daily_cum',
            'scat_radiation_daily_cum', 'direct_radiation_daily_cum', 'total_radiation_2_daily_cum',
            'net_radiation_daily_cum', 'pho_radiation_daily_cum', 'uv_radiation_daily_cum',
            'light_intensity',
            'power',
            'pm25',
            'create_time',
        ]

    def get_power(self, obj):
        # power max 12.6V, min 8.5V
        power = int((float(obj.power) - 8.5) / 4.1 * 100)
        return str(power)
    def get_data_time(self, obj):
        return obj.data_time.strftime('%Y-%m-%d %H:%M:%S')
    def  get_create_time(self, obj):
        return obj.data_time.strftime('%Y-%m-%d %H:%M:%S')
