import matplotlib.pyplot as plt
import numpy as np

# Configuração da figura com 4 vistas técnicas
fig = plt.figure(figsize=(16, 12), dpi=220)
fig.patch.set_facecolor('#F8F9FA')

# Estilo de linhas e cores
C_HULL = '#1F4E79'
C_WOOD = '#8B5A2B'
C_FLOOR = '#CD853F'
C_ENGINE = '#C00000'
C_WATER = '#2E75B6'
C_DETAILS = '#333333'
C_CANOPY = '#5B9BD5'

# -------------------------------------------------------------
# 1. PERFIL LATERAL (ELEVATION & INBOARD ARRANGEMENT)
# -------------------------------------------------------------
ax1 = fig.add_subplot(2, 2, 1)
ax1.set_facecolor('#FFFFFF')

# Casco (Perfil)
x_hull = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
z_sheer = [0.910, 0.900, 0.895, 0.895, 0.905, 0.925, 0.955, 1.000]
z_chine = [0.045, 0.050, 0.060, 0.070, 0.110, 0.190, 0.380, 1.000]
z_keel = [0.000, 0.000, 0.000, 0.010, 0.040, 0.120, 0.280, 1.000]

# Preenchimento do casco
ax1.fill_between(x_hull, z_keel, z_sheer, color='#EBF1F5', alpha=0.7)
ax1.plot(x_hull, z_sheer, color=C_HULL, linewidth=2.5, label='Borda (Sheer)')
ax1.plot(x_hull, z_chine, color='#2E75B6', linewidth=1.5, linestyle='--', label='Chine')
ax1.plot(x_hull, z_keel, color='#1F3864', linewidth=2.5, label='Quilha')
ax1.plot([0, 0], [0, 0.910], color=C_HULL, linewidth=2.5) # Espelho de popa

# Linha d'água de projeto (WL = 0.30 m)
ax1.axhline(0.30, color=C_WATER, linewidth=1.8, linestyle='-.', label='Linha d\'Água (Calado 30 cm)')
ax1.fill_between([0, 7.0], [-0.1, -0.1], [0.30, 0.30], color=C_WATER, alpha=0.12)

# Quilha externa / Skeg protetor do hélice e leme
x_skeg = [0.0, 0.5, 1.5, 2.8, 3.0]
z_skeg = [-0.22, -0.22, -0.15, -0.02, 0.01]
ax1.plot(x_skeg, z_skeg, color='#385723', linewidth=3.0, label='Sapata / Proteção de Quilha')
ax1.fill_between(x_skeg, z_skeg, [0, 0, 0, 0, 0.01], color='#385723', alpha=0.3)

# Eixo, Hélice e Leme
ax1.plot([1.0, 3.2], [-0.14, 0.35], color='#D4AF37', linewidth=3.0) # Linha de eixo
# Hélice (x=0.9, z=-0.15)
ax1.plot([0.9, 0.9], [-0.02, -0.26], color='#B8860B', linewidth=4.0) # Hélice
# Leme (x=0.1 a -0.1, z=-0.22 a 0.20)
ax1.fill([-0.05, 0.15, 0.15, -0.05], [-0.22, -0.22, 0.25, 0.25], color='#595959', alpha=0.8)
ax1.plot([-0.05, -0.05, -0.5], [0.25, 0.95, 1.02], color='#333333', linewidth=2.5) # Cana do leme

# Paneiros (piso do convés)
ax1.plot([0.3, 6.2], [0.25, 0.45], color=C_WOOD, linewidth=3.5, label='Piso de Paneiros')

# Berço e Capô do Motor Agrale M90 (Meia-nau: x=2.8 a 3.8)
ax1.fill([2.8, 3.8, 3.8, 2.8], [0.28, 0.28, 0.78, 0.78], color='#8B0000', alpha=0.85)
ax1.plot([2.8, 3.8, 3.8, 2.8, 2.8], [0.28, 0.28, 0.78, 0.78, 0.28], color='#333333', linewidth=2.0)
ax1.text(3.3, 0.53, 'Agrale M90\n(13 cv)', color='white', fontweight='bold', fontsize=8, ha='center', va='center')

