#!/usr/bin/env python3
import sys
sys.path.insert(0, '/Users/guillemhernandezsola/code/manga-translator')

import cv2
import json
import numpy as np
import importlib.util

spec = importlib.util.spec_from_file_location("manga_translator", "/Users/guillemhernandezsola/code/manga-translator/manga-translator.py")
mt = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mt)

image_path = '004.png'
detector = mt.MacVisionDetector(source_lang='en')
raw = detector.read(image_path)
image = cv2.imread(image_path)

# Filter as the pipeline does
filtered = []
for bbox, text, conf in raw:
    t = mt.normalize_text(text)
    qb = mt.quad_bbox(bbox)
    
    if conf < 0.12:
        continue
    if len(t) < 1:
        continue
    if mt.is_noise_text(t):
        continue
    if mt.is_sound_effect(t):
        continue
    if mt.is_title_text(t):
        continue
    
    filtered.append((bbox, t, conf))

# Now run grouping
bubbles, bubble_boxes, bubble_quads, bubble_indices = mt.group_tokens(
    filtered, image.shape, gap_px=18, bbox_padding=3
)

# Check current bubbles.json for reference
with open('bubbles.json') as f:
    old_bubbles = json.load(f)

print("=== BOX 5 ===")
print(f"Old bounds (from bubbles.json): x={old_bubbles['5']['x']}, y={old_bubbles['5']['y']}, w={old_bubbles['5']['w']}, h={old_bubbles['5']['h']}")
print(f"  (xyxy): ({old_bubbles['5']['x']}, {old_bubbles['5']['y']}, {old_bubbles['5']['x'] + old_bubbles['5']['w']}, {old_bubbles['5']['y'] + old_bubbles['5']['h']})")

# Find bubble at that location in current grouping
for bid, box in bubble_boxes.items():
    if box[0] == 371 and box[1] == 563:  # New box 5 location
        print(f"Current bubble {bid}: {box}")
        print(f"  Detections: {bubble_indices[bid]}")
        for idx in bubble_indices[bid]:
            b = mt.quad_bbox(filtered[idx][0])
            print(f"    {idx}: [{b[0]:3d},{b[1]:3d} -> {b[2]:3d},{b[3]:3d}] = {filtered[idx][1]}")

print("\n=== BOX 7 ===")
print(f"Old bounds (from bubbles.json): x={old_bubbles['7']['x']}, y={old_bubbles['7']['y']}, w={old_bubbles['7']['w']}, h={old_bubbles['7']['h']}")
print(f"  (xyxy): ({old_bubbles['7']['x']}, {old_bubbles['7']['y']}, {old_bubbles['7']['x'] + old_bubbles['7']['w']}, {old_bubbles['7']['y'] + old_bubbles['7']['h']})")

# Find corresponding bubble
for bid, box in bubble_boxes.items():
    x1, y1, x2, y2 = box
    # Check if this overlaps with old box 7
    old_x1, old_y1 = old_bubbles['7']['x'], old_bubbles['7']['y']
    old_x2 = old_x1 + old_bubbles['7']['w']
    old_y2 = old_y1 + old_bubbles['7']['h']
    
    if not (x2 < old_x1 or x1 > old_x2 or y2 < old_y1 or y1 > old_y2):
        print(f"Current bubble {bid}: {box}")
        print(f"  Detections: {bubble_indices[bid]}")
        for idx in bubble_indices[bid]:
            b = mt.quad_bbox(filtered[idx][0])
            print(f"    {idx}: [{b[0]:3d},{b[1]:3d} -> {b[2]:3d},{b[3]:3d}] = {filtered[idx][1]}")
