from moviepy.editor import *

marker_image = ImageClip("AS_annotation_small.png")


def get_annotations_added_duration(annotations):
    duration = 0
    for annotation in annotations:
        duration += get_annotation_duration(annotation)
    return duration


def get_annotation_duration(annotation):
    return len(annotation["text"]) * 0.4


def sort_annotations_by_time(annotations):
    return sorted(annotations, key=lambda k: k["time"])


def get_marker_absolute_pos(marker_position, clip):
    marker_x = (marker_position["x"] * clip.w) - marker_image.w / 2
    marker_y = (marker_position["y"] * clip.h) - marker_image.h / 2
    return marker_x, marker_y


def get_subtitle(annotation, sub_duration):
    txt_clip = TextClip(annotation["text"], color="white", fontsize=70, font='Sans Serif')
    txt_clip = txt_clip.set_position(("center", "bottom"))
    txt_clip = txt_clip.set_start(float(annotation["time"]) / 1000.0)
    txt_clip = txt_clip.set_duration(sub_duration)

    return txt_clip


def get_marker(annotation, marker_duration, video_clip):
    position = annotation["position"]
    marker = marker_image.copy()
    marker = marker.set_position(get_marker_absolute_pos(position, video_clip))
    marker = marker.set_start(float(annotation["time"]) / 1000.0)
    marker = marker.set_duration(marker_duration)

    return marker


def is_annotation_json_valid(annotations):
    for annotation in annotations:
        if not all(k in annotation for k in ("time", "position")):
            return False
    return True


from moviepy.editor import *
import os

marker_image = ImageClip(os.path.join(os.path.dirname(__file__), "./AS_annotation_small.png"))


def get_annotations_added_duration(annotations):
    duration = 0
    for annotation in annotations:
        duration += get_annotation_duration(annotation)
    return duration


def get_annotation_duration(annotation):
    return len(annotation["text"]) * 0.4


def sort_annotations_by_time(annotations):
    return sorted(annotations, key=lambda k: k["time"])


def get_marker_absolute_pos(marker_position, clip):
    marker_x = (marker_position["x"] * clip.w) - marker_image.w / 2
    marker_y = (marker_position["y"] * clip.h) - marker_image.h / 2
    return marker_x, marker_y


def get_subtitle(annotation, sub_duration):
    txt_clip = TextClip(annotation["text"], color="white", fontsize=70, font='Sans Serif')
    txt_clip = txt_clip.set_position(("center", "bottom"))
    txt_clip = txt_clip.set_start(float(annotation["time"]) / 1000.0)
    txt_clip = txt_clip.set_duration(sub_duration)

    return txt_clip


def get_marker(annotation, marker_duration, video_clip):
    position = annotation["position"]
    marker = marker_image.copy()
    marker = marker.set_position(get_marker_absolute_pos(position, video_clip))
    marker = marker.set_start(float(annotation["time"]) / 1000.0)
    marker = marker.set_duration(marker_duration)

    return marker


def is_annotation_json_valid(annotations):
    for annotation in annotations:
        if not all(k in annotation for k in ("time", "position")):
            return False
    return True


Fix annotations relative import