# coding=utf-8
import datetime
from rest_framework import serializers
from django.conf import settings
from apps.agent.models import GeneralAgent,Agent,Store
from utils.booleancharfield import BooleanCharField
from utils.exceptions import CustomError

class GeneralAgentSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    class Meta:
        model = GeneralAgent
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(GeneralAgentSerializer, self).create(validated_data)
        return instance


class AgentSerializer(serializers.ModelSerializer):
    general_agent_text = serializers.CharField(source='general_agent.name', read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)

    class Meta:
        model = Agent
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        validated_data['general_agent'] = self.context['request'].user.general_agent
        instance = super(AgentSerializer, self).create(validated_data)
        return instance


class GeneralAgentComboboxSerializer(serializers.ModelSerializer):
    class Meta:
        model = GeneralAgent
        fields = ('id', 'name',)


class AgentComboboxSerializer(serializers.ModelSerializer):
    class Meta:
        model = Agent
        fields = ('id', 'name', 'general_agent_id')

class StoreComboboxSerializer(serializers.ModelSerializer):
    class Meta:
        model = Store
        fields = ('id', 'name', 'agent_id')

class StoreSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    check_user_text = serializers.CharField(source='check_user.name', read_only=True)
    agent_text = serializers.CharField(source='agent.name', read_only=True)
    enable_text = BooleanCharField(source='enable', read_only=True)
    create_time_f = serializers.DateTimeField(source='create_time', format=settings.SHORT_DATETIME_FORMAT,
                                              read_only=True)
    check_time_f = serializers.DateTimeField(source='check_time', format=settings.SHORT_DATETIME_FORMAT, read_only=True)

    class Meta:
        model = Store
        fields = '__all__'

    def create(self, validated_data):
        agent = self.context['request'].user.agent
        if not agent:
            raise CustomError('当前账号未绑定代理商，禁止添加门店！')
        validated_data['create_user'] = self.context['request'].user
        validated_data['agent'] = self.context['request'].user.agent
        validated_data['end_date'] = (datetime.datetime.now() + datetime.timedelta(days=365)).strftime('%Y-%m-%d')
        instance = super(StoreSerializer, self).create(validated_data)
        return instance
