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

fig, (ax_prof, ax_mid) = plt.subplots(2, 1, figsize=(14, 11), facecolor='#1e222b')

for ax in [ax_prof, ax_mid]:
    ax.set_facecolor('#14171f')
    ax.tick_params(colors='#8e9aaf', labelsize=9)
    for spine in ax.spines.values():
        spine.set_color('#3a4150')

# ==================== 1. PERFIL LONGITUDINAL REBOCADOR 17.5M ====================
ax_prof.set_title("1. PERFIL LONGITUDINAL & ARRANJO DE PROPULSÃO (Rebocador 17,5m em Alumínio 5083)", color='#00adb5', fontsize=13, fontweight='bold', pad=12)
ax_prof.set_xlim(-1.5, 19.5)
ax_prof.set_ylim(-3.5, 5.5)
ax_prof.set_xlabel("Comprimento (m) [0 = Painel de Popa | 17.5 = Proa]", color='#8e9aaf', fontsize=9)
ax_prof.set_ylabel("Altura / Calado em relação à WL (m)", color='#8e9aaf', fontsize=9)

# Linha d'água (WL 0.00)
ax_prof.axhline(0, color='#00bcd4', linestyle='--', linewidth=1.5, label="Linha d'Água (WL)")
ax_prof.text(18.0, 0.15, "WL (0.00)", color='#00bcd4', fontsize=9, fontweight='bold')

# Casco Perfil (Obras Vivas e Mortas)
x_deck = [0.0, 2.0, 8.0, 14.0, 17.5]
y_deck = [0.85, 0.70, 0.65, 1.10, 1.45]

x_bottom = [0.0, 1.5, 4.0, 10.0, 15.0, 17.5]
y_bottom = [-0.60, -2.10, -2.35, -2.35, -1.20, 1.45]

# Polígono Casco
casco_x = list(x_deck) + list(reversed(x_bottom))
casco_y = list(y_deck) + list(reversed(y_bottom))
ax_prof.fill(casco_x, casco_y, color='#2d4059', alpha=0.9, edgecolor='#eeeeee', linewidth=2)

# Skeg / Proteção de Fundo
ax_prof.fill([1.5, 4.0, 4.0, 1.5], [-2.10, -2.35, -2.50, -2.50], color='#1b262c', edgecolor='#eeeeee', linewidth=1)

# Tubete Kort e Hélice Duplo (Representação de 1 bordo)
kort = patches.Rectangle((1.2, -2.25), 1.6, 1.55, facecolor='#e84545', alpha=0.85, edgecolor='#ffffff', linewidth=1.5)
ax_prof.add_patch(kort)
ax_prof.text(2.0, -1.5, "Tubete Kort\n19A (Ø 1,45m)", color='#ffffff', fontsize=8, fontweight='bold', ha='center', va='center')

# Eixo propulsor
ax_prof.plot([2.0, 6.5], [-1.5, -1.1], color='#f9d342', linewidth=3)
# Leme de alta sustentação
ax_prof.fill([-0.4, 0.4, 0.3, -0.4], [-2.3, -2.3, -0.7, -0.7], color='#8d5524', edgecolor='#fff', linewidth=1.2)
ax_prof.text(0.0, -1.5, "Leme", color='#fff', fontsize=8, ha='center')

# Praça de Máquinas (2x 500 HP)
ax_prof.add_patch(patches.Rectangle((6.0, -1.8), 3.5, 1.6, facecolor='#ff5722', alpha=0.8, edgecolor='#fff', linewidth=1.5))
ax_prof.text(7.75, -1.0, "PRAÇA DE MÁQUINAS\n2x Motores Diesel (1.000 HP Total)", color='#fff', fontsize=9, fontweight='bold', ha='center', va='center')

# Tanques de Combustível (Fundo Duplo)
ax_prof.add_patch(patches.Rectangle((9.8, -2.1), 3.2, 1.0, facecolor='#f9d342', alpha=0.7, edgecolor='#fff'))
ax_prof.text(11.4, -1.6, "Tanque Diesel\n(10.000 L)", color='#1a202c', fontsize=8, fontweight='bold', ha='center', va='center')

# Acomodações Sub-convés (Camarins e Galley)
ax_prof.add_patch(patches.Rectangle((9.8, -0.9), 5.5, 1.7, facecolor='#00adb5', alpha=0.3, edgecolor='#00adb5', linewidth=1.5))
ax_prof.text(12.5, 0.0, "CAMARINS (4-6 Leitos) +\nCOZINHA & BANHEIRO", color='#00adb5', fontsize=8.5, fontweight='bold', ha='center', va='center')

# Passadiço / Superestrutura (Wheelhouse)
x_house = [8.5, 8.5, 14.5, 14.5, 13.5, 9.5]
y_house = [0.65, 3.8, 3.8, 1.10, 0.85, 0.65]
ax_prof.fill([8.5, 8.5, 14.0, 14.0], [0.65, 2.3, 2.3, 1.0], color='#393e46', edgecolor='#eeeeee', linewidth=1.5)
# Ponte de Comando Superior (Visão 360°)
ax_prof.fill([9.2, 9.2, 13.2, 13.2], [2.3, 3.8, 3.8, 2.3], color='#222831', edgecolor='#00bcd4', linewidth=1.8)
# Janelas do Passadiço
ax_prof.add_patch(patches.Rectangle((9.5, 2.7), 3.4, 0.9, facecolor='#00bcd4', alpha=0.7, edgecolor='#fff'))
ax_prof.text(11.2, 3.15, "PASSADIÇO (Comando 360°)", color='#ffffff', fontsize=8.5, fontweight='bold', ha='center', va='center')

