import cv2
from ..utils import process_frame

class OpenCVCapture:
    """Wrapper for standard cv2.VideoCapture to support read_branches interface."""
    def __init__(self, rtsp_url, branches):
        self.branches = branches or {}
        if self.branches:
            self.max_branch_w = max(int(b["WIDTH"]) for b in self.branches.values())
            self.max_branch_h = max(int(b["HEIGHT"]) for b in self.branches.values())
        else:
            self.max_branch_w = self.max_branch_h = None
            
        self.cap = cv2.VideoCapture(rtsp_url)
        
    def isOpened(self):
        return self.cap.isOpened()
        
    def get_branches(self):
        ret, frame = self.cap.read()
        if not ret: return False, None
        
        frames_dict = {}
        for bid, bc in self.branches.items():
            frames_dict[bid] = process_frame(frame, bc, self.max_branch_w, self.max_branch_h)
        return True, frames_dict
        
    def release(self):
        self.cap.release()