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

# Estilo naval técnico
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig = plt.figure(figsize=(16, 12), dpi=300)

c_hull = '#0F2B48'
c_wl = '#0284C7'
c_engine = '#EA580C'
c_shaft = '#94A3B8'
c_pod = '#0284C7'
c_wood = '#D97706'
c_tunnel = '#94A3B8'

# 1. PLANTA BAIXA / VISTA SUPERIOR (Top View)
ax1 = fig.add_subplot(2, 2, 1)
ax1.set_title("1. PLANTA BAIXA COM GÔNDOLA CENTRAL (Top View)\nMotor Diesel Único na Meia-Nau/Popa da Ponte", fontsize=10, fontweight='bold', color=c_hull, pad=8)

y_be = 1.39
y_bb = -1.39
bw = 0.41

# Cascos principais
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)

# Convés central
ax1.fill([0.5, 7.5, 7.5, 0.5], [y_bb + bw*0.8, y_bb, y_be, y_be - bw*0.8], color='#F1F5F9', edgecolor=c_hull, lw=1.2)

# Gôndola / Nacela Central (de x = 0 a x = 4.2m)
pod_x = [0.0, 3.8, 4.2, 3.8, 0.0]
pod_y = [-0.45, -0.45, 0.0, 0.45, 0.45]
ax1.fill(pod_x, pod_y, color='#BAE6FD', edgecolor='#0369A1', lw=1.8, label="Gôndola Central (Nacela)")

# Motor Central Único dentro da gôndola (x = 1.5 a 2.6m)
ax1.add_patch(patches.Rectangle((1.4, -0.3), 1.2, 0.6, facecolor='#FED7AA', edgecolor=c_engine, lw=1.5, zorder=3))
ax1.text(2.0, 0, 'Motor Diesel Único\n(40 a 60 CV)', ha='center', va='center', fontsize=7.5, fontweight='bold', color=c_engine, zorder=4)

# Linha de eixo e hélice central único
ax1.plot([0.1, 1.4], [0, 0], color=c_shaft, lw=3.5, zorder=2)
ax1.add_patch(patches.Circle((0.1, 0), 0.20, facecolor='#F59E0B', edgecolor='#B45309', zorder=3))
ax1.text(-0.35, 0, 'Hélice e Leme\nCentrais', ha='center', va='center', fontsize=7, fontweight='bold', color='#B45309')

