from datetime import timedelta

from mower.utils import config
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.recognize import Scene


class MailSolver(SceneGraphSolver):
    solver_name = "领取邮件"
    solver_max_duration = timedelta(minutes=2)

    def run(self):
        if not config.conf.check_mail_enable:
            return True
        self.read = False
        return super().run()

    def transition(self):
        if self.scene() == Scene.MAIL:
            if not self.read:
                if self.find("mail/complete"):
                    logger.info("没有未领取邮件")
                    self.read = True
                    self.solver_update_before_transition = False
                else:
                    logger.info("收取所有邮件")
                    self.tap("mail/collect")
            elif config.conf.delete_read_mail:
                if self.find("mail/clean"):
                    logger.info("没有邮件需要删除")
                    return True
                else:
                    logger.info("删除已读邮件")
                    self.tap("mail/delete")
            else:
                return True
        else:
            self.scene_graph_step(Scene.MAIL)
