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

# Configurar prancha técnica na Escala 1:10
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=(18, 16), 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
c_grid = '#94A3B8'      # Grid milimetrado 1:10

# 9 Balizas (B0 a B8) com medidas reais (em metros) e correspondente na Escala 1:10 (em centímetros)
# (Nome, half_w_m, d_m, chine_offset_m, deck_h_m, deadrise_deg)
stations_1_10 = [
    ("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_10):
    ax = axs[idx // 3, idx % 3]
    
    # Eixos de referência com escala milimétrica
    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 (2,0 cm no desenho 1:10)
    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 para montagem
    # Quilha (8x8 cm real = 8x8 mm em 1:10)
    ax.add_patch(patches.Rectangle((-0.04, -d), 0.08, 0.08, facecolor='white', edgecolor='black', lw=1))
    # Chines (4x4 cm real = 4x4 mm em 1:10)
    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))
    
    # Textos de medidas com indicação da escala 1:10
    largura_real = 2 * half_w
    largura_1_10 = largura_real * 10 # em cm
    profundidade_real = d + deck_h
    profundidade_1_10 = profundidade_real * 10 # em cm
    
    ax.text(0, deck_h + 0.06, f"Boca Topo: {largura_real:.2f} m ({largura_1_10:.1f} 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*10:.1f}cm)", ha='left', va='center', fontsize=6.2, color='#B45309')
    ax.text(0, -d - 0.12, f"Profundidade: {profundidade_real:.2f} m ({profundidade_1_10:.1f} 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 = 10 cm no papel 1:10)", fontsize=6.5)
    ax.set_ylabel("Altura Real em Metros", fontsize=6.5)

plt.suptitle("PLANO DE GABARITOS DAS 9 CAVERNAS NA ESCALA 1:10 (1 metro real = 10 centímetros no desenho)\nMONOCASCO CASCO EM V 8,50 m × 2,70 m — Modelo para Riscamento e Confecção do Picadeiro", fontsize=11, fontweight='bold', color=c_hull, y=0.99)
plt.tight_layout()

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