from datetime import datetime

from actions import logger
from api.boss import get_boss_service
from typing import List
from utilfunctions import (
    get_seconds_to_renew_cache,
    reached_seconds_to_renew_cache,
)

own_delivery_men = []
last_cache_time = None


class UserInteractor(object):

    def __init__(
        self,   # type: UserInteractor
    ):
        # type: (...) -> None

        self.boss_service = get_boss_service()

    def get_own_delivery_men(
            self
    ):
        # type: (...) -> List

        global own_delivery_men
        global last_cache_time

        try:
            seconds_to_renew = get_seconds_to_renew_cache(60 * 5)
            if not own_delivery_men or reached_seconds_to_renew_cache(last_cache_time, seconds_to_renew):
                users_list = self.boss_service.get_users_list()
                own_delivery_men = [user for user in users_list if user.level == -1]
                last_cache_time = datetime.now()

            return [delivery_men.long_name or "" for delivery_men in own_delivery_men]
        except (Exception,):
            logger.exception("Error getting own delivers")
            return []
