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

fig, (ax_main, ax_lower) = plt.subplots(2, 1, figsize=(15, 11), facecolor='#1a1e24')

for ax in [ax_main, ax_lower]:
    ax.set_facecolor('#13171d')
    ax.tick_params(colors='#8e9aaf', labelsize=9)
    for spine in ax.spines.values():
        spine.set_color('#3a4150')

# ==================== 1. CONVÉS PRINCIPAL (MAIN DECK) ====================
ax_main.set_title("1. PLANTA DO CONVÉS PRINCIPAL (Superestrutura, Cozinha/Refeitório, Sanitário & Área de Trabalho)", color='#00adb5', fontsize=12, fontweight='bold', pad=10)
ax_main.set_xlim(-1.5, 18.5)
ax_main.set_ylim(-3.5, 3.5)
ax_main.set_xlabel("Comprimento (m) [0 = Painel de Popa | 17.5 = Proa]", color='#8e9aaf', fontsize=8.5)
ax_main.set_ylabel("Boca / Largura (m)", color='#8e9aaf', fontsize=8.5)

# Contorno Casco Convés Principal
x_hull = np.linspace(0, 17.5, 200)
# Perfil de rebocador: popa larga e arredondada, meio paralelo e proa em colher/V com bico largo para defensas
y_hull = 2.8 * (1 - (x_hull/17.5 - 0.45)**4) * (x_hull/17.5)**0.25
y_hull = np.clip(y_hull, 0, 2.8)

ax_main.plot(x_hull, y_hull, color='#eeeeee', linewidth=2.5)
ax_main.plot(x_hull, -y_hull, color='#eeeeee', linewidth=2.5)
ax_main.plot([0, 0], [-y_hull[0], y_hull[0]], color='#eeeeee', linewidth=2.5)

# Cintão de defensas pesadas
ax_main.plot(x_hull, y_hull + 0.15, color='#ff5722', linewidth=1.5, linestyle=':')
ax_main.plot(x_hull, -y_hull - 0.15, color='#ff5722', linewidth=1.5, linestyle=':')
ax_main.plot([17.0, 17.5, 17.0], [1.2, 0, -1.2], color='#ff5722', linewidth=4) # Defensa proa push

# Superestrutura no Convés Principal (x: 7.5 a 14.5, y: -2.0 a +2.0)
ax_main.add_patch(patches.Rectangle((7.5, -2.0), 7.0, 4.0, facecolor='#222831', edgecolor='#00adb5', linewidth=2))

# Divisões Internas da Casaria do Convés:
# Cozinha / Galley (Bombordo, x: 7.6 a 11.0, y: -1.9 a -0.1)
ax_main.add_patch(patches.Rectangle((7.6, -1.9), 3.4, 1.8, facecolor='#2b323c', edgecolor='#f9d342', linewidth=1.2))
ax_main.add_patch(patches.Rectangle((7.8, -1.8), 0.8, 0.6, facecolor='#e84545')) # Fogão
ax_main.add_patch(patches.Rectangle((8.8, -1.8), 0.7, 0.5, facecolor='#00bcd4')) # Pia dupla
ax_main.add_patch(patches.Rectangle((9.8, -1.8), 1.0, 0.6, facecolor='#f9d342', alpha=0.8)) # Geladeira/Freezer
ax_main.text(9.3, -0.7, "COZINHA (GALLEY)\nFogão, Pia Dupla & Freezer", color='#f9d342', fontsize=8, fontweight='bold', ha='center', va='center')

# Refeitório / Mess Room (Boreste, x: 7.6 a 11.0, y: 0.1 a 1.9)
ax_main.add_patch(patches.Rectangle((7.6, 0.1), 3.4, 1.8, facecolor='#2b323c', edgecolor='#00adb5', linewidth=1.2))
ax_main.add_patch(patches.Rectangle((8.0, 0.4), 2.4, 1.0, facecolor='#8d5524', edgecolor='#fff')) # Mesa 6 pax
ax_main.text(9.2, 0.9, "REFEITÓRIO (MESS ROOM)\nMesa p/ 6 Tripulantes + TV/Armário", color='#ffffff', fontsize=8, fontweight='bold', ha='center', va='center')