# Espaço livre nos cascos para beliches/porões
ax1.text(2.5, y_be, 'Canoa BE:\nBeliches / Porão', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)
ax1.text(2.5, y_bb, 'Canoa BB:\nBeliches / Porão', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

ax1.set_xlim(-0.8, 9.0)
ax1.set_ylim(-2.2, 2.2)
ax1.set_aspect('equal')
ax1.set_xlabel("Comprimento (m)", fontsize=8)
ax1.set_ylabel("Largura (m)", fontsize=8)


# 2. VISTA FRONTAL / SEÇÃO TRANSVERSAL (Front View - Baliza Mestra com Gôndola)
ax2 = fig.add_subplot(2, 2, 2)
ax2.set_title("2. SEÇÃO TRANSVERSAL COM GÔNDOLA (Front View)\nTúnel com Nacela Central Submersa para o Hélice", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Dois cascos laterais
def draw_hull_sec(ax, cx):
    xs = [cx - bw, cx - bw*0.95, cx, cx + bw*0.95, cx + bw]
    ys = [0.85, -0.15, -0.55, -0.15, 0.85]
    ax.fill(xs, ys, color='#E2E8F0', edgecolor=c_hull, lw=1.8)
    ax.fill_between([cx - bw*0.9, cx + bw*0.9], [-0.55, -0.55], [0, 0], color='#BAE6FD', alpha=0.5)

draw_hull_sec(ax2, -1.39)
draw_hull_sec(ax2, 1.39)

# Ponte do convés
ax2.plot([-1.39 + bw, 1.39 - bw], [0.85, 0.85], color=c_hull, lw=2.5) # Piso convés
ax2.plot([-1.39 + bw, -0.5], [0.35, 0.35], color=c_tunnel, lw=1.5)    # Teto túnel esquerdo
ax2.plot([0.5, 1.39 - bw], [0.35, 0.35], color=c_tunnel, lw=1.5)     # Teto túnel direito

# Gôndola / Nacela Central em V suave (afunda na água até -0,40m)
pod_sec_x = [-0.5, -0.4, 0, 0.4, 0.5]
pod_sec_y = [0.35, -0.15, -0.45, -0.15, 0.35]
ax2.fill(pod_sec_x, pod_sec_y, color='#BAE6FD', edgecolor='#0369A1', lw=2)
# Motor dentro da gôndola
ax2.add_patch(patches.Rectangle((-0.3, -0.1), 0.6, 0.7, facecolor='#FED7AA', edgecolor=c_engine, lw=1.5))
ax2.text(0, 0.25, 'Motor Central\n(30 a 50 CV)', ha='center', va='center', fontsize=7, fontweight='bold', color=c_engine)

# Linha d'Água
ax2.axhline(0, color=c_wl, lw=1.8, linestyle='--', label="Linha d'Água (WL = 0)")

# Cotas
ax2.annotate('', xy=(-0.5, -0.65), xytext=(0.5, -0.65), arrowprops=dict(arrowstyle='<->', color='#0369A1', lw=1.2))
ax2.text(0, -0.80, 'Largura Gôndola: ~0,90 m', ha='center', va='top', fontsize=7.5, fontweight='bold', color='#0369A1')

ax2.set_xlim(-2.3, 2.3)
ax2.set_ylim(-1.0, 1.6)
ax2.set_aspect('equal')
ax2.set_xlabel("Largura (m)", fontsize=8)
ax2.set_ylabel("Altura (m)", fontsize=8)
ax2.legend(loc='lower right', fontsize=7)


# 3. VISTA LATERAL / CORTE LONGITUDINAL DA GÔNDOLA (Side Profile)
ax3 = fig.add_subplot(2, 1, 2)
ax3.set_title("3. CORTE LONGITUDINAL DA GÔNDOLA CENTRAL (Side View)", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Canoa lateral ao fundo (transparente)
x_canoa = [0, 0, 4.5, 7.5, 8.5, 8.2, 0]
y_canoa = [0.85, -0.55, -0.55, -0.30, 0.95, 0.95, 0.85]
ax3.plot(x_canoa, y_canoa, color='#94A3B8', lw=1.5, linestyle=':')

# Gôndola Central em perfil (começa na meia-nau x=4.2m e vai até a popa x=0m)
x_pod_prof = [0, 0, 2.5, 4.2, 3.8, 0]
y_pod_prof = [0.85, -0.45, -0.45, 0.35, 0.85, 0.85]
ax3.fill(x_pod_prof, y_pod_prof, color='#E0F2FE', edgecolor='#0369A1', lw=2.2, label="Gôndola Central (Nacela)")

# Motor Diesel Central dentro da gôndola
ax3.add_patch(patches.Rectangle((1.5, -0.2), 1.2, 0.65, facecolor='#FED7AA', edgecolor=c_engine, lw=1.8, zorder=3))
ax3.text(2.1, 0.12, 'Motor Diesel Centro (1x)', ha='center', va='center', fontsize=8, fontweight='bold', color=c_engine, zorder=4)

# Linha de Eixo e Reversor
ax3.add_patch(patches.Rectangle((1.2, -0.22), 0.3, 0.45, facecolor='#FDBA74', edgecolor=c_engine, lw=1.2, zorder=3))
ax3.plot([0.25, 1.3], [-0.40, -0.15], color=c_shaft, lw=4, zorder=2)
# Hélice
ax3.add_patch(patches.Rectangle((0.12, -0.55), 0.12, 0.30, facecolor='#F59E0B', edgecolor='#B45309', lw=1.2, zorder=3))
ax3.text(0.18, -0.65, 'Hélice Central', ha='center', va='top', fontsize=7, fontweight='bold', color='#B45309')
# Leme Central
ax3.add_patch(patches.Rectangle((-0.15, -0.55), 0.12, 0.45, facecolor='#475569', edgecolor='black', lw=1.2, zorder=3))
ax3.text(-0.10, -0.70, 'Leme', ha='center', va='top', fontsize=7, fontweight='bold', color='#475569')

# Skeg de proteção da gôndola
ax3.plot([-0.1, 1.8], [-0.55, -0.45], color=c_hull, lw=3.5)

# Linha d'Água
ax3.axhline(0, color=c_wl, lw=1.8, linestyle='--', label="Linha d'Água (WL = 0)")
ax3.fill_between([0, 4.2], [-0.45, 0], [0, 0], color='#BAE6FD', alpha=0.4)

# Textos
ax3.text(6.0, 0.3, 'Proa do Túnel Livre\n(Corta Marola Sem Bater)', ha='center', fontsize=8, fontweight='bold', color='#0369A1')

ax3.set_xlim(-0.8, 9.2)
ax3.set_ylim(-1.0, 1.5)
ax3.set_aspect('equal')
ax3.set_xlabel("Comprimento Longitudinal (metros)", fontsize=8)
ax3.set_ylabel("Altura (metros)", fontsize=8)
ax3.legend(loc='upper right', fontsize=7.5)

plt.tight_layout()
output_img = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/catamara_motor_central_gondola.png"
plt.savefig(output_img, dpi=300, bbox_inches='tight')
plt.close()
print("Desenho do catamara com motor central gerado com sucesso!")
