# coding=utf-8
import json
from rest_framework import serializers
from apps.option.models import Option
from django.conf import settings
from utils.exceptions import CustomError
from utils.booleancharfield import BooleanCharField, TimeCharField, DateCharField
from utils.format import strftime,strfdate
from .models import ReportCustomer,NewCustomer,Review

class ReportCustomerSerializer(serializers.ModelSerializer):
    gender_text = serializers.CharField(source='get_gender_display', read_only=True)
    report_status_text = serializers.CharField(source='get_report_status_display', read_only=True)
    source_text = serializers.CharField(source='source.name', read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    check_user_text = serializers.CharField(source='check_user.name', read_only=True)
    project_text = serializers.SerializerMethodField()

    def get_project_text(self, obj):
        project = obj.project.all()
        return ','.join(project.values_list('name', flat=True))

    class Meta:
        model = ReportCustomer
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        store = self.context['request'].user.store
        if not store:
            raise CustomError('当前账号未绑定门店，禁止报备客户！')
        validated_data['store'] = store
        instance = super(ReportCustomerSerializer, self).create(validated_data)
        projects = self.initial_data['project']
        if projects:
            projects = json.loads(projects)
        else:
            raise CustomError(u'请选择项目！')

        for project in projects:
            instance.project.add(project)
        return instance

    def update(self, instance, validated_data):
        instance.project.clear()
        projects = self.initial_data['project']
        if projects:
            projects = json.loads(projects)
        else:
            raise CustomError(u'请选择项目')

        for project in projects:
            instance.project.add(project)
        instance = super(ReportCustomerSerializer, self).update(instance, validated_data)
        return instance


class NewCustomerSerializer(serializers.ModelSerializer):
    track_user_text = serializers.CharField(source='track_user.name', read_only=True)
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    potential_level_text = serializers.CharField(source='potential_level.name', read_only=True)
    stage_progress_text = serializers.CharField(source='stage_progress.name', read_only=True)
    stage_progress_sort = serializers.CharField(source='stage_progress.sort', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    create_time_f = TimeCharField(source='create_time', read_only=True)
    end_time_f = TimeCharField(source='end_time', read_only=True)
    next_time_f = DateCharField(source='next_time', read_only=True)

    name = serializers.CharField(source='report_customer.name', read_only=True)
    tel = serializers.CharField(source='report_customer.tel', read_only=True)
    village = serializers.CharField(source='report_customer.village', read_only=True)
    address = serializers.CharField(source='report_customer.address', read_only=True)
    source_text = serializers.CharField(source='report_customer.source.name', read_only=True)
    gender_text = serializers.SerializerMethodField()
    project_text = serializers.SerializerMethodField()
    last_review = serializers.SerializerMethodField()
    enter_count = serializers.SerializerMethodField()
    last_enter_time = serializers.SerializerMethodField()

    def get_last_enter_time(self, obj):
        review = Review.objects.filter(customer=obj, is_entry=True).order_by('-create_time').first()
        if review:
            return strftime(review.create_time)
        else:
            return ''

    def get_enter_count(self, obj):
        review = Review.objects.filter(customer=obj, is_entry=True)
        return review.count()

    def get_last_review(self, obj):
        review = Review.objects.filter(customer=obj).order_by('-create_time').first()
        if review:
            return review.description
        else:
            return ''

    def get_project_text(self, obj):
        project = obj.report_customer.project.all()
        return ','.join(project.values_list('name', flat=True))

    def get_gender_text(self, obj):
        return settings.GENDER_CHOICES[obj.report_customer.gender - 1][1]

    class Meta:
        model = NewCustomer
        fields =  '__all__'

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

        return instance

    def update(self, instance, validated_data):

        instance = super(NewCustomerSerializer, self).update(instance, validated_data)
        return instance


class ReviewSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    check_user_text = serializers.CharField(source='check_user.name', read_only=True)
    check_status_text = serializers.CharField(source='get_check_status_display', read_only=True)
    create_time_f = TimeCharField(source='create_time', read_only=True)
    check_time_f = TimeCharField(source='check_time', read_only=True)
    next_time_f = TimeCharField(source='next_time', read_only=True)
    is_giveup_text = BooleanCharField(source='is_giveup', read_only=True)

    name = serializers.CharField(source='customer.report_customer.name', read_only=True)
    tel = serializers.CharField(source='customer.report_customer.tel', read_only=True)
    village = serializers.CharField(source='customer.report_customer.village', read_only=True)
    address = serializers.CharField(source='customer.report_customer.address', read_only=True)
    track_user_text = serializers.CharField(source='customer.track_user.name', read_only=True)
    project_text = serializers.SerializerMethodField()

    def get_project_text(self, obj):
        project = obj.customer.report_customer.project.all()
        return ','.join(project.values_list('name', flat=True))

    class Meta:
        model = Review
        fields = '__all__'