# Banheiro Principal do Convés (x: 11.2 a 13.0, y: -1.9 a -0.1)
ax_main.add_patch(patches.Rectangle((11.2, -1.9), 1.8, 1.8, facecolor='#2b323c', edgecolor='#00bcd4', linewidth=1.2))
ax_main.add_patch(patches.Circle((11.6, -1.4), 0.25, facecolor='#fff')) # Vaso
ax_main.add_patch(patches.Circle((12.5, -1.4), 0.2, facecolor='#00bcd4')) # Pia
ax_main.add_patch(patches.Rectangle((12.1, -0.7), 0.7, 0.5, facecolor='#00adb5', alpha=0.5)) # Box chuveiro
ax_main.text(12.1, -0.35, "WC / Chuveiro", color='#00bcd4', fontsize=7.5, ha='center', va='center')

# Hall de Acesso & Escada p/ Camarins Inferiores (x: 11.2 a 14.3, y: 0.1 a 1.9)
ax_main.add_patch(patches.Rectangle((11.2, 0.1), 3.1, 1.8, facecolor='#1f242d', edgecolor='#eeeeee', linewidth=1))
ax_main.add_patch(patches.Rectangle((13.0, 0.3), 1.1, 1.4, facecolor='#8d5524', edgecolor='#fff'))
for i in range(5):
    ax_main.plot([13.0, 14.1], [0.3 + i*0.28, 0.3 + i*0.28], color='#fff', linewidth=1)
ax_main.text(13.55, 1.0, "Escada p/\nCamarins", color='#fff', fontsize=7.5, fontweight='bold', ha='center', va='center')
ax_main.text(12.0, 1.0, "Hall / Vestiário\nCapas / Botas", color='#8e9aaf', fontsize=7.5, ha='center', va='center')

# Convés de Trabalho de Popa (Aft Working Deck, x: 0.5 a 7.5)
ax_main.add_patch(patches.Rectangle((3.5, -0.7), 1.5, 1.4, facecolor='#e84545', edgecolor='#fff', linewidth=1.5))
ax_main.text(4.25, 0, "GUINCHO DE REBOQUE\n& BITA H (Towing Winch)", color='#fff', fontsize=8, fontweight='bold', ha='center', va='center')
ax_main.add_patch(patches.Circle((1.5, 1.8), 0.35, facecolor='#f9d342'))
ax_main.add_patch(patches.Circle((1.5, -1.8), 0.35, facecolor='#f9d342'))
ax_main.text(1.5, 1.8, "Bita", color='#1a202c', fontsize=7, fontweight='bold', ha='center', va='center')
ax_main.text(1.5, -1.8, "Bita", color='#1a202c', fontsize=7, fontweight='bold', ha='center', va='center')
ax_main.text(5.5, 0, "CONVÉS DE TRABALHO DESIMPEDIDO", color='#00adb5', fontsize=8.5, fontweight='bold', ha='center')

# ==================== 2. CONVÉS INFERIOR / ACOMODAÇÕES ====================
ax_lower.set_title("2. PLANTA DO CONVÉS INFERIOR (Camarim Comandante, Camarim Guarnição, Máquinas & Tanques)", color='#00adb5', fontsize=12, fontweight='bold', pad=10)
ax_lower.set_xlim(-1.5, 18.5)
ax_lower.set_ylim(-3.5, 3.5)
ax_lower.set_xlabel("Comprimento (m)", color='#8e9aaf', fontsize=8.5)
ax_lower.set_ylabel("Boca / Largura (m)", color='#8e9aaf', fontsize=8.5)

