#!/usr/bin/env python3
"""Test różnych stylów nakładki"""

import requests
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

# Pobierz testowy obrazek
url = "https://twojerowery.shop/ro64/wp-content/uploads/2026/02/allegro-image-14694.jpg"
response = requests.get(url, timeout=30)
image = Image.open(BytesIO(response.content))

# Tytuł i cena
title = "Rower MTB górski Kross"
price = "688 PLN"

# Font paths
font_paths = [
    "/usr/share/fonts/truetype/montserrat/Montserrat-Black.ttf",
    "/usr/share/fonts/truetype/montserrat/Montserrat-Bold.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
]

def get_font(size):
    for path in font_paths:
        try:
            return ImageFont.truetype(path, size)
        except:
            continue
    return ImageFont.load_default()

def add_overlay(image, title, price, style_name, bg_color, title_color, price_color):
    """Dodaje nakładkę w zadanym stylu"""
    if image.mode != 'RGBA':
        image = image.convert('RGBA')
    
    overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)
    
    width, height = image.size
    
    font_size = int(width * 0.09)
    font = get_font(font_size)
    
    title_font_size = int(width * 0.05)
    title_font = get_font(title_font_size)
    
    # === TYTUŁ NA ŚRODKU ===
    lines = [title[:20], title[20:40]] if len(title) > 20 else [title]
    lines = [l.strip() for l in lines if l.strip()]
    
    line_heights = []
    line_widths = []
    for line in lines:
        bbox = draw.textbbox((0, 0), line, font=title_font)
        line_widths.append(bbox[2] - bbox[0])
        line_heights.append(bbox[3] - bbox[1])
    
    max_line_width = max(line_widths) if line_widths else 0
    total_title_height = sum(line_heights) + (len(lines) - 1) * int(height * 0.008)
    
    padding_x = int(width * 0.015)
    padding_y = int(height * 0.01)
    
    title_box_width = max_line_width + 2 * padding_x
    title_box_height = total_title_height + 2 * padding_y
    
    # ŚRODEK obrazka
    title_x = (width - title_box_width) // 2
    title_y = (height - title_box_height) // 2 - int(height * 0.08)
    
    corner_radius = int(height * 0.012)
    
    # Tło tytułu
    draw.rounded_rectangle(
        [title_x, title_y, title_x + title_box_width, title_y + title_box_height],
        radius=corner_radius,
        fill=bg_color
    )
    
    # Tekst tytułu
    current_y = title_y + padding_y
    for i, line in enumerate(lines):
        if line:
            bbox = draw.textbbox((0, 0), line, font=title_font)
            line_width = bbox[2] - bbox[0]
            line_offset_y = bbox[1]
            
            line_x = title_x + (title_box_width - line_width) // 2
            line_y = current_y - line_offset_y
            
            draw.text((line_x, line_y), line, font=title_font, fill=title_color)
        
        current_y += line_heights[i] + int(height * 0.008) if i < len(line_heights) else 0
    
    # === CENA NA DOLE ===
    bbox = draw.textbbox((0, 0), price, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    
    price_padding_x = int(width * 0.012)
    price_padding_y = int(height * 0.008)
    
    price_box_width = text_width + 2 * price_padding_x
    price_box_height = text_height + 2 * price_padding_y
    
    price_x = (width - price_box_width) // 2
    price_y = height - price_box_height - int(height * 0.03)
    
    price_corner_radius = int(height * 0.015)
    
    # Tło ceny
    draw.rounded_rectangle(
        [price_x, price_y, price_x + price_box_width, price_y + price_box_height],
        radius=price_corner_radius,
        fill=bg_color
    )
    
    # Tekst ceny
    text_x = price_x + (price_box_width - text_width) // 2
    text_y = price_y + (price_box_height - text_height) // 2 - bbox[1]
    
    draw.text((text_x, text_y), price, font=font, fill=price_color)
    
    return Image.alpha_composite(image, overlay)


# Style configurations
styles = [
    ("white_on_dark", (0, 0, 0, 200), (255, 255, 255, 255), (255, 255, 255, 255)),
    ("yellow_on_blue", (20, 40, 80, 220), (255, 215, 0, 255), (255, 215, 0, 255)),
    ("white_on_red", (180, 30, 30, 220), (255, 255, 255, 255), (255, 255, 255, 255)),
    ("black_on_yellow", (255, 200, 0, 240), (0, 0, 0, 255), (0, 0, 0, 255)),
    ("white_on_green", (30, 120, 50, 220), (255, 255, 255, 255), (255, 255, 255, 255)),
]

for i, (style_name, bg, title_c, price_c) in enumerate(styles, 1):
    result = add_overlay(image.copy(), title, price, style_name, bg, title_c, price_c)
    result_rgb = Image.new('RGB', result.size, (255, 255, 255))
    result_rgb.paste(result, mask=result.split()[-1])
    result_rgb.save(f'/var/www/files/style_{i}_{style_name}.jpg', 'JPEG', quality=95)
    print(f"Style {i} ({style_name}): https://backupserwer.shop/style_{i}_{style_name}.jpg")

print("\n✅ Gotowe! Sprawdź linki.")
