import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

# Configurar prancha técnica na Escala 1:20
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig, axs = plt.subplots(3, 3, figsize=(16, 14), dpi=300)

c_hull = '#0F2B48'      # Azul Marinho
c_wl = '#0284C7'        # Linha d'Água (WL)
c_wood = '#D97706'      # Madeira / Compensado
c_bg_wood = '#FEF3C7'   # Fundo madeira
c_cut = '#DC2626'       # Linha de corte vermelha

# 9 Balizas (B0 a B8) para o casco de 8,50 m na Escala 1:20 (1 metro real = 5 cm no papel / 50 mm)
stations_1_20 = [
    ("BALIZA 0 (Espelho de Popa - x = 0,0 m)", 1.25, 0.48, 0.16, 0.85, 17),
    ("BALIZA 1 (Praça de Ré - x = 0,97 m)", 1.30, 0.50, 0.18, 0.86, 18),
    ("BALIZA 2 (Porão de Peixe - x = 1,95 m)", 1.34, 0.52, 0.20, 0.88, 20),
    ("BALIZA 3 (Meia-Nau Ré - x = 2,92 m)", 1.35, 0.55, 0.22, 0.92, 22),
    ("BALIZA 4 (Meia-Nau Mestra - x = 3,90 m)", 1.35, 0.58, 0.25, 0.98, 25),
    ("BALIZA 5 (Posto de Comando - x = 4,87 m)", 1.30, 0.55, 0.28, 1.05, 30),
    ("BALIZA 6 (Cabine de Vante - x = 5,85 m)", 1.15, 0.48, 0.32, 1.15, 36),
    ("BALIZA 7 (Transição de Proa - x = 6,82 m)", 0.85, 0.38, 0.38, 1.28, 42),
    ("BALIZA 8 (Roda de Proa - x = 7,80 m)", 0.38, 0.22, 0.45, 1.40, 48)
]

for idx, (title, half_w, d, chine_offset, deck_h, deadrise) in enumerate(stations_1_20):
    ax = axs[idx // 3, idx % 3]
    
    # Eixos
    ax.axvline(0, color='black', lw=1.2, linestyle='-', label="Linha de Centro (CL)")
    ax.axhline(0, color=c_wl, lw=1.5, linestyle='--', label="Linha d'Água (WL = 0)")
    ax.axhline(deck_h, color='#475569', lw=1.0, linestyle=':', label=f"Convés (+{deck_h:.2f}m)")
    ax.axhline(-d, color='#B91C1C', lw=1.0, linestyle=':', label=f"Quilha (-{d:.2f}m)")
    
    # Grid de referência a cada 0,20 m real (1,0 cm no papel 1:20)
    for gy in np.arange(-0.8, 1.6, 0.2):
        if abs(gy) > 0.01:
            ax.axhline(gy, color='#E2E8F0', lw=0.5, linestyle='-')
    for gx in np.arange(-1.6, 1.8, 0.2):
        if abs(gx) > 0.01:
            ax.axvline(gx, color='#E2E8F0', lw=0.5, linestyle='-')
            
    chine_w = half_w * 0.92
    chine_y = -d + chine_offset
    
    # Polígono da baliza
    xs_poly = [-half_w, -chine_w, 0, chine_w, half_w, -half_w]
    ys_poly = [deck_h, chine_y, -d, chine_y, deck_h, deck_h]
    
    ax.fill(xs_poly, ys_poly, color=c_bg_wood, edgecolor=c_cut, lw=2.0, alpha=0.9)
    
    # Entalhes
    ax.add_patch(patches.Rectangle((-0.04, -d), 0.08, 0.08, facecolor='white', edgecolor='black', lw=1))
    ax.add_patch(patches.Rectangle((-chine_w - 0.02, chine_y - 0.02), 0.04, 0.04, facecolor='white', edgecolor='black', lw=0.8))
    ax.add_patch(patches.Rectangle((chine_w - 0.02, chine_y - 0.02), 0.04, 0.04, facecolor='white', edgecolor='black', lw=0.8))
    
    # Medidas na Escala 1:20 (1m = 5cm / 50mm)
    largura_real = 2 * half_w
    largura_1_20 = largura_real * 5 # em cm
    profundidade_real = d + deck_h
    profundidade_1_20 = profundidade_real * 5 # em cm
    
    ax.text(0, deck_h + 0.06, f"Boca: {largura_real:.2f} m ({largura_1_20:.2f} cm no papel)", ha='center', va='bottom', fontsize=7.2, fontweight='bold', color=c_hull)
    ax.text(chine_w + 0.04, chine_y, f"Chine: {chine_w:.2f}m\n({chine_w*5:.2f}cm)", ha='left', va='center', fontsize=6.2, color='#B45309')
    ax.text(0, -d - 0.12, f"Profundidade: {profundidade_real:.2f} m ({profundidade_1_20:.2f} cm no papel)", ha='center', va='top', fontsize=7.0, fontweight='bold', color='#B91C1C')
    ax.text(0, (deck_h - d)/2, f"Deadrise: {deadrise}°", ha='center', va='center', fontsize=7.5, fontweight='bold', color='#1E3A8A')

    ax.set_title(title, fontsize=8, fontweight='bold', color=c_hull, pad=3)
    ax.set_xlim(-1.65, 1.65)
    ax.set_ylim(-0.95, 1.65)
    ax.set_aspect('equal')
    ax.set_xlabel("Largura Real em Metros (1 m real = 5 cm no desenho 1:20)", fontsize=6.5)
    ax.set_ylabel("Altura Real em Metros", fontsize=6.5)

plt.suptitle("PLANO DE GABARITOS DAS 9 CAVERNAS NA ESCALA 1:20 (1 metro real = 5 centímetros / 50 milímetros)\nCASCO 8,50 m × 2,70 m — Gabaritos Reduzidos para Desenho em Folha A4 / Maquete Rápida", fontsize=11, fontweight='bold', color=c_hull, y=0.99)
plt.tight_layout()

output_1_20 = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/gabarito_cavernas_escala_1_20.png"
plt.savefig(output_1_20, dpi=300, bbox_inches='tight')
plt.close()
print("Gabarito 1:20 gerado com sucesso!")