# Borda falsa / Castelo de proa e popa
ax1.fill([5.5, 7.0, 7.0, 5.5], [0.93, 1.00, 1.00, 0.93], color='#D2B48C', alpha=0.9) # Plataforma de proa
ax1.fill([0.0, 0.8, 0.8, 0.0], [0.91, 0.90, 0.90, 0.91], color='#D2B48C', alpha=0.9) # Bancada de popa
ax1.plot([0.8, 5.5], [0.90, 0.93], color='#5C3A21', linewidth=2.5) # Borda de madeira (Trilho)

# Toldo / Capota de Passeio (Lona removível)
ax1.plot([1.2, 1.2], [0.90, 1.85], color='#7F7F7F', linewidth=2.0)
ax1.plot([3.3, 3.3], [0.90, 1.90], color='#7F7F7F', linewidth=2.0)
ax1.plot([5.0, 5.0], [0.92, 1.85], color='#7F7F7F', linewidth=2.0)
ax1.plot([1.0, 5.2], [1.85, 1.85], color=C_CANOPY, linewidth=4.0, label='Capota / Toldo de Sol')

ax1.set_title('1. Perfil Lateral & Arranjo Interno (Corte Longitudinal)', fontsize=11, fontweight='bold', color='#1F3864')
ax1.set_xlabel('Comprimento X (m)', fontsize=9)
ax1.set_ylabel('Altura Z (m)', fontsize=9)
ax1.set_xlim(-0.8, 7.5)
ax1.set_ylim(-0.4, 2.1)
ax1.grid(True, linestyle=':', alpha=0.6)
ax1.legend(loc='upper right', fontsize=7.5)

# -------------------------------------------------------------
# 2. PLANTA BAIXA / CONVÉS (DECK & SEATING LAYOUT)
# -------------------------------------------------------------
ax2 = fig.add_subplot(2, 2, 2)
ax2.set_facecolor('#FFFFFF')

# Linhas de Borda (Sheer) BE e BB
y_sheer = [0.830, 0.960, 1.050, 1.075, 1.030, 0.880, 0.580, 0.000]
ax2.plot(x_hull, y_sheer, color=C_HULL, linewidth=2.5)
ax2.plot(x_hull, [-y for y in y_sheer], color=C_HULL, linewidth=2.5)
ax2.plot([0, 0], [-0.830, 0.830], color=C_HULL, linewidth=2.5) # Espelho

# Fundo / Paneiros de madeira
ax2.fill_between(x_hull, [-y*0.85 for y in y_sheer], [y*0.85 for y in y_sheer], color='#F4ECD8', alpha=0.9)

# Capô do Motor Agrale M90 (Centro)
rect_eng = plt.Rectangle((2.8, -0.35), 1.0, 0.70, facecolor='#8B0000', edgecolor='#333333', linewidth=1.5, zorder=5)
ax2.add_patch(rect_eng)
ax2.text(3.3, 0, 'Motor Agrale M90\n13 cv (Centro)', color='white', fontweight='bold', fontsize=7.5, ha='center', va='center', zorder=6)

# Bancos Laterais para 6 Passageiros
# Banco Popa
ax2.fill([0.0, 0.7, 0.7, 0.0], [-0.80, -0.80, 0.80, 0.80], color=C_WOOD, alpha=0.85, zorder=4)
ax2.text(0.35, 0, 'Banco de Popa\n(Comando / Piloto)', color='white', fontsize=7.5, fontweight='bold', ha='center', va='center')

# Bancos Longitudinais de Meia-Nau / Pesca
ax2.fill([1.0, 2.6, 2.6, 1.0], [0.60, 0.75, 0.95, 0.80], color=C_WOOD, alpha=0.85, zorder=4)
ax2.fill([1.0, 2.6, 2.6, 1.0], [-0.60, -0.75, -0.95, -0.80], color=C_WOOD, alpha=0.85, zorder=4)
ax2.fill([4.0, 5.2, 5.2, 4.0], [0.55, 0.40, 0.75, 0.90], color=C_WOOD, alpha=0.85, zorder=4)
ax2.fill([4.0, 5.2, 5.2, 4.0], [-0.55, -0.40, -0.75, -0.90], color=C_WOOD, alpha=0.85, zorder=4)

ax2.text(1.8, 0.78, 'Passageiros', color='white', fontsize=7, fontweight='bold', ha='center', va='center')
ax2.text(1.8, -0.78, 'Passageiros', color='white', fontsize=7, fontweight='bold', ha='center', va='center')
ax2.text(4.6, 0.58, 'Pesca / Lazer', color='white', fontsize=7, fontweight='bold', ha='center', va='center')
ax2.text(4.6, -0.58, 'Pesca / Lazer', color='white', fontsize=7, fontweight='bold', ha='center', va='center')

