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

# Estilo de prancha técnica
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig = plt.figure(figsize=(18, 14), dpi=300)

c_hull = '#0F172A'      # Grafite escuro
c_wood = '#D97706'      # Tom madeira/compensado
c_bg_wood = '#FEF3C7'   # Fundo madeira cortada
c_wl = '#0284C7'        # Linha d'água
c_cut = '#DC2626'       # Linha de corte vermelha
c_text = '#1E293B'
c_dim = '#047857'       # Cotas verdes/escuras

# ---------------------------------------------------------
# PAINEL 1: GABARITOS DE CORTE DAS 9 BALIZAS (B0 a B8 - CASCO HARD CHINE)
# ---------------------------------------------------------
# Estações a cada 1,0 m ao longo dos 8,0 m LWL
# Formato Hard Chine (Andy Smith Style)
stations_hard_chine = [
    ("B0 - ESPELHO DE POPA (x=0,0m)", 0.38, 0.50, 0.15, 0.85),
    ("B1 - PRAÇA DE MÁQUINAS (x=1,0m)", 0.40, 0.54, 0.18, 0.85),
    ("B2 - PORÃO DE RÉ (x=2,0m)", 0.41, 0.55, 0.20, 0.85),
    ("B3 - PORÃO DE VANTE (x=3,0m)", 0.41, 0.55, 0.20, 0.85),
    ("B4 - MEIA-NAU / TRAVESSA (x=4,0m)", 0.41, 0.55, 0.20, 0.85),
    ("B5 - ACOMODAÇÕES (x=5,0m)", 0.40, 0.52, 0.22, 0.88),
    ("B6 - CABINE DE VANTE (x=6,0m)", 0.36, 0.48, 0.25, 0.90),
    ("B7 - TRANSIÇÃO PROA (x=7,0m)", 0.28, 0.40, 0.28, 0.92),
    ("B8 - PROA WAVE-PIERCING (x=8,0m)", 0.14, 0.25, 0.30, 0.95),
]

