import logging
import os
from datetime import datetime
import cv2


def write_image(out, frame):
    """
    writes frame from the webcam as png file to disk. datetime is used as filename.
    """
    if not os.path.exists(out):
        os.makedirs(out)
    now = datetime.now()
    dt_string = now.strftime("%d_%m_%Y_%H_%M_%S")
    filename = f'{out}{dt_string}.png'
    logging.info(f'write image {filename}')
    cv2.imwrite(filename, frame)


def key_action():
    k = cv2.waitKey(1)
    if k == 113:  # q button
        return 'q'
    if k == 32:  # space bar
        return 'space'
    if k == 112:  # p key
        return 'p'
    return None


def init_cam(width, height):
    """
    setups and creates a connection to the webcam
    """
    logging.info('start web cam')
    cap = cv2.VideoCapture(0)

    # Check success
    if not cap.isOpened():
        raise ConnectionError("Could not open video device")

    # Set properties. Each returns === True on success (i.e. correct
    # resolution)
    assert cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    assert cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    logging.info('web cam connected')
    return cap


def add_text(text, frame):
    # Put some rectangular box on the image
    cv2.putText(s)  # Funktion füllen
    return frame
