import os

from systools import sys_parsefilepath

from application.repository.pos import CommonPOSRepository
from application.util import LoggerUtil


logger = LoggerUtil.get_logger_name(__name__)


class JanitorService(object):

    def __init__(
        self,                       # type: JanitorService
        common_pos_repository,      # type: CommonPOSRepository
    ):
        # type: (...) -> None

        super(JanitorService, self).__init__()

        self.common_pos_repository = common_pos_repository

        self.data_path = sys_parsefilepath("{HVDATADIR}")
        self.internal_order_path = os.path.join(self.data_path, "bundles", "internalorder")

    def clean_internal_order_file(
        self,       # type: JanitorService
    ):
        # type: (...) -> None

        try:
            if os.path.exists(self.internal_order_path) and os.path.isfile(self.internal_order_path):
                os.remove(self.internal_order_path)
                self.common_pos_repository.restart_hv(
                    event_type="POS_UPDATER_JANITOR",
                )
        except (Exception,):
            logger.exception("Error cleaning internal order file")