# Plataforma de Proa (Castelinho para ancoragem e solário)
ax2.fill([5.5, 7.0, 5.5], [0.70, 0.0, -0.70], color=C_WOOD, alpha=0.9, zorder=4)
ax2.text(6.0, 0, 'Castelo de Proa\n(Âncora / Paiol)', color='white', fontsize=7.5, fontweight='bold', ha='center', va='center')

# Linhas de cavernas estruturais (espaçadas a cada 1m)
for x_c in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]:
    ax2.plot([x_c, x_c], [-1.0, 1.0], color='#888888', linestyle=':', alpha=0.5)

ax2.set_title('2. Planta de Convés & Layout dos Bancos (6 Pessoas)', fontsize=11, fontweight='bold', color='#1F3864')
ax2.set_xlabel('Comprimento X (m)', fontsize=9)
ax2.set_ylabel('Boca Y (m)', fontsize=9)
ax2.set_xlim(-0.5, 7.5)
ax2.set_ylim(-1.3, 1.3)
ax2.grid(True, linestyle=':', alpha=0.6)

# -------------------------------------------------------------
# 3. SEÇÃO MESTRA TRANSVERSAL (MIDSHIP SECTION & ENGINE BED)
# -------------------------------------------------------------
ax3 = fig.add_subplot(2, 2, 3)
ax3.set_facecolor('#FFFFFF')

# Seção Mestra na Baliza 4 (x=3.0 m)
# Borda: y=1.075, z=0.895 | Chine: y=0.990, z=0.070 | Quilha: y=0, z=0.010
y_sec = [-1.075, -0.990, 0.0, 0.990, 1.075]
z_sec = [0.895, 0.070, 0.010, 0.070, 0.895]

# Casco externo
ax3.plot(y_sec, z_sec, color=C_HULL, linewidth=3.0, label='Casco em Compensado Naval (12 mm)')
ax3.fill(y_sec, z_sec, color='#EBF1F5', alpha=0.6)

# Linha d'água na seção (Calado 30 cm)
ax3.axhline(0.30, color=C_WATER, linewidth=1.8, linestyle='-.', label='Linha d\'Água (WL)')
ax3.fill_between([-1.2, 1.2], [-0.1, -0.1], [0.30, 0.30], color=C_WATER, alpha=0.1)

# Piso / Paneiros
ax3.plot([-0.85, 0.85], [0.25, 0.25], color=C_WOOD, linewidth=3.0, label='Piso de Paneiros (Estrado)')

# Berço do Motor Agrale M90 (Madeira de lei)
ax3.fill([-0.30, -0.20, -0.20, -0.30], [0.03, 0.03, 0.35, 0.35], color='#4A2E18')
ax3.fill([0.20, 0.30, 0.30, 0.20], [0.03, 0.03, 0.35, 0.35], color='#4A2E18')

# Capô do Motor
ax3.fill([-0.35, 0.35, 0.35, -0.35], [0.25, 0.25, 0.78, 0.78], color='#8B0000', alpha=0.85)
ax3.plot([-0.35, 0.35, 0.35, -0.35, -0.35], [0.25, 0.25, 0.78, 0.78, 0.25], color='#333333', linewidth=2.0)
ax3.text(0, 0.52, 'Motor Agrale M90\n(13 cv Diesel)', color='white', fontweight='bold', fontsize=8, ha='center', va='center')

# Bancos Laterais
ax3.fill([-1.02, -0.65, -0.65, -1.02], [0.55, 0.55, 0.60, 0.60], color=C_WOOD, alpha=0.9)
ax3.fill([0.65, 1.02, 1.02, 0.65], [0.55, 0.55, 0.60, 0.60], color=C_WOOD, alpha=0.9)
ax3.text(-0.83, 0.66, 'Banco BB', color='#333333', fontsize=7.5, fontweight='bold', ha='center')
ax3.text(0.83, 0.66, 'Banco BE', color='#333333', fontsize=7.5, fontweight='bold', ha='center')

# Borda e Verdugo
ax3.plot([-1.10, -1.075], [0.895, 0.895], color='#5C3A21', linewidth=4.0)
ax3.plot([1.075, 1.10], [0.895, 0.895], color='#5C3A21', linewidth=4.0)

