from PIL import Image
import math

def process_sheet(input_path, output_path, target_size=(64, 64)):
    print(f"Processing sheet {input_path}...")
    try:
        img = Image.open(input_path).convert("RGBA")
        
        # 1. Crop to main content first to remove heavy borders
        bbox = img.getbbox()
        if not bbox:
            print("Empty image")
            return
            
        # We assume 3 cols, 2 rows roughly distributed in the bbox
        # But actually, finding the exact grid in a screenshot is hard.
        # Let's try to detect 6 separated blobs? No, too complex.
        
        # Simple approach: Crop to bbox, then assume uniform grid.
        content = img.crop(bbox)
        w, h = content.size
        
        cell_w = w / 3
        cell_h = h / 2
        
        frames = []
        
        # Extract 6 frames
        for row in range(2):
            for col in range(3):
                left = col * cell_w
                top = row * cell_h
                right = left + cell_w
                bottom = top + cell_h
                
                cell = content.crop((left, top, right, bottom))
                
                # DO NOT TRIM individual cells! Use the full cell to preserve animation offset.
                
                # Find the best scale to fit the cell into target_size
                cw, ch = cell.size
                scale = min(target_size[0] / cw, target_size[1] / ch)
                
                new_w = int(cw * scale)
                new_h = int(ch * scale)
                
                resized = cell.resize((new_w, new_h), Image.Resampling.LANCZOS)
                
                final_frame = Image.new("RGBA", target_size, (0,0,0,0))
                
                # Center horizontally, align BOTTOM vertically (usually better for feet)
                off_x = (target_size[0] - new_w) // 2
                off_y = target_size[1] - new_h
                
                # If the original image had a lot of empty space at bottom, feet might float.
                # But since we cropped 'content' to global bbox first, it should be tight enough vertically.
                
                final_frame.paste(resized, (off_x, off_y))
                
                frames.append(final_frame)
        
        # Create Strip (6x1)
        strip_w = target_size[0] * 6
        strip_h = target_size[1]
        strip = Image.new("RGBA", (strip_w, strip_h))
        
        for i, frame in enumerate(frames):
            strip.paste(frame, (i * target_size[0], 0))
            
        strip.save(output_path)
        print(f"Saved strip to {output_path}")

    except Exception as e:
        print(f"Error: {e}")

process_sheet("assets/sprites/player_walk.png", "assets/sprites/player_walk_strip.png")
process_sheet("assets/sprites/zombie_walk.png", "assets/sprites/zombie_walk_strip.png")
