#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.views import View

from Controller.CheckUserData import DataValid
from Object.RedisObject import RedisObject
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Service.CommonService import CommonService


class VerifyCodeView(View):
    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.GET
        return self.validate(request_dict, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.POST
        return self.validate(request_dict, operation)

    def validate(self, request_dict, operation):

        response = ResponseObject()

        if operation == 'forget':
            return self.verify_forget(request_dict, response)
        elif operation == 'register':
            return self.verify_register(request_dict, response)
        elif operation == 'image':
            return self.verify_image(request_dict, response)
        else:
            return response.json(404)

    def verify_forget(self, request_dict, response):
        email = request_dict.get('email', None)
        phone = request_dict.get('phone', None)
        authcode = request_dict.get('authcode', None)
        key = '_forgetPwdResetCode'

        if authcode is None:
            return response.json(444)

        authcode = CommonService.decode_data(authcode)

        if email is not None:
            email = email.strip()
            return self.email_validate(key, email, authcode, response)
        elif phone is not None:
            phone = phone.strip()
            return self.phone_validate(key, phone, authcode, response)
        else:
            return response.json(444)

    def verify_register(self, request_dict, response):
        email = request_dict.get('email', None)
        phone = request_dict.get('phone', None)
        authcode = request_dict.get('authcode', None)
        key = '_identifyingCode'
        if authcode is None:
            return response.json(444)

        authcode = CommonService.decode_data(authcode)

        if email is not None:
            email = email.strip()
            return self.email_validate(key, email, authcode, response)
        elif phone is not None:
            phone = phone.strip()
            return self.phone_validate(key, phone, authcode, response)
        else:
            return response.json(444)

    def verify_image(self, request_dict, response):
        imageCodeId = request_dict.get('imageCodeId', None)
        authcode = request_dict.get('authcode', None)

        if imageCodeId and authcode:

            authcode = CommonService.decode_data(authcode)
            image_code_key = 'image_code_' + imageCodeId
            redisObj = RedisObject(db=6)
            # redis里面的验证码
            redis_image_code = redisObj.get_data(key=image_code_key)
            # 验证用户输入的验证码和redis中的验证码
            if redis_image_code is False or authcode.lower() != redis_image_code.lower():
                return response.json(121)
            else:
                return response.json(0)
        else:
            return response.json(444)

    def email_validate(self, key, email, authcode, response):
        authcode.strip()
        data_valid = DataValid()
        if data_valid.email_validate(email) is not True:
            return response.json(105)
        reds = RedisObject()
        resetCode = reds.get_data(key=email + key)
        if resetCode is False:
            return response.json(120)
        if authcode != resetCode:
            return response.json(121)
        return response.json(0)

    def phone_validate(self, key, phone, authcode, response):
        authcode.strip()
        data_valid = DataValid()
        if data_valid.mobile_validate(phone) is not True:
            return response.json(100)
        reds = RedisObject()
        resetCode = reds.get_data(key=phone + key)
        if resetCode is False:
            return response.json(120)
        print(resetCode)
        if authcode != resetCode:
            return response.json(121)
        return response.json(0)