ax3.set_title('3. Seção Mestra & Instalação do Motor (Meia-Nau)', fontsize=11, fontweight='bold', color='#1F3864')
ax3.set_xlabel('Boca / Meia-Boca (m)', fontsize=9)
ax3.set_ylabel('Altura Acima da Quilha (m)', fontsize=9)
ax3.set_xlim(-1.3, 1.3)
ax3.set_ylim(-0.1, 1.2)
ax3.grid(True, linestyle=':', alpha=0.6)
ax3.legend(loc='upper right', fontsize=7.5)

# -------------------------------------------------------------
# 4. PERSPECTIVA 3D ISOMÉTRICA DO BARCO COMPLETO
# -------------------------------------------------------------
ax4 = fig.add_subplot(2, 2, 4, projection='3d')
ax4.set_facecolor('#FFFFFF')

# Balizas em 3D
data_3d = [
    [1, 0.0, 0.830, 0.770, 0.910, 0.045, 0.0],
    [2, 1.0, 0.960, 0.890, 0.900, 0.050, 0.0],
    [3, 2.0, 1.050, 0.975, 0.895, 0.060, 0.0],
    [4, 3.0, 1.075, 0.990, 0.895, 0.070, 0.010],
    [5, 4.0, 1.030, 0.920, 0.905, 0.110, 0.040],
    [6, 5.0, 0.880, 0.740, 0.925, 0.190, 0.120],
    [7, 6.0, 0.580, 0.440, 0.955, 0.380, 0.280],
    [8, 7.0, 0.000, 0.000, 1.000, 1.000, 1.000],
]

for row in data_3d:
    stn, x, sh_hb, ch_hb, sh_ht, ch_ht, k_ht = row
    ys = [-sh_hb, -ch_hb, 0.0, ch_hb, sh_hb]
    zs = [sh_ht, ch_ht, k_ht, ch_ht, sh_ht]
    xs = [x, x, x, x, x]
    ax4.plot(xs, ys, zs, color=C_HULL, linewidth=1.2, alpha=0.7)

# Longitudinais
xs_3d = [r[1] for r in data_3d]
ax4.plot(xs_3d, [-r[2] for r in data_3d], [r[4] for r in data_3d], color='#C00000', linewidth=2.0)
ax4.plot(xs_3d, [r[2] for r in data_3d], [r[4] for r in data_3d], color='#C00000', linewidth=2.0)
ax4.plot(xs_3d, [-r[3] for r in data_3d], [r[5] for r in data_3d], color='#2E75B6', linewidth=2.0)
ax4.plot(xs_3d, [r[3] for r in data_3d], [r[5] for r in data_3d], color='#2E75B6', linewidth=2.0)
ax4.plot(xs_3d, [0 for _ in data_3d], [r[6] for r in data_3d], color='#385723', linewidth=2.5)

# Caixa do motor 3D
xe = [2.8, 3.8, 3.8, 2.8, 2.8]
ye = [-0.35, -0.35, 0.35, 0.35, -0.35]
ze_base = [0.25, 0.25, 0.25, 0.25, 0.25]
ze_top = [0.75, 0.75, 0.75, 0.75, 0.75]
ax4.plot(xe, ye, ze_base, color='#8B0000', linewidth=1.5)
ax4.plot(xe, ye, ze_top, color='#8B0000', linewidth=2.0)
for i in range(4):
    ax4.plot([xe[i], xe[i]], [ye[i], ye[i]], [ze_base[i], ze_top[i]], color='#8B0000', linewidth=1.5)

# Cana do Leme 3D
ax4.plot([0.0, -0.4], [0.0, 0.0], [0.85, 0.95], color='#333333', linewidth=3.0)

ax4.set_title('4. Visão Geral Tridimensional', fontsize=11, fontweight='bold', color='#1F3864')
ax4.set_xlabel('X (m)')
ax4.set_ylabel('Y (m)')
ax4.set_zlabel('Z (m)')
ax4.view_init(elev=24, azim=-55)

plt.suptitle('PROJETO CONSTRUTIVO NAVAL — BARCO 7,00 M FUNDO SEMI-PLANO\nArmador/Projetista: Tino Neto | Motorização: Agrale M90 (13 cv Diesel)', fontsize=13, fontweight='bold', color='#1F3864')
plt.tight_layout()
plt.savefig('/home/magdalena/eee/alunos/tino-neto/desenho_barco_pronto_tino_neto.png', dpi=220)
print('Desenho do barco pronto gerado!')
