# coding=utf-8

import os
import logging
import datetime
import glob

from subliminal import region as subliminal_cache_region

from app.get_args import args
from app.jobs_queue import jobs_queue


def cache_maintenance(job_id=None, wait_for_completion=False):
    if not job_id:
        jobs_queue.add_job_from_function("Performing Cache Maintenance", is_progress=False,
                                         wait_for_completion=wait_for_completion)
        return

    main_cache_validity = 14  # days
    pack_cache_validity = 4  # days

    logging.debug("BAZARR Running cache maintenance")
    now = datetime.datetime.now()

    def remove_expired(path, expiry):
        mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path))
        if mtime + datetime.timedelta(days=expiry) < now:
            try:
                os.remove(path)
            except (IOError, OSError):
                logging.debug("Couldn't remove cache file: %s", os.path.basename(path))

    # main cache
    for fn in subliminal_cache_region.backend.all_filenames:
        remove_expired(fn, main_cache_validity)

    # archive cache
    for fn in glob.iglob(os.path.join(args.config_dir, "*.archive")):
        remove_expired(fn, pack_cache_validity)

    jobs_queue.update_job_name(job_id=job_id, new_job_name="Performed Cache Maintenance")
