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

# Configurar prancha técnica estilo CAD Naval de alta precisão
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig = plt.figure(figsize=(18, 11), dpi=300)

c_hull = '#0F172A'       # Contorno do casco
c_bulkhead = '#1E293B'   # Anteparas estruturais espessas
c_cockpit = '#F5E6CA'    # Tom bege/teca da praça de popa (como na imagem original)
c_cushion = '#E2E8F0'    # Estofados / Colchões
c_wood = '#B45309'       # Madeiramento / Mesas
c_wc = '#BAE6FD'         # Banheiro
c_galley = '#CBD5E1'     # Cozinha inox / bancada
c_dim = '#DC2626'        # Cotas vermelhas

ax = fig.add_subplot(1, 1, 1)
ax.set_title("PLANTA BAIXA DE ARRANJO GERAL (GENERAL ARRANGEMENT) — EMBARCAÇÃO CABINADA 8,50 m × 2,90 m\nCockpit Aberto em Teca, Salão com Dinete e Cozinha, Posto de Comando à Ré do Para-brisa, WC Fechado e Suíte de Proa", fontsize=11, fontweight='bold', color='#0F172A', pad=12)

# Orientação idêntica à imagem: Popa na esquerda (x = 0 m), Proa na direita (x = 8,50 m)
# Boca Máxima = 2,90 m (semi-boca = 1,45 m)
x_hull = np.linspace(0, 8.50, 160)
w_half = np.where(x_hull < 4.8, 1.25 + 0.20 * (x_hull / 4.8)**1.3, 1.45 * np.sqrt(np.maximum(0, 1 - ((x_hull - 4.8) / 3.7)**2)))

# Contorno externo do casco
ax.plot(x_hull, w_half, color=c_hull, lw=2.5)
ax.plot(x_hull, -w_half, color=c_hull, lw=2.5)
ax.plot([0, 0], [-1.25, 1.25], color=c_hull, lw=2.5) # Espelho de popa reto

# Plataforma de Banho / Popa (x = -0,40 a 0,0 m)
ax.fill([-0.40, 0, 0, -0.40], [-1.15, -1.25, 1.25, 1.15], color='#E2E8F0', edgecolor=c_hull, lw=1.5)
ax.text(-0.20, 0, "Plataforma de Popa", rotation=90, ha='center', va='center', fontsize=6.5, fontweight='bold', color='#475569')

# Fundo geral
ax.fill_between(x_hull, -w_half, w_half, color='#FFFFFF')
ax.axhline(0, color='#94A3B8', lw=0.8, linestyle=':') # Linha de centro

# Púlpito de proa e rolete de âncora (x = 8.50 a 8.85 m)
ax.plot([8.50, 8.85, 8.50], [0.15, 0, -0.15], color=c_hull, lw=2.0)
ax.add_patch(patches.Circle((8.65, 0), 0.08, facecolor='white', edgecolor='black', lw=1.0))

# =========================================================================
# 1. PRAÇA DE POPA / COCKPIT ABERTO (TOM BEGE / TECA) — x = 0,0 a 2,60 m
# =========================================================================
# Piso do cockpit em tom bege (como na imagem enviada)
x_cockpit = np.linspace(0, 2.60, 60)
w_cp = np.interp(x_cockpit, x_hull, w_half)
ax.fill_between(x_cockpit, -w_cp + 0.15, w_cp - 0.15, color=c_cockpit, edgecolor='#D97706', lw=1.0)

# Ripas do piso de teca no cockpit
for rx in np.arange(0.20, 2.60, 0.20):
    ax.plot([rx, rx], [-np.interp(rx, x_hull, w_half) + 0.15, np.interp(rx, x_hull, w_half) - 0.15], color='#D97706', lw=0.5, linestyle=':')

# Bancos perimétricos / de popa
ax.add_patch(patches.Rectangle((0.05, -1.10), 0.45, 2.20, facecolor=c_cushion, edgecolor='#475569', lw=1.0))
ax.text(0.25, 0, "Banco de Popa", rotation=90, ha='center', va='center', fontsize=6.5, fontweight='bold', color='#475569')

