# -*- coding:utf-8 -*-

"""
Author: qiaoxinjiu
Create Data: 2021/11/22 10:41
"""
import time
from base_framework.public_tools.my_faker import MyFaker
from base_framework.public_tools.pymailtm.pymailtm import MailTm
from retrying import retry
from base_framework.public_tools import log
from base_framework.public_tools.runner import Runner

magic_faker = MyFaker()


class MailHelper:
    """
    邮件相关操作
    """
    new_mt_mail_obj = None

    def __init__(self):
        self.mail_account = MailTm()
        self.obj_runner = Runner()
        self.obj_log = log.get_logger()

    @retry(stop_max_attempt_number=5, wait_fixed=5000)
    def get_ci_new_mail(self):
        """
            | 功能说明： | 生成随机邮件地址 |
            | 返回参数： | dic | 邮件地址 |
            | 作者信息： | 作者  林于棚   | 修改时间      |
            举例说明：
            | get_ci_new_mail() |
        """
        faker_mail = magic_faker.gen_str(min_chars=8)
        faker_num = magic_faker.create_num(start=1, end=10000)
        return faker_mail.lower()+str(faker_num)+'@citest.com'

    @retry(stop_max_attempt_number=5, wait_fixed=5000)
    def get_new_mail_mt(self):
        """
            | 功能说明： | https://mail.tm/zh/ 生成随机邮件地址 |
            | 返回参数： | dic | 邮件地址 |
            | 作者信息： | 作者  林于棚   | 修改时间      |
            举例说明：
            | get_new_email() |
        """
        self.new_mt_mail_obj = self.mail_account.get_account(password='666666')
        return self.new_mt_mail_obj.address
        # obj_log.info('生成的邮件地址：{}'.format(self.new_mt_mail_obj.address))

    # @retry(stop_max_attempt_number=5, wait_fixed=5000)
    def get_mt_mail_message(self, address=None, password='666666'):
        """
           | 功能说明： | 获取 https://mail.tm/zh/ 生成邮件地址的信息 |
           | 输入参数: | mail_obj       | self.new_mt_mail_obj  |
           | 返回参数： | dic | 邮件地址 |
           | 作者信息： | 作者  林于棚   | 修改时间      |
           举例说明：
           | get_mt_mail_message() |
       """
        mail_message = []
        first_flag = 0
        self.get_new_mail_mt()
        account = self.new_mt_mail_obj
        if account.address != address and address:
            account.address = address
            account.password = password
            account.jwt = MailTm._make_account_request("token", address, password)
            account.auth_headers = {
                    "accept": "application/ld+json",
                    "Content-Type": "application/json",
                    "Authorization": "Bearer {}".format(account.jwt["token"])
                }
        self.obj_log.info('生成的邮件地址：{}'.format(account.address))
        self.obj_log.info('收取邮件中.............')
        for i in range(2):
            first_len = len(mail_message)
            mail_message = account.get_messages()
            last_len = len(mail_message)
            if first_len <= last_len != 0:
                continue
            elif last_len == 0 and first_flag < 1:
                first_flag += 1
                self.obj_log.info('没有收到邮件，重试中..............')
                time.sleep(4)
            else:
                self.obj_log.info('没有收任何到邮件!')
                break
        return mail_message


if __name__ == '__main__':
    obj_mail = MailHelper()
    print(obj_mail.get_ci_new_mail())
    # print(obj_mail.get_mt_mail_message())