# Mastro de Navegação e Antenas
ax_prof.plot([11.2, 11.2], [3.8, 5.0], color='#eeeeee', linewidth=2.5)
ax_prof.add_patch(patches.Circle((11.2, 4.8), 0.25, facecolor='#f9d342')) # Radar

# Bita e Guincho de Reboque (Popa)
ax_prof.add_patch(patches.Rectangle((3.8, 0.7), 1.2, 0.9, facecolor='#e84545', edgecolor='#fff', linewidth=1.5))
ax_prof.text(4.4, 1.2, "Bita / Guincho\nde Reboque", color='#fff', fontsize=8, fontweight='bold', ha='center', va='center')

# Defensas Cilíndricas Pesadas (Proa e Cintão)
ax_prof.add_patch(patches.Circle((17.3, 1.3), 0.5, facecolor='#111111', edgecolor='#ff5722', linewidth=2))
ax_prof.text(17.3, 2.1, "Defensa Proa\n(Pusher)", color='#ff5722', fontsize=8, fontweight='bold', ha='center')


# ==================== 2. SEÇÃO MESTRA E ESCANTILHONAMENTO ====================
ax_mid.set_title("2. SEÇÃO MESTRA (Chave Estrutural em Alumínio 5083-H111 - Boca 5,60m x Pontal 3,00m)", color='#00adb5', fontsize=13, fontweight='bold', pad=12)
ax_mid.set_xlim(-3.5, 3.5)
ax_mid.set_ylim(-3.0, 2.0)
ax_mid.set_xlabel("Boca / Meia-Nau (m) [-2.8m a +2.8m]", color='#8e9aaf', fontsize=9)
ax_mid.set_ylabel("Altura (m) [0.0 = Linha d'Água]", color='#8e9aaf', fontsize=9)

# Linha d'água na Seção
ax_mid.axhline(0, color='#00bcd4', linestyle='--', linewidth=1.5)
ax_mid.text(2.9, 0.1, "WL (0.00)", color='#00bcd4', fontsize=9, fontweight='bold')

# Casco Seção Mestra (Chine duplo em alumínio)
x_mid_pts = [-2.8, -2.7, -2.4, 0.0, 2.4, 2.7, 2.8]
y_mid_pts = [0.65, -0.8, -2.2, -2.35, -2.2, -0.8, 0.65]
ax_mid.plot(x_mid_pts, y_mid_pts, color='#eeeeee', linewidth=3)
ax_mid.plot([-2.8, 2.8], [0.65, 0.65], color='#eeeeee', linewidth=3) # Vaus do Convés

# Chapa do Convés
ax_mid.text(0, 0.85, "Convés Principal: Chapa Alumínio 8 mm (16 mm sob a Bita)", color='#00adb5', fontsize=9, fontweight='bold', ha='center')

# Costado e Fundo
ax_mid.text(-2.9, -0.2, "Costado:\nChapa 10 mm", color='#f9d342', fontsize=8.5, ha='right', va='center')
ax_mid.text(0, -2.65, "Fundo & Quilha: Chapa Alumínio 12 mm reforçada", color='#e84545', fontsize=9, fontweight='bold', ha='center')

# Fundo Duplo / Longarinas
ax_mid.plot([-2.2, 2.2], [-1.4, -1.4], color='#393e46', linewidth=2, linestyle=':')
ax_mid.text(0, -1.8, "Fundo Duplo Estanque (Tanques de Óleo Diesel)", color='#8e9aaf', fontsize=8.5, ha='center')

# Motores na Seção (2x lado a lado)
ax_mid.add_patch(patches.Rectangle((-1.8, -1.3), 1.2, 1.1, facecolor='#ff5722', alpha=0.85, edgecolor='#fff'))
ax_mid.text(-1.2, -0.75, "Motor BB\n500 HP", color='#fff', fontsize=8.5, fontweight='bold', ha='center', va='center')

ax_mid.add_patch(patches.Rectangle((0.6, -1.3), 1.2, 1.1, facecolor='#ff5722', alpha=0.85, edgecolor='#fff'))
ax_mid.text(1.2, -0.75, "Motor BE\n500 HP", color='#fff', fontsize=8.5, fontweight='bold', ha='center', va='center')

# GMt indicador
ax_mid.scatter([0], [-0.5], color='#f9d342', s=90, zorder=5)
ax_mid.text(0.15, -0.5, "G (KG=1,85m)", color='#f9d342', fontsize=8.5, fontweight='bold')

ax_mid.scatter([0], [0.62], color='#00bcd4', s=90, zorder=5)
ax_mid.text(0.15, 0.62, "M (KM=2,97m) -> GMt = 1,12 m", color='#00bcd4', fontsize=9, fontweight='bold')

plt.tight_layout()
plt.savefig('/home/magdalena/eee/alunos/matheus-silva/dimensionamento_rebocador.png', dpi=200, bbox_inches='tight')
print("Gráfico de Dimensionamento do Rebocador salvo com sucesso!")
