from rest_framework import serializers

from apps.am.models import Area
from apps.hrm.serializers import EmployeeSimpleSerializer, EmployeeShortSerializer
from apps.third.models import BltBind, TDevice, Tlog, DoorAuth
from apps.utils.serializers import CustomModelSerializer
from django.core.cache import cache


class PicSerializer(serializers.Serializer):
    path = serializers.CharField(label='图片oss地址')


class TDeviceSerializer(CustomModelSerializer):
    employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)
    area_name = serializers.CharField(source='area.name', read_only=True)
    mtask_ = serializers.SerializerMethodField()

    class Meta:
        model = TDevice
        fields = '__all__'

    def get_mtask_(self, obj):
        return cache.get('vchannel_' + obj.code, {})


class TDeviceUpdateSerializer(CustomModelSerializer):
    class Meta:
        model = TDevice
        fields = ['is_clock', 'access_list']

    def update(self, instance, validated_data):
        if validated_data.get('access_list', []):
            validated_data['access_list'] = [
                'employee', 'remployee', 'visitor']
        return super().update(instance, validated_data)


class TDeviceSimpleSerializer(CustomModelSerializer):
    class Meta:
        model = TDevice
        fields = ['id', 'name', 'code']


class RequestCommonSerializer(serializers.Serializer):
    method_choice = (
        ('post', 'post'),
        ('get', 'get'),
        ('put', 'put'),
        ('delete', 'delete')
    )
    url = serializers.CharField(label='请求地址', required=False)
    method = serializers.ChoiceField(
        label='请求方法', choices=method_choice, required=False)
    params = serializers.JSONField(
        label='请求参数', required=False, allow_null=True)
    json = serializers.JSONField(
        label='请求body(json格式)', required=False, allow_null=True)
    code = serializers.CharField(
        label='请求短标识', required=False, allow_null=True)


class BindAreaSerializer(serializers.Serializer):
    ids = serializers.ListField(child=serializers.CharField(), label='设备ID列表')
    area = serializers.PrimaryKeyRelatedField(queryset=Area.objects.all(),
                                              label="区域ID")


class LabelLocationSerializer(CustomModelSerializer):

    class Meta:
        model = TDevice
        fields = ['location', 'area', 'areas']


class BindAreasSerializer(serializers.Serializer):
    type = serializers.CharField(label='设备类型')
    codes = serializers.ListField(child=serializers.CharField(), label='标识列表')
    areas = serializers.PrimaryKeyRelatedField(queryset=Area.objects.all(),
                                               label="区域ID列表",
                                               required=False, many=True)


class TlogSerializer(CustomModelSerializer):
    class Meta:
        model = Tlog
        fields = '__all__'


class BltSerializer(serializers.ModelSerializer):
    employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)

    class Meta:
        model = TDevice
        fields = ['code', 'obj_cate', 'employee', 'employee_', 'id']


class BltQuerySerializer(serializers.ModelSerializer):
    depts = serializers.ListField(
        child=serializers.CharField(), label="部门ID列表", required=False)

    class Meta:
        model = TDevice
        fields = ['area', 'depts']


class BltBindCreateSerializer(serializers.ModelSerializer):
    # blt = serializers.PrimaryKeyRelatedField(queryset=TDevice.objects.filter(type=TDevice.DEVICE_BLT))
    code = serializers.CharField(label='标签mac地址')

    class Meta:
        model = BltBind
        fields = ['type', 'code', 'employee']


class BltCreatesSerializer(serializers.Serializer):
    macs = serializers.ListField(
        child=serializers.CharField(), label='标签mac列表')


class DoorAuthSerializer(CustomModelSerializer):
    employee_ = EmployeeShortSerializer(source='employee', read_only=True)
    dchannel_ = TDeviceSimpleSerializer(source='dchannel', read_only=True)

    class Meta:
        model = DoorAuth
        fields = '__all__'
