import base64
import hashlib
import os
import random
import time

# import rsa as rsa
import yaml

from common.request_util import *
from common.yaml_util import read_yaml


class DubugTalk:
    # 获得随机数
    def get_random_number(self, min, max):
        return str(random.randint(int(min), int(max)))

    def read_yaml(self, key):
        return read_yaml(key)

    # Md5加密
    def md5(self, args):
        # 以指定的编码格式编码字符串
        utf8_str = str(args).encode("utf-8")
        # MD5加密（哈稀算法）
        md5_str = hashlib.md5(utf8_str).hexdigest()
        return md5_str.upper()

    # base64加密
    def bs64(self, args):
        # 以指定的编码格式编码字符串
        utf8_str = str(args).encode("utf-8")
        # base64加密
        ba64_str = base64.b64encode(utf8_str).decode('utf-8')
        return ba64_str.upper()

    # RSA双钥加密
    # # 生成公钥私钥到指定文件
    # def create_key(self):
    #     # 根据密钥长度生成公钥私钥
    #     (public_key, private_key) = rsa.newkeys(1024)
    #     # 报存公钥
    #     with open("public.pem", "w+") as f:
    #         f.write(public_key.save_pkcs1().decode())
    #     # 报存私钥
    #     with open("private.pem", "w+") as f:
    #         f.write(private_key.save_pkcs1().decode())
    # 通过公钥加密
    # def public_key_jiami(self, args):
    #     # 导入密钥
    #     with open("public.pem") as f:
    #         pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
    #     # 加密
    #     byte_str = rsa.encrypt(str(args).encode("utf-8"), pubkey)
    #     # 把二进制转化成字符串格式
    #     miwen = base64.b64encode(byte_str).decode("utf-8")
    #     return miwen
    # 通过私钥解密
    # def private_key_jiemi(self,args):
    #     with open("private.pem") as f:
    #         prikey = rsa.PrivateKey.load_pkcs1(f.read().encode())
    #     # 把字符串转换二进制
    #     byte_key = base64.b64decode(args)
    #     #解密
    #     mingwen = rsa.decrypt(byte_key, prikey).decode()
    #     return mingwen

    # singe签名
    def singes(self, yaml_path):
        last_url = ""
        last_data = {}
        with open(os.getcwd() + yaml_path, encoding="utf-8") as f:
            yaml_value = yaml.load(f, Loader=yaml.FullLoader)
            for caseinfo in yaml_value:
                # print(caseinfo)
                caseinfo_keys = caseinfo.keys()
                # 判断一级关键字是否存在：name,request，validate
                if "request" in caseinfo_keys:
                    # 判断url
                    if "url" in caseinfo['request'].keys():
                        last_url = caseinfo['request']['url']
                        print(caseinfo['request']['url'])
                    # 判断参数
                    req = caseinfo['request']
                    for key, value in req.items():
                        if key in ["params", "data", "json"]:
                            for p_key, p_value in req[key].items():
                                last_data[p_key] = p_value
        last_url = last_url[last_url.index("?") + 1:len(last_url)]
        # 把url字典格式加入到last_data字典格式里面
        lis = last_url.split("&")
        print(lis)
        for a in lis:
            last_data[a[0:a.index('=')]] = a[a.index('=') + 1:len(a)]
        from common.request_util import RequestUtil
        # last_data = RequestUtil(self).replace_value(last_data)
        print(last_data)