for idx, (title, half_w, d, chine_h, deck_h) in enumerate(stations_hard_chine):
    ax = plt.subplot2grid((4, 6), (idx // 3, (idx % 3) * 2), colspan=2)
    
    # Eixos de centro e referência
    ax.axvline(0, color='black', lw=1.2, linestyle='-')
    ax.axhline(0, color=c_wl, lw=1.2, linestyle='--')
    ax.axhline(deck_h, color='#475569', lw=0.8, linestyle=':')
    ax.axhline(-d, color='#B91C1C', lw=0.8, linestyle=':')
    
    # Vértices do molde de madeira Hard Chine
    # Pontos: Topo-BE -> Chine-BE -> Quilha -> Chine-BB -> Topo-BB
    # Chine fica em y = -d + chine_h
    chine_y = -d + chine_h
    chine_w = half_w * 0.92
    
    poly_x = [-half_w, -chine_w, 0, chine_w, half_w, -half_w]
    poly_y = [deck_h, chine_y, -d, chine_y, deck_h, deck_h]
    
    ax.fill(poly_x, poly_y, color=c_bg_wood, edgecolor=c_cut, lw=1.8, label="Linha de Corte")
    
    # Entalhes para montagem (Quilha 6x6cm no fundo e Longarinas 4x4cm no chine)
    ax.add_patch(patches.Rectangle((-0.03, -d), 0.06, 0.06, 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))
    
    # Textos de medidas para riscar na chapa
    ax.text(0, deck_h + 0.04, f"Boca Topo: {2*half_w:.2f} m", ha='center', va='bottom', fontsize=7, fontweight='bold', color=c_hull)
    ax.text(chine_w + 0.02, chine_y, f"Chine: {chine_w:.2f}m", ha='left', va='center', fontsize=6.5, color='#B45309')
    ax.text(0, -d - 0.08, f"Profundidade: {d + deck_h:.2f} m", ha='center', va='top', fontsize=6.8, fontweight='bold', color='#B91C1C')
    
    ax.set_title(title, fontsize=7.5, fontweight='bold', color=c_hull, pad=2)
    ax.set_xlim(-0.55, 0.55)
    ax.set_ylim(-0.75, 1.15)
    ax.set_aspect('equal')
    ax.axis('off')

# ---------------------------------------------------------
# PAINEL 2: GABARITOS DE CORTE DA CABINE CENTRAL (CUDDY CABIN & HARDTOP)
# ---------------------------------------------------------
# 1. Painel Frontal do Para-brisa (Facetado)
ax_c1 = plt.subplot2grid((4, 6), (3, 0), colspan=2)
ax_c1.set_title("PAINEL FRONTAL DO PARA-BRISA (CABINE)", fontsize=8, fontweight='bold', color=c_hull)
# Formato trapezoidal do para-brisa frontal
p_front_x = [-0.90, -0.75, 0.75, 0.90, -0.90]
p_front_y = [0, 0.85, 0.85, 0, 0]
ax_c1.fill(p_front_x, p_front_y, color='#E0F2FE', edgecolor=c_cut, lw=2)
# Recortes dos vidros
ax_c1.add_patch(patches.Polygon([[-0.80, 0.1], [-0.68, 0.75], [-0.05, 0.75], [-0.05, 0.1]], closed=True, facecolor='#38BDF8', edgecolor=c_hull, lw=1))
ax_c1.add_patch(patches.Polygon([[0.05, 0.1], [0.05, 0.75], [0.68, 0.75], [0.80, 0.1]], closed=True, facecolor='#38BDF8', edgecolor=c_hull, lw=1))
ax_c1.text(0, 0.42, "Vidros / Esquadrias", ha='center', va='center', fontsize=7, fontweight='bold', color='white')
ax_c1.text(0, -0.12, "Base: 1,80 m | Topo: 1,50 m | Altura: 0,85 m", ha='center', fontsize=7, fontweight='bold', color=c_hull)
ax_c1.set_xlim(-1.1, 1.1)
ax_c1.set_ylim(-0.25, 1.05)
ax_c1.set_aspect('equal')
ax_c1.axis('off')

# 2. Laterais da Cabine (Boreste / Bombordo com respiros vermelhos)
ax_c2 = plt.subplot2grid((4, 6), (3, 2), colspan=2)
ax_c2.set_title("LATERAIS DA CABINE (CORTAR 2 PEÇAS: BE / BB)", fontsize=8, fontweight='bold', color=c_hull)
# Formato perfil lateral
p_lat_x = [0, 2.60, 2.10, 0, 0]
p_lat_y = [0, 0, 0.95, 0.95, 0]
ax_c2.fill(p_lat_x, p_lat_y, color=c_bg_wood, edgecolor=c_cut, lw=2)
# Janela lateral facetada
ax_c2.add_patch(patches.Polygon([[0.4, 0.25], [1.8, 0.25], [1.5, 0.80], [0.4, 0.80]], closed=True, facecolor='#38BDF8', edgecolor=c_hull, lw=1))
# Respiro tipo Louver vermelho
ax_c2.add_patch(patches.Rectangle((1.9, 0.35), 0.35, 0.2, facecolor='#DC2626', edgecolor='black', lw=1))
ax_c2.text(1.3, -0.12, "Comprimento: 2,60 m | Altura: 0,95 m | Ângulo Proa: 60°", ha='center', fontsize=7, fontweight='bold', color=c_hull)
ax_c2.set_xlim(-0.2, 2.8)
ax_c2.set_ylim(-0.25, 1.15)
ax_c2.set_aspect('equal')
ax_c2.axis('off')

# 3. Teto Rígido (Hardtop) & Arco Targa
ax_c3 = plt.subplot2grid((4, 6), (3, 4), colspan=2)
ax_c3.set_title("TETO RÍGIDO (HARDTOP) & ARCO TARGA", fontsize=8, fontweight='bold', color=c_hull)
# Teto Hardtop
ax_c3.fill([-1.0, -0.9, 0.9, 1.0, -1.0], [0, 2.4, 2.4, 0, 0], color='#F1F5F9', edgecolor=c_cut, lw=2)
ax_c3.text(0, 1.2, "TETO HARDTOP\nLargura: 2,00 m\nComprimento: 2,40 m", ha='center', va='center', fontsize=7.5, fontweight='bold', color=c_hull)
# Arco Targa vermelho destacado ao lado
ax_c3.plot([-0.95, -0.95, 0.95, 0.95], [0.1, 0.5, 0.5, 0.1], color='#DC2626', lw=4)
ax_c3.text(0, 0.3, "Arco Targa Vermelho (Fixação Radar/Antenas)", ha='center', va='bottom', fontsize=6.5, fontweight='bold', color='#DC2626')
ax_c3.set_xlim(-1.2, 1.2)
ax_c3.set_ylim(-0.25, 2.6)
ax_c3.set_aspect('equal')
ax_c3.axis('off')

plt.suptitle("PLANO DE CORTE COMPLETO EM COMPENSADO NAVAL (GABARITOS DE MADEIRA EM ESCALA)\nPOWERCAT 8,50 m × 3,60 m — 9 Cavernas do Casco Hard Chine + Cabine Central e Teto", fontsize=11, fontweight='bold', color=c_hull, y=0.99)
plt.tight_layout()

output_plano_corte = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/plano_corte_madeira_casco_e_cabine.png"
plt.savefig(output_plano_corte, dpi=300, bbox_inches='tight')
plt.close()
print("Plano de corte completo gerado com sucesso!")