# Caixa / Alçapão do motor sob o piso
ax.add_patch(patches.Rectangle((0.80, -0.55), 1.10, 1.10, facecolor='none', edgecolor='#DC2626', lw=1.2, linestyle='--'))
ax.text(1.35, 0, "ALÇAPÃO DO MOTOR DIESEL\n(Sob o Piso do Cockpit)", ha='center', va='center', fontsize=6.2, fontweight='bold', color='#B91C1C')

ax.text(2.0, 0.70, "PRAÇA DE POPA\n(Cockpit Aberto em Teca)", ha='center', fontsize=7.0, fontweight='bold', color='#78350F')

# =========================================================================
# 2. ANTEPARA DE FECHAMENTO DO CASARIO — x = 2,60 m (Porta Corrediça)
# =========================================================================
w_at_260 = np.interp(2.60, x_hull, w_half)
ax.plot([2.60, 2.60], [-w_at_260, -0.35], color=c_bulkhead, lw=3.0)
ax.plot([2.60, 2.60], [0.35, w_at_260], color=c_bulkhead, lw=3.0)
# Porta de correr de vidro
ax.plot([2.58, 2.58], [-0.35, 0.35], color='#38BDF8', lw=2.5)
ax.text(2.60, 0, "Porta de Correr", rotation=90, ha='center', va='center', fontsize=5.8, fontweight='bold', color='#0284C7')

# =========================================================================
# 3. SALÃO PRINCIPAL / CASARIO (PILOTHOUSE) — x = 2,60 a 5,40 m
# =========================================================================
# 3.1. Bombordo (Lado Superior no Desenho): DINETE / SALÃO DE REFEIÇÕES
# Sofá em U com mesa central
dinette_pts = [[2.70, 0.45], [2.70, 1.35], [5.20, 1.35], [5.20, 0.45], [4.60, 0.45], [4.60, 0.85], [3.30, 0.85], [3.30, 0.45]]
poly_dinette = patches.Polygon(dinette_pts, closed=True, facecolor=c_cushion, edgecolor='#475569', lw=1.2)
ax.add_patch(poly_dinette)
# Mesa de jantar no centro da dinete
mesa_dinette = patches.Rectangle((3.40, 0.45), 1.10, 0.75, facecolor=c_wood, edgecolor='#78350F', lw=1.5)
ax.add_patch(mesa_dinette)
ax.text(3.95, 0.82, "MESA DINETE\n(Refeições)", ha='center', va='center', fontsize=6.5, fontweight='bold', color='white')
ax.text(3.95, 1.15, "Sofá em U (Converte em Cama)", ha='center', va='center', fontsize=6.2, color='#1E293B')

