#!/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))

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

print("=== BUBBLE 7 & 8 ANALYSIS ===\n")
print("Current Bubble 7 (right side content):")
for bid in [7]:
    if bid in bubble_indices:
        box = bubble_boxes[bid]
        print(f"  Box: {box}")
        print(f"  Indices: {bubble_indices[bid]}")
        indices = bubble_indices[bid]
        boxes = [mt.quad_bbox(filtered[i][0]) for i in indices]
        min_x = min(b[0] for b in boxes)
        max_x = max(b[2] for b in boxes)
        print(f"  X range: {min_x} - {max_x}")
        for idx in indices:
            b = mt.quad_bbox(filtered[idx][0])
            print(f"    {idx}: x=[{b[0]:3d},{b[2]:3d}] y=[{b[1]:3d},{b[3]:3d}] = {filtered[idx][1]}")

print("\nCurrent Bubble 8 (left side content):")
for bid in [8]:
    if bid in bubble_indices:
        box = bubble_boxes[bid]
        print(f"  Box: {box}")
        print(f"  Indices: {bubble_indices[bid]}")
        indices = bubble_indices[bid]
        boxes = [mt.quad_bbox(filtered[i][0]) for i in indices]
        min_x = min(b[0] for b in boxes)
        max_x = max(b[2] for b in boxes)
        print(f"  X range: {min_x} - {max_x}")
        for idx in indices:
            b = mt.quad_bbox(filtered[idx][0])
            print(f"    {idx}: x=[{b[0]:3d},{b[2]:3d}] y=[{b[1]:3d},{b[3]:3d}] = {filtered[idx][1]}")

# Check the horizontal gap between them
print("\n=== GAP ANALYSIS ===")
if 7 in bubble_indices and 8 in bubble_indices:
    boxes7 = [mt.quad_bbox(filtered[i][0]) for i in bubble_indices[7]]
    boxes8 = [mt.quad_bbox(filtered[i][0]) for i in bubble_indices[8]]
    
    max_x7 = max(b[2] for b in boxes7)
    min_x8 = min(b[0] for b in boxes8)
    
    print(f"Bubble 7 max X: {max_x7}")
    print(f"Bubble 8 min X: {min_x8}")
    print(f"Horizontal gap: {min_x8 - max_x7}")
    
    # Check Y overlap
    min_y7 = min(b[1] for b in boxes7)
    max_y7 = max(b[3] for b in boxes7)
    min_y8 = min(b[1] for b in boxes8)
    max_y8 = max(b[3] for b in boxes8)
    
    print(f"\nBubble 7 Y range: {min_y7} - {max_y7}")
    print(f"Bubble 8 Y range: {min_y8} - {max_y8}")
    print(f"Y overlap: {max(0, min(max_y7, max_y8) - max(min_y7, min_y8))} pixels")
