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

plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig = plt.figure(figsize=(16, 11), dpi=300)

c_hull = '#0F2B48'
c_wl = '#0284C7'
c_bulkhead = '#D97706'
c_keel = '#1E293B'
c_frame = '#475569'
c_tank = '#10B981'
c_ice = '#60A5FA'
c_engine = '#EA580C'
c_berth = '#8B5CF6'
c_text = '#1E293B'

# 1. LINHAS DE FORMA E BALIZAS TRANSVERSAIS (Body Plan - Estações de 0 a 8)
ax1 = fig.add_subplot(2, 2, 1)
ax1.set_title("1. PLANO DE BALIZAS TRANSVERSAIS (Body Plan do Semi-Casco)\nFormato das Cavernas de Vante (Proa) e Ré (Popa)", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Balizas de Ré (Lado Esquerdo do gráfico)
# Estações: Baliza 0 (Espelho), Baliza 1, Baliza 2, Baliza 3, Baliza 4 (Meia-Nau)
b_stations_aft = [
    # (x_offset, max_w, depth, v_angle_factor, label)
    (-0.38, 0.38, 0.50, 0.15, "B0 (Popa/Espelho)"),
    (-0.40, 0.40, 0.56, 0.18, "B1"),
    (-0.41, 0.41, 0.60, 0.22, "B2 (Porão)"),
    (-0.41, 0.41, 0.60, 0.24, "B3"),
    (-0.41, 0.41, 0.60, 0.25, "B4 (Meia-Nau)"),
]

for w_max, w_max_val, d, v_fact, lbl in b_stations_aft:
    ys = np.linspace(-d, 0.9, 50)
    # Forma de V assimétrica / bojo suave
    xs = - (w_max_val * np.power(np.maximum(0, (ys + d)/(0.9 + d)), 0.45) * (1 - v_fact * np.exp(-3*(ys+d))))
    ax1.plot(xs, ys, color='#2563EB', lw=1.6, label=lbl if lbl in ["B0 (Popa/Espelho)", "B4 (Meia-Nau)"] else "")

# Balizas de Vante (Lado Direito do gráfico)
# Baliza 5, Baliza 6, Baliza 7, Baliza 8 (Roda de Proa)
b_stations_fwd = [
    (0.40, 0.40, 0.58, 0.30, "B5"),
    (0.36, 0.36, 0.52, 0.45, "B6 (Cabine)"),
    (0.28, 0.28, 0.42, 0.65, "B7"),
    (0.12, 0.12, 0.25, 0.85, "B8 (Proa Fina)"),
]

for w_max, w_max_val, d, v_fact, lbl in b_stations_fwd:
    ys = np.linspace(-d, 0.9, 50)
    xs = (w_max_val * np.power(np.maximum(0, (ys + d)/(0.9 + d)), 0.6) * (1 - v_fact * np.exp(-2.5*(ys+d))))
    ax1.plot(xs, ys, color='#DC2626', lw=1.6, label=lbl if lbl in ["B8 (Proa Fina)"] else "")

ax1.axhline(0, color=c_wl, lw=1.5, linestyle='--', label="Linha d'Água (WL)")
ax1.axvline(0, color='black', lw=1.0, linestyle=':')
ax1.plot([0, 0], [-0.65, 0.9], color=c_keel, lw=2)

ax1.text(-0.25, -0.75, "← Balizas de Ré\n(Seções de Popa)", ha='center', fontsize=7.5, fontweight='bold', color='#2563EB')
ax1.text(0.25, -0.75, "Balizas de Vante →\n(Seções de Proa)", ha='center', fontsize=7.5, fontweight='bold', color='#DC2626')

ax1.set_xlim(-0.55, 0.55)
ax1.set_ylim(-0.85, 1.1)
ax1.set_xlabel("Semi-Boca (m)", fontsize=8)
ax1.set_ylabel("Altura a partir da WL (m)", fontsize=8)
ax1.legend(loc='upper right', fontsize=6.5)


# 2. CORTE LONGITUDINAL INTERNO DA CANOA (Internal Profile & Bulkheads)
ax2 = fig.add_subplot(2, 2, 2)
ax2.set_title("2. COMPARTIMENTAÇÃO INTERNA DO SEMI-CASCO (Perfil)\nCavernas Estruturais, Anteparas Estanques e Acomodações", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Contorno lateral da canoa
x_c = [0, 0, 4.5, 7.5, 8.5, 8.0, 0]
y_c = [0.9, -0.6, -0.6, -0.35, 0.9, 0.9, 0.9]
ax2.plot([0, 0, 4.5, 7.5, 8.5, 8.0, 0], [0.9, -0.6, -0.6, -0.35, 0.9, 0.9, 0.9], color=c_hull, lw=2.2)
ax2.fill([0, 0, 4.5, 7.5, 8.5, 8.0, 0], [0.9, -0.6, -0.6, -0.35, 0.9, 0.9, 0.9], color='#F8FAFC')
ax2.axhline(0, color=c_wl, lw=1.2, linestyle='--')

# Falso quilhão / Skeg estrutural de proteção do hélice e leme
ax2.fill([0, 1.8, 1.8, 0], [-0.6, -0.6, -0.4, -0.4], color='#CBD5E1', edgecolor=c_hull, lw=1.2)
ax2.plot([-0.2, 0], [-0.5, -0.5], color='#EA580C', lw=3) # Eixo
ax2.add_patch(patches.Rectangle((-0.3, -0.65), 0.1, 0.3, facecolor='#EA580C', edgecolor='#9A3412')) # Hélice
ax2.add_patch(patches.Rectangle((-0.45, -0.65), 0.08, 0.4, facecolor='#475569', edgecolor='black')) # Leme

# Anteparas Estanques (Bulkheads) - Linhas Verticais Laranjas
bulkheads_x = [0.2, 1.8, 3.8, 5.8, 7.6]
for bx in bulkheads_x:
    ax2.axvline(bx, ymin=0.15, ymax=0.9, color=c_bulkhead, lw=2.5, linestyle='-')

# Compartimentos Nomeados
# 1. Praça de Máquinas (0.2 a 1.8m)
ax2.add_patch(patches.Rectangle((0.3, -0.4), 1.3, 0.5, facecolor='#FED7AA', edgecolor=c_engine, lw=1.2))
ax2.text(0.95, -0.15, 'Motor Diesel\n(25-30 HP)', ha='center', va='center', fontsize=7, fontweight='bold', color=c_engine)
ax2.text(0.95, 0.6, 'Praça de\nMáquinas', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

# 2. Porão de Pescado / Gelo (1.8 a 3.8m)
ax2.add_patch(patches.Rectangle((1.9, -0.5), 1.8, 0.8, facecolor='#BAE6FD', edgecolor='#0284C7', lw=1.2))
ax2.text(2.8, -0.1, 'Porão Térmico\n(PUR 80mm)\n500 kg Peixe + Gelo', ha='center', va='center', fontsize=6.8, fontweight='bold', color='#0369A1')
ax2.text(2.8, 0.6, 'Porão de Carga', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

# 3. Tanques e Baterias / Paiol (3.8 a 5.8m)
ax2.add_patch(patches.Rectangle((3.9, -0.5), 0.9, 0.4, facecolor='#A7F3D0', edgecolor='#059669', lw=1.2))
ax2.text(4.35, -0.3, 'Diesel\n175 L', ha='center', va='center', fontsize=6.5, fontweight='bold', color='#065F46')
ax2.add_patch(patches.Rectangle((4.85, -0.5), 0.85, 0.4, facecolor='#E0E7FF', edgecolor='#4338CA', lw=1.2))
ax2.text(5.27, -0.3, 'Água\n150 L', ha='center', va='center', fontsize=6.5, fontweight='bold', color='#3730A3')
# Beliche superior
ax2.add_patch(patches.Rectangle((3.9, 0.1), 1.8, 0.35, facecolor='#DDD6FE', edgecolor=c_berth, lw=1.2))
ax2.text(4.8, 0.27, 'Beliche Inferior (1 Pescador)', ha='center', va='center', fontsize=6.5, fontweight='bold', color=c_berth)

# 4. Cabine de Proa / Beliche (5.8 a 7.6m)
ax2.add_patch(patches.Rectangle((5.9, 0.0), 1.6, 0.4, facecolor='#DDD6FE', edgecolor=c_berth, lw=1.2))
ax2.text(6.7, 0.2, 'Beliche Vante\n(1 Pescador)', ha='center', va='center', fontsize=6.5, fontweight='bold', color=c_berth)
ax2.text(6.7, 0.6, 'Cabine Proa', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

# 5. Caixa de Colisão / Pique de Proa (7.6 a 8.5m)
ax2.text(8.0, 0.1, 'Pique de Proa\n(Estanque)', ha='center', va='center', fontsize=6, fontweight='bold', color='#991B1B')

ax2.set_xlim(-0.8, 9.0)
ax2.set_ylim(-0.9, 1.2)
ax2.set_xlabel("Comprimento Longitudinal (m)", fontsize=8)
ax2.set_ylabel("Altura (m)", fontsize=8)


# 3. DETALHE ESTRUTURAL E LAMINADO PRFV (Sandwich e Reforços)
ax3 = fig.add_subplot(2, 2, 3)
ax3.set_title("3. DETALHAMENTO DO LAMINADO E ESTRUTURAÇÃO (PRFV)\nCorte da Casca, Longarinas e Reforço de Fundo", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Desenho em corte ampliado da seção do casco
# Casca externa (Gelcoat + Laminação)
ax3.plot([-1.2, -1.0, 0, 1.0, 1.2], [1.2, -0.2, -1.0, -0.2, 1.2], color=c_hull, lw=3.5, label="Casca Externa (PRFV Sólido 8-10 mm)")
ax3.plot([-1.1, -0.9, 0, 0.9, 1.1], [1.2, -0.15, -0.9, -0.15, 1.2], color='#0284C7', lw=2, label="Núcleo / Espuma PVC (Divinycell)")

# Longarinas de Fundo (Girders)
ax3.add_patch(patches.Rectangle((-0.5, -0.75), 0.18, 0.5, facecolor='#CBD5E1', edgecolor=c_hull, lw=1.5))
ax3.text(-0.41, -0.5, 'Longarina\nBombordo', ha='center', va='center', fontsize=6.5, fontweight='bold')

ax3.add_patch(patches.Rectangle((0.32, -0.75), 0.18, 0.5, facecolor='#CBD5E1', edgecolor=c_hull, lw=1.5))
ax3.text(0.41, -0.5, 'Longarina\nBoreste', ha='center', va='center', fontsize=6.5, fontweight='bold')

# Quilha Reforçada (Sapata de Fundo Maciça com Rovings Unidirecionais)
ax3.add_patch(patches.Polygon([[-0.25, -0.9], [0.25, -0.9], [0, -1.15]], closed=True, facecolor='#1E293B', edgecolor='black', lw=1.5))
ax3.text(0, -1.0, 'Quilha Maciça\n(Proteção Impacto)', ha='center', va='center', fontsize=6.5, fontweight='bold', color='white')

# Piso do Porão / Convés Interno
ax3.plot([-0.95, 0.95], [-0.25, -0.25], color='#D97706', lw=2.5)
ax3.text(0, -0.15, 'Piso Estrutural / Fundo do Porão', ha='center', va='center', fontsize=7.5, fontweight='bold', color='#D97706')

# Isolamento Térmico PUR 80mm
ax3.fill([-0.85, 0.85, 0.85, -0.85], [-0.2, -0.2, 0.7, 0.7], color='#BAE6FD', alpha=0.6)
ax3.text(0, 0.25, 'Isolamento Térmico\nPoliuretano Rígido (80 mm)\n+ Revestimento PRFV Higiênico', ha='center', va='center', fontsize=7.5, fontweight='bold', color='#0369A1')

# Borda de União do Túnel (Flange Estrutural)
ax3.plot([1.1, 1.8], [1.2, 1.2], color=c_hull, lw=4)
ax3.text(1.45, 1.3, 'Flange / Viga de\nLigação da Ponte', ha='center', va='bottom', fontsize=7, fontweight='bold', color=c_hull)

ax3.set_xlim(-1.6, 2.0)
ax3.set_ylim(-1.3, 1.6)
ax3.set_aspect('equal')
ax3.legend(loc='upper left', fontsize=6.5)


# 4. TABELA DE ESPECIFICAÇÕES CONSTRUTIVAS DA CANOA
ax4 = fig.add_subplot(2, 2, 4)
ax4.axis('off')
ax4.set_title("4. ESPECIFICAÇÕES TÉCNICAS DO SEMI-CASCO (CANOA)", fontsize=10, fontweight='bold', color=c_hull, pad=8)

specs_text = """
• Geometria de Cada Semi-Casco (Canoa):
  - Comprimento Total: 8,50 m | LWL: 8,00 m
  - Boca Máxima da Canoa (b): 0,82 m
  - Relação de Esbeltez (L/b): 9,75 (Casco ultra-eficiente, corta ondas)
  - Calado de Operação (T): 0,60 m | Pontal (D): 1,50 m
  - Formato de Seção: V profundo na proa (fatia a onda) evoluindo 
    para V suave com fundo arredondado e quilhão na ré (sustentação e rumo).

• Esquema de Compartimentação Estanque (Normas NORMAM):
  - 1. Pique de Proa (Caixa de colisão estanque)
  - 2. Cabine de Vante (Beliche + armários de rancho)
  - 3. Meia-Nau (Beliche inferior + Tanque Diesel 175L + Água 150L)
  - 4. Porão de Pescado (Capacidade de 500 kg peixe + 550 kg gelo)
  - 5. Praça de Máquinas (Motor Diesel 25-30 HP + Linha de Eixo + Bateria)

• Especificação da Laminação em Fibra de Vidro (PRFV):
  - Gelcoat Isoftálico Naval com barreira anti-osmose (NPG).
  - Casca externa em PRFV sólido (Manta 450 g/m² + Tecido Biaxial 600 g/m²).
  - Costado em Sandwich com núcleo de espuma rígida de PVC (Divinycell 15 mm).
  - Quilha e espelho de popa reforçados com roving unidirecional maciço.
  - Resina Isoftálica ou Éster-Vinílica na camada de barreira.
"""

ax4.text(0.02, 0.98, specs_text, ha='left', va='top', fontsize=8, family='monospace', color=c_text, linespacing=1.35)

plt.tight_layout()
output_img2 = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/detalhe_canoas_powercat.png"
plt.savefig(output_img2, dpi=300, bbox_inches='tight')
plt.close()
print("Desenho detalhado das canoas gerado com sucesso!")
