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=(18, 14), dpi=300)

c_hull = '#0F2B48'
c_wl = '#0284C7'
c_engine = '#EA580C'
c_shaft = '#64748B'
c_pod = '#0284C7'
c_wood = '#D97706'
c_bg_wood = '#FEF3C7'
c_deck = '#F1F5F9'
c_cut = '#DC2626'
c_text = '#1E293B'

# -------------------------------------------------------------------------
# 1. ARRANJO GERAL DE CONVÉS (Top View com Gôndola e Espaços)
# -------------------------------------------------------------------------
ax1 = fig.add_subplot(2, 2, 1)
ax1.set_title("1. ARRANJO GERAL DE CONVÉS (Top View — 8,50 m × 3,60 m)\nGôndola Central, Porões nos Cascos, Beliches e Praça de Pesca", fontsize=10, fontweight='bold', color=c_hull, pad=8)

y_be = 1.39
y_bb = -1.39
bw = 0.41

xp = np.linspace(0, 8.5, 100)
w_ext = np.where(xp < 5.5, bw, bw * (1 - ((xp - 5.5)/3.0)**1.2))
ax1.plot(xp, y_be + w_ext, color=c_hull, lw=1.8)
ax1.plot(xp, y_be - w_ext*0.8, color=c_hull, lw=1.8)
ax1.plot(xp, y_bb - w_ext, color=c_hull, lw=1.8)
ax1.plot(xp, y_bb + w_ext*0.8, color=c_hull, lw=1.8)

# Piso do convés
ax1.fill([0.5, 7.8, 7.8, 0.5], [y_bb + bw*0.8, y_bb, y_be, y_be - bw*0.8], color=c_deck, edgecolor=c_hull, lw=1.2)

# Gôndola Central
ax1.fill([0.0, 4.0, 4.5, 4.0, 0.0], [-0.45, -0.45, 0.0, 0.45, 0.45], color='#BAE6FD', edgecolor='#0369A1', lw=1.5)
ax1.add_patch(patches.Rectangle((1.5, -0.35), 1.2, 0.7, facecolor='#FED7AA', edgecolor=c_engine, lw=1.5, zorder=3))
ax1.text(2.1, 0, 'Motor Diesel Central\n(30 a 50 CV)', ha='center', va='center', fontsize=6.5, fontweight='bold', color=c_engine, zorder=4)

# Tanques de combustível e água na gôndola
ax1.add_patch(patches.Rectangle((2.8, -0.35), 0.8, 0.7, facecolor='#A7F3D0', edgecolor='#059669', lw=1.2, zorder=3))
ax1.text(3.2, 0, 'Tanque Diesel\n(300 L)', ha='center', va='center', fontsize=6, fontweight='bold', color='#065F46', zorder=4)

# Porões de peixe nos cascos (500 kg cada)
ax1.add_patch(patches.Rectangle((1.2, y_be - 0.3), 1.6, 0.6, facecolor='#93C5FD', edgecolor=c_hull, lw=1))
ax1.text(2.0, y_be, 'Porão Térmico BE\n(500 kg peixe + gelo)', ha='center', va='center', fontsize=6, fontweight='bold', color=c_hull)
ax1.add_patch(patches.Rectangle((1.2, y_bb - 0.3), 1.6, 0.6, facecolor='#93C5FD', edgecolor=c_hull, lw=1))
ax1.text(2.0, y_bb, 'Porão Térmico BB\n(500 kg peixe + gelo)', ha='center', va='center', fontsize=6, fontweight='bold', color=c_hull)

# Beliches na vante das canoas
ax1.add_patch(patches.Rectangle((5.0, y_be - 0.3), 2.2, 0.6, facecolor='#DDD6FE', edgecolor='#6D28D9', lw=1))
ax1.text(6.1, y_be, '2 Beliches Vante BE', ha='center', va='center', fontsize=6, fontweight='bold', color='#4C1D95')
ax1.add_patch(patches.Rectangle((5.0, y_bb - 0.3), 2.2, 0.6, facecolor='#DDD6FE', edgecolor='#6D28D9', lw=1))
ax1.text(6.1, y_bb, '2 Beliches Vante BB', ha='center', va='center', fontsize=6, fontweight='bold', color='#4C1D95')

