#! /usr/bin/env python3

"""Monitor sound files; For each program, check for:

- new files;
- deleted files;
- differences between files and sound;
- quality of the files;

It tries to parse the file name to get the date of the diffusion of an
episode and associate the file with it; We use the following format:
    yyyymmdd[_n][_][name]

Where:
    'yyyy' the year of the episode's diffusion;
    'mm' the month of the episode's diffusion;
    'dd' the day of the episode's diffusion;
    'n' the number of the episode (if multiple episodes);
    'name' the title of the sound;


To check quality of files, call the command sound_quality_check using the
parameters given by the setting SOUND_QUALITY. This script requires
Sox (and soxi).
"""
import logging
from argparse import RawTextHelpFormatter

from django.core.management.base import BaseCommand

from aircox.controllers.sound_monitor import SoundMonitor

logger = logging.getLogger("aircox.commands")


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.formatter_class = RawTextHelpFormatter
        parser.add_argument(
            "-q",
            "--quality_check",
            action="store_true",
            help="Enable quality check using sound_quality_check on all " "sounds marqued as not good",
        )
        parser.add_argument(
            "-s",
            "--scan",
            action="store_true",
            help="Scan programs directories for changes, plus check for a "
            " matching diffusion on sounds that have not been yet assigned",
        )
        parser.add_argument(
            "-m",
            "--monitor",
            action="store_true",
            help="Run in monitor mode, watch for modification in the " "filesystem and react in consequence",
        )

    def handle(self, *args, **options):
        monitor = SoundMonitor()
        if options.get("scan"):
            monitor.scan()
        if options.get("monitor"):
            monitor.monitor()
