from CommonCode import wq
from CommonCode.video_meta import FTPVideo
import os
import sys
import time


sys.path.insert(0, "/home/thebears/Source/ml/sources_mltraining/scoring_scheduler")
sys.path.insert(0, "/home/thebears/Source/ml/sources_mltraining/model_runner/")

from trim_with_birds import execute_file_trimming
from video_reduce_resolution import convert_file_with_check
input_topic = wq.TOPICS.ml_vision_objdet_results_purge_skipped
output_if_success = wq.TOPICS.ml_vision_videos_modify_success
output_if_fail = wq.TOPICS.ml_vision_videos_modify_failed



def perform_loop():
    file_to_score = wq.consume(input_topic)
    if file_to_score is None:
        return

    try:
        did_trim = False#        did_trim = execute_file_trimming(files_to_check = file_to_score)
        did_convert = convert_file_with_check(file_to_score)
        FTPVideo(file_to_score).frames_info;
        wq.publish(output_if_success, file_to_score)
        print(f"File trimmed:{did_trim}, did_downsize:{did_convert}, {file_to_score}")

    except Exception as e:
        wq.publish(output_if_fail, {file_to_score: str(e)})
        print(f"File fail to create {file_to_score}, {e}")

# %%
while True:
    perform_loop()
    time.sleep(1)