# 3.2. Boreste (Lado Inferior no Desenho): COZINHA LINEAR (GALLEY) — x = 2,70 a 4,00 m
ax.add_patch(patches.Rectangle((2.70, -1.35), 1.30, 0.60, facecolor=c_galley, edgecolor='#475569', lw=1.2))
# Pia com cuba redonda
ax.add_patch(patches.Circle((3.05, -1.05), 0.16, facecolor='white', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((3.05, -1.05), 0.04, facecolor='#38BDF8'))
# Fogão cooktop de 2 bocas
ax.add_patch(patches.Rectangle((3.35, -1.25), 0.50, 0.40, facecolor='#CBD5E1', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((3.48, -1.05), 0.09, facecolor='#DC2626'))
ax.add_patch(patches.Circle((3.72, -1.05), 0.09, facecolor='#DC2626'))
ax.text(3.35, -0.65, "Cozinha (Pia + Cooktop)", ha='center', fontsize=6.2, fontweight='bold', color='#1E293B')

# 3.3. Boreste: POSTO DE COMANDO INTERNO (HELM STATION) — x = 4,00 a 5,40 m
# Console de comando / painel de instrumentos
ax.add_patch(patches.Rectangle((4.80, -1.38), 0.55, 0.85, facecolor='#1E293B', edgecolor='black', lw=1.2))
# Volante / Timão
ax.add_patch(patches.Circle((4.75, -0.95), 0.20, facecolor='none', edgecolor='black', lw=1.8))
ax.plot([4.75, 4.75], [-1.15, -0.75], color='black', lw=1.2)
ax.plot([4.55, 4.95], [-0.95, -0.95], color='black', lw=1.2)
# Cadeira giratória ergonômica do piloto
ax.add_patch(patches.Circle((4.30, -0.95), 0.24, facecolor='#F8FAFC', edgecolor=c_hull, lw=1.5))
ax.add_patch(patches.Rectangle((4.04, -1.15), 0.08, 0.40, facecolor=c_hull, edgecolor='black', lw=1.0)) # Encosto
ax.text(4.45, -0.60, "Posto de Comando (Piloto)", ha='center', fontsize=6.5, fontweight='bold', color=c_hull)

# Corredor central de circulação no salão
ax.text(3.30, 0, "Circulação Salão", ha='center', va='center', fontsize=5.8, color='#64748B')

# =========================================================================
# 4. ANTEPARA FRONTAL DO SALÃO E DESCIDA — x = 5,40 m
# =========================================================================
w_at_540 = np.interp(5.40, x_hull, w_half)
ax.plot([5.40, 5.40], [-w_at_540, -0.30], color=c_bulkhead, lw=3.0)
ax.plot([5.40, 5.40], [0.30, w_at_540], color=c_bulkhead, lw=3.0)
# Degraus de descida para a proa
for st_x in [5.30, 5.42, 5.54]:
    ax.plot([st_x, st_x], [-0.25, 0.25], color='#B45309', lw=2.0)
ax.text(5.42, 0, "Descida", ha='center', va='center', fontsize=5.5, fontweight='bold', color='#78350F')

# =========================================================================
# 5. BANHEIRO FECHADO (WC) E ARMARINHO — x = 5,40 a 6,50 m
# =========================================================================
# Banheiro privativo a Bombordo (Lado Superior)
ax.plot([6.50, 6.50], [0.40, np.interp(6.50, x_hull, w_half)], color=c_bulkhead, lw=2.5)
ax.plot([5.40, 6.50], [0.40, 0.40], color=c_bulkhead, lw=2.0) # Parede da porta
ax.fill([5.40, 5.40, 6.50, 6.50], [0.40, np.interp(5.40, x_hull, w_half), np.interp(6.50, x_hull, w_half), 0.40], color=c_wc, alpha=0.55)
# Vaso sanitário marítimo
ax.add_patch(patches.Ellipse((5.85, 0.90), 0.36, 0.28, facecolor='white', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((5.85, 0.90), 0.08, facecolor='#38BDF8'))
# Lavatório de canto
ax.add_patch(patches.Rectangle((6.15, 0.50), 0.30, 0.35, facecolor='white', edgecolor='black', lw=0.8))
ax.add_patch(patches.Circle((6.30, 0.67), 0.08, facecolor='#38BDF8'))
ax.text(5.95, 1.20, "Banheiro / WC", ha='center', fontsize=6.5, fontweight='bold', color='#0369A1')

# Armário / Closet a Boreste (Lado Inferior)
ax.add_patch(patches.Rectangle((5.50, -1.25), 0.90, 0.60, facecolor='#FEF3C7', edgecolor='#B45309', lw=1.2))
ax.text(5.95, -0.95, "Armário / Rouparia\n(Hanging Locker)", ha='center', va='center', fontsize=6.0, fontweight='bold', color='#78350F')

# =========================================================================
# 6. CABINE DE CASAL DE PROA (V-BERTH / SUÍTE) — x = 6,50 a 8,40 m
# =========================================================================
# Antepara de fechamento em x = 6,50 m
ax.plot([6.50, 6.50], [-np.interp(6.50, x_hull, w_half), 0.40], color=c_bulkhead, lw=2.5)

# Cama triangular de casal em V
v_pts_be = [[6.50, 0.06], [6.50, 1.15], [8.30, 0.20], [8.30, 0.06]]
poly_v_be = patches.Polygon(v_pts_be, closed=True, facecolor=c_cushion, edgecolor='#475569', lw=1.2)
ax.add_patch(poly_v_be)

v_pts_bb = [[6.50, -0.06], [6.50, -1.15], [8.30, -0.20], [8.30, -0.06]]
poly_v_bb = patches.Polygon(v_pts_bb, closed=True, facecolor=c_cushion, edgecolor='#475569', lw=1.2)
ax.add_patch(poly_v_bb)

# Costuras do colchão de proa
for vx in [7.00, 7.50, 7.90]:
    w_at = np.interp(vx, x_hull, w_half) * 0.70
    ax.plot([vx, vx], [0.06, w_at], color='#94A3B8', lw=0.8, linestyle='--')
    ax.plot([vx, vx], [-0.06, -w_at], color='#94A3B8', lw=0.8, linestyle='--')

ax.text(7.35, 0, "CAMAROTE DE PROA\n(Cama de Casal em V)", ha='center', va='center', fontsize=7.2, fontweight='bold', color='#1E293B')
ax.text(8.15, 0, "Paiol", ha='center', va='center', fontsize=5.5, color='#64748B')

# =========================================================================
# COTAS TÉCNICAS LONGITUDINAIS E TRANSVERSAIS
# =========================================================================
# Comprimento Total (LOA) = 8,50 m
ax.annotate('', xy=(0, -1.85), xytext=(8.50, -1.85), arrowprops=dict(arrowstyle='<->', color=c_dim, lw=1.5))
ax.text(4.25, -2.02, "Comprimento Total (LOA) = 8,50 m (28 pés)", ha='center', va='top', fontsize=8.5, fontweight='bold', color=c_dim)

# Boca Máxima = 2,90 m
ax.annotate('', xy=(4.80, -1.45), xytext=(4.80, 1.45), arrowprops=dict(arrowstyle='<->', color=c_dim, lw=1.5))
ax.text(5.00, 0, "Boca Máxima (B) = 2,90 m", ha='left', va='center', fontsize=8.0, fontweight='bold', color=c_dim, rotation=90)

# Divisão dos 4 Grandes Setores
# 1. Cockpit: 2,60 m
ax.annotate('', xy=(0, 1.75), xytext=(2.60, 1.75), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(1.30, 1.88, "Praça de Popa (2,60 m)", ha='center', fontsize=7.0, fontweight='bold')

# 2. Salão / Comando: 2,80 m
ax.annotate('', xy=(2.60, 1.75), xytext=(5.40, 1.75), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(4.00, 1.88, "Salão / Cozinha / Posto de Comando (2,80 m)", ha='center', fontsize=7.0, fontweight='bold')

# 3. Banheiro e Armário: 1,10 m
ax.annotate('', xy=(5.40, 1.75), xytext=(6.50, 1.75), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(5.95, 1.88, "WC (1,10 m)", ha='center', fontsize=7.0, fontweight='bold')

# 4. Cabine de Proa: 2,00 m
ax.annotate('', xy=(6.50, 1.75), xytext=(8.50, 1.75), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(7.50, 1.88, "Proa (2,00 m)", ha='center', fontsize=7.0, fontweight='bold')

ax.set_xlim(-0.8, 9.5)
ax.set_ylim(-2.3, 2.3)
ax.set_aspect('equal')
ax.axis('off')

plt.tight_layout()
output_planta_trawler = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/planta_arranjo_trawler_lobster_8_5m.png"
plt.savefig(output_planta_trawler, dpi=300, bbox_inches='tight')
plt.close()
print("Planta Arranjo Geral Trawler/Lobster gerada com sucesso!")
