#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib

from django.contrib.auth.hashers import make_password, check_password
from django.views import View

from Object.uidManageResponseObject import uidManageResponseObject


class TestView(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 = uidManageResponseObject()

        if operation == 'createPassword':
            return self.do_create_password(request_dict, response)
        elif operation == 'checkPassword':
            return self.do_check_password(request_dict, response)
        else:
            return response.json(404)

    def do_create_password(self, request_dict, response):
        password = request_dict.get('password', None)
        print(password)
        result = make_password(password)
        print(result)
        print(check_password(password, result))
        return response.json(0, result)

    def do_check_password(self, request_dict, response):
        password = request_dict.get('password', None)
        password2 = request_dict.get('password2', None)
        print(password)
        print(password2)
        check = check_password(password, password2)

        return response.json(0, check)

    # def make_password(self, password):
    #     md5 = hashlib.md5()
    #     md5.update(str(password).encode('utf-8'))
    #     return md5.hexdigest()
    #
    # def check_password(self, password, encryp):
    #     print(password)
    #     print(encryp)
    #     password1 = self.make_password(password)
    #     print(password1)
    #     return encryp == self.make_password(password)
