from apps.am.serializers import AreaSerializer
from apps.ecm.models import AlgoChannel, EventCate, Remind, NotifySetting, Event
from apps.third.serializers import TDeviceSimpleSerializer
from apps.third.models import TDevice
from apps.utils.fields import MyFilePathField
from apps.utils.serializers import CustomModelSerializer
from rest_framework import serializers
from apps.system.serializers import UserSimpleSerializer
from apps.utils.constants import EXCLUDE_FIELDS
from rest_framework.exceptions import ParseError
from apps.hrm.serializers import EmployeeSimpleSerializer


class EventCateCreateUpdateSerializer(CustomModelSerializer):
    class Meta:
        model = EventCate
        exclude = EXCLUDE_FIELDS


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


class EventCateListSerializer(CustomModelSerializer):
    class Meta:
        model = EventCate
        fields = '__all__'


class EventCateUpdateSerializer(CustomModelSerializer):
    class Meta:
        model = EventCate
        fields = ['speaker_on', 'speakers', 'filter_area_level',
                  'voice_person', 'handle_minute', 'voice_num', 'priority', 'same_allow_minute', 'origin_level', 'is_auto_escalate']


class NotifySettingsSerializer(CustomModelSerializer):
    event_cate_name = serializers.CharField(source='event_cate.name', read_only=True)
    post_name = serializers.CharField(source='post.name', read_only=True)
    user_name = serializers.CharField(source='user.name', read_only=True)

    class Meta:
        model = NotifySetting
        fields = '__all__'

    def check(self, validated_data):
        post = validated_data.get('post', None)
        user = validated_data.get('user', None)
        variable = validated_data.get('variable', None)
        if post:
            validated_data['obj_cate'] = 'post'
            validated_data['user'] = None
            validated_data['variable'] = None
        elif user:
            validated_data['obj_cate'] = 'user'
            validated_data['post'] = None
            validated_data['variable'] = None
        elif variable:
            validated_data['obj_cate'] = 'variable'
            validated_data['post'] = None
            validated_data['user'] = None
        else:
            raise ParseError('请指定岗位或用户或变量')
        return validated_data

    def create(self, validated_data):
        validated_data = self.check(validated_data)
        return super().create(validated_data)

    def update(self, instance, validated_data):
        validated_data = self.check(validated_data)
        return super().update(instance, validated_data)


class EventSerializer(serializers.ModelSerializer):
    area_ = AreaSerializer(source='area', read_only=True)
    area_name = serializers.CharField(source='area.name', read_only=True)
    cates_ = EventCateSimpleSerializer(source='cates', read_only=True, many=True)
    employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)
    employee_name = serializers.CharField(source='employee.name', read_only=True)
    operation_name = serializers.CharField(source='opl.operation.name', read_only=True)
    vchannel_ = TDeviceSimpleSerializer(source='vchannel', read_only=True)
    handle_user_name = serializers.CharField(source='handle_user.name', read_only=True)
    face_img_f = MyFilePathField(source='face_img', read_only=True)
    global_img_f = MyFilePathField(source='global_img', read_only=True)
    voice_f = MyFilePathField(source='voice', read_only=True)

    class Meta:
        model = Event
        fields = '__all__'


class EventDetailSerializer(serializers.ModelSerializer):
    area_ = AreaSerializer(source='area', read_only=True)
    cates_ = EventCateSimpleSerializer(source='cates', read_only=True, many=True)
    employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)
    operation_name = serializers.CharField(source='opl.operation.name', read_only=True)
    vchannel_ = TDeviceSimpleSerializer(source='vchannel', read_only=True)
    handle_user_name = serializers.CharField(source='handle_user.name', read_only=True)
    face_img_f = MyFilePathField(source='face_img', read_only=True)
    global_img_f = MyFilePathField(source='global_img', read_only=True)
    voice_f = MyFilePathField(source='voice', read_only=True)

    class Meta:
        model = Event
        fields = '__all__'


class EventSimpleSerializer(serializers.ModelSerializer):
    cates_ = EventCateSimpleSerializer(source='cates', read_only=True, many=True)

    class Meta:
        model = Event
        fields = ['id', 'cates_', 'voice_msg', 'create_time', 'handle_user']


class EventHandleSerializer(CustomModelSerializer):
    class Meta:
        model = Event
        fields = ['handle_desc', 'mark']


class RemindSerializer(CustomModelSerializer):
    event_ = EventSimpleSerializer(source='event', read_only=True)
    recipient_ = UserSimpleSerializer(source='recipient', read_only=True)

    class Meta:
        model = Remind
        fields = '__all__'


class AlgoChannelCreateSerializer(CustomModelSerializer):
    class Meta:
        model = AlgoChannel
        fields = ['algo', 'vchannel', 'always_on']


class AlgoChannelCreatesSerializer(CustomModelSerializer):
    vchannels = serializers.PrimaryKeyRelatedField(queryset=TDevice.objects.all(), many=True, label="视频通道列表")

    class Meta:
        model = AlgoChannel
        fields = ['algo', 'vchannels']


class AlgoChannelSerializer(CustomModelSerializer):
    algo_ = EventCateSimpleSerializer(source='algo', read_only=True)
    vchannel_ = TDeviceSimpleSerializer(source='vchannel', read_only=True)

    class Meta:
        model = AlgoChannel
        fields = '__all__'


class EventAggSerializer(serializers.Serializer):
    start_create = serializers.DateField(required=False, allow_null=True)
    end_create = serializers.DateField(required=False, allow_null=True)
    is_handled = serializers.BooleanField(required=False)
    completed = serializers.BooleanField(default=False)