# Linhas internas do casco no convés inferior
y_hull_low = 2.4 * (1 - (x_hull/17.5 - 0.45)**4) * (x_hull/17.5)**0.35
y_hull_low = np.clip(y_hull_low, 0, 2.4)
ax_lower.plot(x_hull, y_hull_low, color='#eeeeee', linewidth=2)
ax_lower.plot(x_hull, -y_hull_low, color='#eeeeee', linewidth=2)
ax_lower.plot([0, 0], [-y_hull_low[0], y_hull_low[0]], color='#eeeeee', linewidth=2)

# Anteparas Estanques Estruturais (Bulkheads)
bh_x = [3.0, 9.0, 15.5]
for bx in bh_x:
    ax_lower.plot([bx, bx], [-2.4, 2.4], color='#ff5722', linewidth=2.5, linestyle='--')
ax_lower.text(3.0, 2.6, "Antepara Estanque", color='#ff5722', fontsize=7.5, ha='center')
ax_lower.text(9.0, 2.6, "Antepara Estanque", color='#ff5722', fontsize=7.5, ha='center')
ax_lower.text(15.5, 2.6, "Antepara de Colisão", color='#ff5722', fontsize=7.5, ha='center')

# ZONA 1: PROA (x: 15.5 a 17.5) -> Paiol de Amarra / Pique de Vante
ax_lower.text(16.5, 0, "PIQUE DE\nVANTE\n(Lastro/Amarra)", color='#8e9aaf', fontsize=7.5, ha='center', va='center')

# ZONA 2: CAMARINS (x: 9.0 a 15.5) -> Acomodações de Pernoite 4-6 Tripulantes
# Corredor Central (y: -0.4 a +0.4)
ax_lower.fill([9.0, 15.5, 15.5, 9.0], [-0.4, -0.4, 0.4, 0.4], color='#1f242d')
ax_lower.text(12.2, 0, "CORREDOR CENTRAL", color='#8e9aaf', fontsize=7.5, ha='center', va='center')

# Camarim 1: Oficial / Comandante (Boreste, x: 9.2 a 12.2, y: 0.45 a 2.2)
ax_lower.add_patch(patches.Rectangle((9.2, 0.45), 3.0, 1.75, facecolor='#2b323c', edgecolor='#00adb5', linewidth=1.5))
# Cama Comandante
ax_lower.add_patch(patches.Rectangle((9.4, 1.25), 1.9, 0.85, facecolor='#2d4059', edgecolor='#00bcd4'))
ax_lower.text(10.35, 1.67, "Cama Oficial (2,0x0,9m)", color='#fff', fontsize=7.5, ha='center', va='center')
# Mesa de trabalho / Armário
ax_lower.add_patch(patches.Rectangle((11.4, 0.55), 0.7, 0.6, facecolor='#8d5524', edgecolor='#fff'))
ax_lower.add_patch(patches.Rectangle((9.4, 0.55), 0.8, 0.55, facecolor='#393e46', edgecolor='#fff'))
ax_lower.text(11.75, 0.85, "Mesa", color='#fff', fontsize=7, ha='center', va='center')
ax_lower.text(9.8, 0.82, "Armário", color='#fff', fontsize=7, ha='center', va='center')
ax_lower.text(10.7, 0.55, "CAMARIM DO COMANDANTE (1-2 Leitos)", color='#00adb5', fontsize=8, fontweight='bold', ha='center', va='bottom')

