import cv2
import pickle
from pathlib import Path

# TemporalTracker already saved the video chunks? No.
# I will use fast_verify.py's frames but run process_pages directly and print all its output.
import fast_verify
from youtube_tab_to_pdf import extract_unique_scroll

# Actually, I will just write a wrapper around ScoreExtractor to print to file
import sys

def main():
    cap = cv2.VideoCapture("output/サカナクション／新宝島(エレキギターTAB) 難易度★★★ sakanaction shintakarajima.mp4")
    # Quick dynamic crop
    ret, initial = cap.read()
    scale = 1280 / initial.shape[1]
    
    from youtube_tab_to_pdf import _find_white_tab_strip
    crop_top, crop_bottom = 0, int(initial.shape[0] * scale)
    
    cap.set(cv2.CAP_PROP_POS_FRAMES, 500)
    ret, check_frame = cap.read()
    if ret:
        resized_check = cv2.resize(check_frame, (1280, int(check_frame.shape[0] * scale)))
        bounds = _find_white_tab_strip(resized_check)
        if bounds:
            crop_top, crop_bottom = bounds
            
    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    # We don't want to load ALL 15000 frames into memory. Use TemporalTracker directly!
    from video_cv_tracker import TemporalTracker
    tracker = TemporalTracker(diff_threshold=0.05)
    
    count = 0
    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    
    while True:
        ret, frame = cap.read()
        if not ret: break
        if count % 4 == 0:
            resized = cv2.resize(frame, (1280, int(frame.shape[0] * scale)))
            tracker.process_frame(resized[crop_top:crop_bottom, :])
        count += 1
        
    unique_pages = tracker.get_unique_pages()
    print(f"Got {len(unique_pages)} unique pages from tracker.")
    
    from score_extractor import ScoreExtractor
    extractor = ScoreExtractor()
    
    # We will hook print
    original_print = print
    with open("score_log.txt", "w") as f:
        def my_print(*args, **kwargs):
            text = " ".join(map(str, args))
            f.write(text + "\n")
            original_print(*args, **kwargs)
        
        import builtins
        builtins.print = my_print
        
        extractor.process_pages(unique_pages)
        
        builtins.print = original_print

if __name__ == "__main__":
    main()
