# coding=utf-8

from django.db import transaction
from rest_framework.views import APIView
from rest_framework import generics
from rest_framework_jwt.views import VerifyJSONWebToken,RefreshJSONWebToken
from rest_framework.serializers import ValidationError

from utils import response_ok, response_error
from utils.wx.WXBizDataCrypt import WXBizDataCrypt
from utils.permission import IsCustomerUser


from .serializers import *


class CustomerLoginView(APIView):
    serializer_class = CustomerMobileSignSerializer

    def post(self, request, *args, **kwargs):
        ser = self.serializer_class(data=request.data)
        if ser.is_valid():
            return response_ok(ser.validated_data)
        else:
            return response_error('参数错误')


class CustomerRefreshTokenView(RefreshJSONWebToken):
    def post(self, request, *args, **kwargs):
        try:
            ser = self.serializer_class(data=request.data)
            if ser.is_valid(raise_exception=True):
                return response_ok({'token': ser.validated_data['token']})
        except ValidationError as e:
            return response_error(u'登录状态失效，请重新登录[' + e.detail['error'][0] + ']')


class CustomerVerifyTokenView(VerifyJSONWebToken):
    def post(self, request, *args, **kwargs):
        try:
            ser = self.serializer_class(data=request.data)
            if ser.is_valid(raise_exception=True):
                return response_ok({'token': ser.validated_data['token']})
        except ValidationError as e:
            return response_error(u'登录状态失效，请重新登录[' + e.detail['error'][0] + ']')


class WxLoginView(APIView):
    serializer_class = WechatLoginSerializer

    def post(self, request, *args, **kwargs):
        ser = self.serializer_class(data=request.data)
        if ser.is_valid():
            return response_ok(ser.validated_data)
        else:
            return response_error('参数错误')


class WxBindView(APIView):
    serializer_class = WechatBindSerializer

    def post(self, request, *args, **kwargs):
        ser = self.serializer_class(data=request.data)
        if ser.is_valid():
            return response_ok(ser.validated_data)
        else:
            return response_error('参数错误')


class SetUserInfoView(APIView):
    permission_classes = [IsCustomerUser, ]

    def post(self, request, *args, **kwargs):
        name = request.data['name']
        face = request.data['face']

        with transaction.atomic():
            customer = request.customer
            customer.name = name
            customer.face = face
            customer.save()
            tenant_log(customer.user, BizLog.INSERT, u'客户设置信息，id=%d' % customer.id)
        return response_ok()



class CustomerInfoView(generics.RetrieveAPIView):
    permission_classes = [IsCustomerUser, ]
    serializer_class = CustomerSerializer

    def get_object(self):
        obj = self.request.customer
        return obj

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return response_ok(serializer.data)

