

from rest_framework import serializers
from smartfarming.models.camera import MongoCameraPhoto, MongoCameraData, MongoCameraAccount
from smartfarming.models.device import MongoDevice


class MongoCameraPhotoSerializers(serializers.ModelSerializer):


    class Meta:
        model = MongoCameraPhoto
        fields = "__all__"



class MongoDeviceCameraSerializers(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    sim = serializers.SerializerMethodField()
    d_id = serializers.SerializerMethodField()
    photo_num = serializers.SerializerMethodField()
    camera_playback = serializers.SerializerMethodField()

    def get_photo_num(self, obj):
        return 1 if MongoCameraPhoto.objects.filter(device_id=obj.device_id) else 0

    def get_camera_playback(self, obj):
        camera_data = ""
        try:
            camera_data = MongoCameraData.objects.get(device_id=obj.device_id)
            return camera_data.camera_playback
        except Exception as e:
            return 0

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

    def get_sim(self, obj):
        return obj.simid
    
    def get_d_id(self, obj):
        return obj.id

    class Meta:
        model = MongoDevice
        fields = [
            "device_name",
            "device_id",
            "status",
            "camera_playback",
            "sim",
            "photo_num",
            "d_id",
            "jk_live_img"
        ]