from concurrent.futures import ThreadPoolExecutor as TPE
from queue import Queue
from time import sleep, time

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from .media_info import is_video
from .convert import convert_video


def consume_video_queue(queue: Queue):
    while True:
        filename = queue.get()

        if is_video(filename):
            convert_video(filename)

        else:
            print(filename, 'not video')


class AddedFileHandler(FileSystemEventHandler):
    def __init__(self, queue: Queue):
        super().__init__()
        self.queue = queue

    def on_created(self, event):
        print(event)

        if event.is_directory:
            return

        self.queue.put(event.src_path)


def watch_directory(directory: str):
    observer = Observer()
    queue = Queue()
    handler = AddedFileHandler(queue)

    thread_pool = TPE(1)
    future = thread_pool.submit(consume_video_queue, queue)

    observer.schedule(handler, directory, recursive=True)
    observer.start()

    try:
        while True:
            sleep(1)

    except KeyboardInterrupt:
        observer.stop()

    observer.join()
    queue.join()
    future.cancel()
    thread_pool.shutdown()

