from rest_framework import serializers
from ....models import *
from ....funcs import fullname_for_user


class Personal_change_mailing_status_Serializer(serializers.Serializer):
    mailing_ID = serializers.IntegerField()
    mailing_status = serializers.BooleanField()


class User_sync_Serializer(serializers.ModelSerializer):
    id_1s = serializers.SerializerMethodField()

    def get_id_1s(self, obj):
        return obj.user_profile.id_1s

    class Meta:
        model = User
        fields = (
            'id_1s',
        )


class Profile_list_Serializer(serializers.ModelSerializer):
    from GeneralApp.api.v1.communications.communications_api_serializers import Communications_create_Serializer

    office_name = serializers.SerializerMethodField(required=False)
    company_name = serializers.SerializerMethodField(required=False)
    manager_name = serializers.SerializerMethodField(required=False)
    company_client_type = serializers.SerializerMethodField(required=False)

    communications = Communications_create_Serializer(many=True)

    def get_company_client_type(self, obj):
        try:
            if obj and obj.company_obj:
                return obj.company_obj.client_type
            else:
                return None
        except:
            return None

    def get_office_name(self, obj):
        try:
            if obj and obj.office:
                return obj.office.name
        except:
            return None

        return None

    def get_company_name(self, obj):
        try:
            if obj and obj.company_obj:
                return obj.company_obj.name
        except:
            return None

        return None

    def get_manager_name(self, obj):
        try:
            if obj and obj.company_obj and obj.company_obj.manager_obj:
                return '{0} {1}'.format(obj.company_obj.manager_obj.last_name, obj.company_obj.manager_obj.first_name)
        except:
            return None

        return None

    class Meta:
        model = UserProfile
        fields = (
            'id',
            'enable',
            'company_obj', 'company_name',
            'company_position', 'company_client_type',
            'phone',
            'delivery_address',
            'office', 'office_name',
            'departament',
            'document_sign_person',
            'work_start_D',
            'work_finish_D',
            'birthdate',
            'comment',
            'communications',
            'priority_connect_type',
            'modifiedDT',
            'mailing_sets',
            'manager_name'
        )


class User_list_Serializer(serializers.ModelSerializer):
    full_name = serializers.SerializerMethodField('get_full_name_user', required=False)

    # user_profile = Profile_list_Serializer()

    def get_full_name_user(self, obj):
        name = fullname_for_user(obj)
        if not name:
            name = obj.email
        return name

    class Meta:
        model = User
        fields = (
            'id', 'full_name'
        )


class Personal_list_Serializer(User_list_Serializer):
    user_profile = Profile_list_Serializer()

    class Meta:
        model = User
        fields = (
            'id', 'full_name', 'first_name', 'last_name', 'is_active', 'is_staff', 'user_profile', 'email'
        )
        extra_kwargs = {
            'email': {'required': 'False'},
            'first_name': {'required': 'False'},
            'last_name': {'required': 'False'},
        }
