from rest_framework import serializers
from django.contrib.auth import get_user_model
from . import models

User = get_user_model()


class CustomUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('email', 'full_name', 'role', 'is_active')


class CustomUserReadSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'email', 'full_name', 'role', 'is_active', 'provider', 'notify')


class CustomUserWriteSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.CustomUser
        fields = ('email', 'full_name', 'role', 'password', 'provider', 'notify')


class CreatorSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.CustomUser
        fields = ('email',)


class CustomUserAdminSerializer(serializers.ModelSerializer):

    password = serializers.CharField(write_only=True, required=False, style={'input_type': 'password'})

    class Meta:
        model = models.CustomUser
        fields = ('id', 'email', 'full_name', 'role', 'password', 'is_active', 'notify')

    def update(self, instance, validated_data):

        for key, value in validated_data.items():
            instance.__dict__[key] = value
        if 'password' in validated_data:
            instance.set_password(validated_data['password'])
        instance.save()
        return instance


class ChangePasswordSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=True)
    password2 = serializers.CharField(write_only=True, required=True)
    old_password = serializers.CharField(write_only=True, required=True)

    class Meta:
        model = models.CustomUser
        fields = ('old_password', 'password', 'password2')

    def validate(self, attrs):
        if attrs['password'] != attrs['password2']:
            raise serializers.ValidationError({"password": "Password fields didn't match."})

        return attrs

    def validate_old_password(self, value):
        user = self.context['request'].user
        if not user.check_password(value):
            raise serializers.ValidationError({"old_password": "Old password is not correct"})
        return value

    def update(self, instance, validated_data):

        instance.set_password(validated_data['password'])
        instance.save()

        return instance


class UpdateUserSerializer(serializers.ModelSerializer):
    email = serializers.EmailField(required=True)

    class Meta:
        model = models.CustomUser
        fields = ('full_name', 'email', 'notify')

    def validate_email(self, value):
        user = self.context['request'].user
        if models.CustomUser.objects.exclude(pk=user.pk).filter(email=value).exists():
            raise serializers.ValidationError({"email": "This email is already in use."})
        return value

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            instance.__dict__[key] = value

        instance.save()

        return instance

