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

import cv2
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
filtered = []
for bbox, text, conf in raw:
    t = mt.normalize_text(text)
    if conf < 0.12 or len(t) < 1 or mt.is_noise_text(t) or mt.is_sound_effect(t) or mt.is_title_text(t):
        continue
    filtered.append((bbox, t, conf))

# Get the indices we're interested in (left and right bubbles)
left_indices = [41, 42, 43, 44, 45, 46]  # LET, GO, OFF, ME, AL-, REA-
right_indices = [47, 48, 49, 50, 51, 52, 53, 54]  # DON'T, WORRY!, HARUKO, ...

print("=== CHECKING GROUPING CONDITIONS ===\n")

# Check if they would be united in group_tokens
boxes_left = [mt.quad_bbox(filtered[i][0]) for i in left_indices]
boxes_right = [mt.quad_bbox(filtered[i][0]) for i in right_indices]

# Check overlap_or_near
print("Checking overlap_or_near with gap=18:")
for li, bi in enumerate(left_indices):
    for ri, bj in enumerate(right_indices):
        b_left = boxes_left[li]
        b_right = boxes_right[ri]
        gap_x = max(0, max(b_left[0], b_right[0]) - min(b_left[2], b_right[2]))
        gap_y = max(0, max(b_left[1], b_right[1]) - min(b_left[3], b_right[3]))
        overlaps = gap_x <= 18 and gap_y <= 18
        if overlaps:
            print(f"  {bi} and {bj} overlap/near: gap_x={gap_x}, gap_y={gap_y}")

# Check distance check
hs = [max(1.0, b[3] - b[1]) for b in [*boxes_left, *boxes_right]]
med_h = float(np.median(hs)) if hs else 12.0
dist_thresh = max(20.0, med_h * 2.2)

print(f"\nMedian height: {med_h}")
print(f"Distance threshold: {dist_thresh}")

print("\nChecking distance check:")
for li, bi in enumerate(left_indices[:1]):  # Just check first from each
    for ri, bj in enumerate(right_indices[:1]):
        b_left = boxes_left[li]
        b_right = boxes_right[ri]
        cx_left = (b_left[0] + b_left[2]) / 2.0
        cy_left = (b_left[1] + b_left[3]) / 2.0
        cx_right = (b_right[0] + b_right[2]) / 2.0
        cy_right = (b_right[1] + b_right[3]) / 2.0
        d = ((cx_left - cx_right) ** 2 + (cy_left - cy_right) ** 2) ** 0.5
        within_dist = d <= dist_thresh
        within_y = abs(cy_left - cy_right) <= med_h * 3.0
        print(f"  {bi} to {bj}: distance={d:.1f}, within_dist={within_dist}, within_y_tol={within_y}")