# Camarim 2: Guarnição / Marinheiros (Bombordo, x: 9.2 a 14.0, y: -2.2 a -0.45)
ax_lower.add_patch(patches.Rectangle((9.2, -2.2), 4.8, 1.75, facecolor='#2b323c', edgecolor='#00adb5', linewidth=1.5))
# Beliche Duplo 1
ax_lower.add_patch(patches.Rectangle((9.4, -2.1), 1.9, 0.75, facecolor='#2d4059', edgecolor='#00bcd4'))
ax_lower.text(10.35, -1.72, "Beliche Duplo (2 leitos)", color='#fff', fontsize=7.5, ha='center', va='center')
# Beliche Duplo 2
ax_lower.add_patch(patches.Rectangle((11.5, -2.1), 1.9, 0.75, facecolor='#2d4059', edgecolor='#00bcd4'))
ax_lower.text(12.45, -1.72, "Beliche Duplo (2 leitos)", color='#fff', fontsize=7.5, ha='center', va='center')
# Armários Individuais Guarnição
ax_lower.add_patch(patches.Rectangle((9.4, -1.15), 3.8, 0.55, facecolor='#393e46', edgecolor='#fff'))
ax_lower.text(11.3, -0.87, "4x Armários / Gaveteiros Individuais", color='#ffffff', fontsize=7.5, ha='center', va='center')
ax_lower.text(11.6, -0.5, "CAMARIM DA GUARNIÇÃO (4 Leitos)", color='#00adb5', fontsize=8, fontweight='bold', ha='center', va='bottom')

# Banheiro Adicional Inferior / Lavabo (Boreste Vante, x: 12.4 a 14.8, y: 0.45 a 1.9)
ax_lower.add_patch(patches.Rectangle((12.4, 0.45), 2.4, 1.45, facecolor='#2b323c', edgecolor='#00bcd4', linewidth=1.2))
ax_lower.add_patch(patches.Circle((12.8, 0.9), 0.22, facecolor='#fff')) # Vaso
ax_lower.add_patch(patches.Circle((13.6, 0.9), 0.18, facecolor='#00bcd4')) # Pia
ax_lower.add_patch(patches.Rectangle((14.0, 0.6), 0.7, 0.7, facecolor='#00adb5', alpha=0.5)) # Chuveiro
ax_lower.text(13.6, 1.5, "WC / Banho Inferior", color='#00bcd4', fontsize=7.5, ha='center', va='center')

# ZONA 3: PRAÇA DE MÁQUINAS (x: 3.0 a 9.0)
ax_lower.add_patch(patches.Rectangle((3.1, -2.2), 5.8, 4.4, facecolor='#181c22', edgecolor='#ff5722', linewidth=1.5))
# Motor Bombordo
ax_lower.add_patch(patches.Rectangle((4.5, -1.8), 3.2, 1.1, facecolor='#ff5722', alpha=0.9, edgecolor='#fff'))
ax_lower.text(6.1, -1.25, "MOTOR DIESEL BB\n500 BHP @ 1800 rpm", color='#fff', fontsize=8, fontweight='bold', ha='center', va='center')
# Motor Boreste
ax_lower.add_patch(patches.Rectangle((4.5, 0.7), 3.2, 1.1, facecolor='#ff5722', alpha=0.9, edgecolor='#fff'))
ax_lower.text(6.1, 1.25, "MOTOR DIESEL BE\n500 BHP @ 1800 rpm", color='#fff', fontsize=8, fontweight='bold', ha='center', va='center')
# Gerador de Serviço
ax_lower.add_patch(patches.Rectangle((7.0, -0.4), 1.5, 0.8, facecolor='#f9d342', edgecolor='#fff'))
ax_lower.text(7.75, 0, "Gerador\n30 kVA", color='#1a202c', fontsize=7.5, fontweight='bold', ha='center', va='center')
ax_lower.text(4.0, 0, "PRAÇA DE\nMÁQUINAS", color='#ff5722', fontsize=8.5, fontweight='bold', ha='center', va='center')

# ZONA 4: COMPARTIMENTO DO LEME & TANQUES DE POPA (x: 0 a 3.0)
ax_lower.add_patch(patches.Rectangle((0.2, -1.8), 2.7, 3.6, facecolor='#222831', edgecolor='#00adb5'))
ax_lower.text(1.5, 0, "COMPARTIMENTO\nDO LEME &\nTANQUES LASTRO", color='#00adb5', fontsize=8, fontweight='bold', ha='center', va='center')

plt.tight_layout()
plt.savefig('/home/magdalena/eee/alunos/matheus-silva/arranjo_camarins_rebocador.png', dpi=200, bbox_inches='tight')
print("Planta dos Camarins salva com sucesso!")