# Posto de comando à meia-nau
ax1.add_patch(patches.Rectangle((4.2, 0.45), 1.2, 0.8, facecolor='#3B82F6', edgecolor='#1D4ED8', lw=1.2))
ax1.text(4.8, 0.85, 'Posto de Comando\n(Timão / Painel)', ha='center', va='center', fontsize=6.5, fontweight='bold', color='white')

ax1.set_xlim(-0.8, 9.2)
ax1.set_ylim(-2.2, 2.2)
ax1.set_aspect('equal')
ax1.set_xlabel("Comprimento (m)", fontsize=7)
ax1.set_ylabel("Boca (m)", fontsize=7)

# -------------------------------------------------------------------------
# 2. DETALHAMENTO DA GÔNDOLA EM CORTE (Side Profile & Seção Transversal)
# -------------------------------------------------------------------------
ax2 = fig.add_subplot(2, 2, 2)
ax2.set_title("2. DETALHE DA GÔNDOLA CENTRAL (Perfil e Seção de Fundo)\nEntrada Suave na Meia-Nau e Saída no Hélice", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Perfil da gôndola (x=0 a 4.5m)
x_g = [0, 0, 2.5, 4.5, 4.0, 0]
y_g = [0.85, -0.45, -0.45, 0.35, 0.85, 0.85]
ax2.fill(x_g, y_g, color='#E0F2FE', edgecolor='#0369A1', lw=2)

# Motor, Eixo e Hélice
ax2.add_patch(patches.Rectangle((1.5, -0.2), 1.2, 0.65, facecolor='#FED7AA', edgecolor=c_engine, lw=1.5))
ax2.text(2.1, 0.12, 'Motor Diesel Central', ha='center', va='center', fontsize=7, fontweight='bold', color=c_engine)
ax2.plot([0.2, 1.3], [-0.40, -0.15], color=c_shaft, lw=3.5)
ax2.add_patch(patches.Rectangle((0.08, -0.55), 0.12, 0.30, facecolor='#F59E0B', edgecolor='#B45309', lw=1.2))
ax2.add_patch(patches.Rectangle((-0.18, -0.55), 0.10, 0.45, facecolor='#475569', edgecolor='black', lw=1.2))
ax2.plot([-0.1, 2.0], [-0.55, -0.45], color=c_hull, lw=3) # Sapata/Skeg

# Linha d'água
ax2.axhline(0, color=c_wl, lw=1.5, linestyle='--', label="Linha d'Água (WL = 0)")
ax2.fill_between([0, 4.5], [-0.45, 0], [0, 0], color='#BAE6FD', alpha=0.35)

ax2.annotate('', xy=(4.5, 0.35), xytext=(4.5, 0), arrowprops=dict(arrowstyle='<->', color='#0369A1', lw=1))
ax2.text(4.6, 0.17, 'Entrada suave acima da WL\n(Sem bater ondas)', fontsize=6.5, color='#0369A1', fontweight='bold')

ax2.set_xlim(-0.5, 5.5)
ax2.set_ylim(-0.8, 1.2)
ax2.set_aspect('equal')
ax2.set_xlabel("Comprimento da Gôndola (m)", fontsize=7)
ax2.set_ylabel("Altura (m)", fontsize=7)
ax2.legend(loc='upper right', fontsize=6.5)

# -------------------------------------------------------------------------
# 3. ESTRUTURA E ESPECIFICAÇÃO DE LAMINAÇÃO PRFV (Corte Construtivo)
# -------------------------------------------------------------------------
ax3 = fig.add_subplot(2, 2, 3)
ax3.set_title("3. ESTRUTURAÇÃO E LAMINAÇÃO EM PRFV (Sandwich e Reforços)\nEspecificação das Camadas e Longarinas", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Corte do fundo e costado de um semi-casco
ax3.plot([-1.1, -0.9, 0, 0.9, 1.1], [1.1, -0.1, -0.8, -0.1, 1.1], color=c_hull, lw=3.5, label="PRFV Sólido (8-10 mm)")
ax3.plot([-1.0, -0.8, 0, 0.8, 1.0], [1.1, -0.05, -0.7, -0.05, 1.1], color='#0284C7', lw=2, label="Núcleo Sandwich (Divinycell 15 mm)")

# Longarinas longitudinais de fundo
ax3.add_patch(patches.Rectangle((-0.45, -0.6), 0.15, 0.45, facecolor='#CBD5E1', edgecolor=c_hull, lw=1.2))
ax3.add_patch(patches.Rectangle((0.30, -0.6), 0.15, 0.45, facecolor='#CBD5E1', edgecolor=c_hull, lw=1.2))
ax3.text(0, -0.4, 'Longarinas Estruturais\n(Caixas em PRFV c/ Roving)', ha='center', va='center', fontsize=6.5, fontweight='bold')

# Viga transversal da ponte
ax3.plot([0.9, 1.8], [1.1, 1.1], color=c_hull, lw=4)
ax3.text(1.35, 1.2, 'Flange / Viga Mestra\nde Ligação da Ponte', ha='center', va='bottom', fontsize=6.8, fontweight='bold', color=c_hull)

# Quilha maciça
ax3.add_patch(patches.Polygon([[-0.2, -0.75], [0.2, -0.75], [0, -0.95]], closed=True, facecolor='#1E293B', edgecolor='black'))
ax3.text(0, -0.85, 'Quilha Maciça', ha='center', va='center', fontsize=6, color='white', fontweight='bold')

ax3.set_xlim(-1.4, 2.0)
ax3.set_ylim(-1.1, 1.5)
ax3.set_aspect('equal')
ax3.legend(loc='upper left', fontsize=6.5)

# -------------------------------------------------------------------------
# 4. PLANO DE PICADEIRO E GABARITOS DE CORTE (Montagem na Madeira)
# -------------------------------------------------------------------------
ax4 = fig.add_subplot(2, 2, 4)
ax4.set_title("4. GABARITO DE PICADEIRO DAS CAVERNAS E GÔNDOLA\nMontagem Alinhada com Linha de Centro e Nível", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Desenho esquemático das balizas no picadeiro
ax4.axhline(0, color=c_wl, lw=1.2, linestyle='--', label="Linha d'Água (Nível Zero)")
ax4.axvline(-1.39, color='#64748B', lw=1, linestyle=':', label="Eixo Canoa BB")
ax4.axvline(1.39, color='#64748B', lw=1, linestyle=':', label="Eixo Canoa BE")
ax4.axvline(0, color='black', lw=1.2, linestyle='-', label="Linha de Centro Geral (CL)")

# Formato das cavernas de meia-nau
def draw_mold(ax, cx):
    xs = [cx - 0.41, cx - 0.38, cx, cx + 0.38, cx + 0.41]
    ys = [0.85, -0.15, -0.55, -0.15, 0.85]
    ax.fill(xs, ys, color=c_bg_wood, edgecolor=c_cut, lw=1.5)
    # Entalhes
    ax.add_patch(patches.Rectangle((cx - 0.03, -0.55), 0.06, 0.06, facecolor='white', edgecolor='black', lw=0.8))

draw_mold(ax4, -1.39)
draw_mold(ax4, 1.39)

# Molde da Gôndola Central no picadeiro
ax4.fill([-0.45, -0.35, 0, 0.35, 0.45], [0.35, -0.15, -0.45, -0.15, 0.35], color='#BAE6FD', edgecolor=c_cut, lw=1.5)
ax4.text(0, -0.2, 'Molde Gôndola\nCentral', ha='center', va='center', fontsize=6.5, fontweight='bold', color='#0369A1')

# Travessa de picadeiro superior unindo tudo
ax4.plot([-1.9, 1.9], [0.95, 0.95], color=c_wood, lw=3)
ax4.text(0, 1.05, 'Viga Guia Superior do Picadeiro (Garante Esquadro)', ha='center', va='bottom', fontsize=6.5, fontweight='bold', color=c_wood)

ax4.set_xlim(-2.2, 2.2)
ax4.set_ylim(-0.9, 1.4)
ax4.set_aspect('equal')
ax4.legend(loc='lower right', fontsize=6)

plt.tight_layout()
output_all = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/projeto_completo_catamara_gondola.png"
plt.savefig(output_all, dpi=300, bbox_inches='tight')
plt.close()
print("Prancha completa das 4 opções gerada!")
