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

# Configurar prancha técnica estilo CAD Naval
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 preto sólido
c_bulkhead = '#1E293B'   # Anteparas estruturais escuras
c_cushion = '#E2E8F0'    # Estofamento dos beliches/sofás
c_wood = '#D97706'       # Mesas / Bancadas de madeira
c_wc = '#BAE6FD'         # Sanitário / Área molhada
c_galley = '#FED7AA'     # Cozinha / Fogão
c_deck = '#F8FAFC'       # Piso / Passadiço
c_dim = '#DC2626'        # Cotas vermelhas

ax = fig.add_subplot(1, 1, 1)
ax.set_title("PLANTA BAIXA DE ARRANJO GERAL INTERNO (GENERAL ARRANGEMENT PLAN)\nEmbarcação Cabinada de 8,50 m × 2,85 m | Proa em V, Banheiro Fechado, Cozinha, Sofá/Dinete e Cockpit", fontsize=11, fontweight='bold', color='#0F172A', pad=12)

# Orientação: Popa na esquerda (x=0 m) e Proa na direita (x=8.50 m)
# Meia-boca máxima de 1.425 m (Boca = 2.85 m)
x_hull = np.linspace(0, 8.50, 150)
w_half = np.where(x_hull < 4.5, 1.15 + 0.275 * (x_hull / 4.5)**1.3, 1.425 * np.sqrt(np.maximum(0, 1 - ((x_hull - 4.5) / 4.0)**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.15, 1.15], color=c_hull, lw=2.5) # Espelho de popa

# Preenchimento do piso do convés
ax.fill_between(x_hull, -w_half, w_half, color=c_deck, alpha=0.9)

# Linha de Centro tracejada (Centerline)
ax.axhline(0, color='#94A3B8', lw=1.0, linestyle=':')

# Púlpito de Proa / Ferragem de ancoragem (x = 8.5 a 8.9m)
ax.plot([8.5, 8.85, 8.5], [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))

# -------------------------------------------------------------------------
# 1. CABINE DE PROA (V-BERTH / BELICHE EM "V") — x = 5.80 m a 8.40 m
# -------------------------------------------------------------------------
# Antepara de proa em x = 5.80 m
ax.plot([5.80, 5.80], [-w_half[int(5.80/8.5*150)], w_half[int(5.80/8.5*150)]], color=c_bulkhead, lw=3.0)

# Almofadas em V (Leito duplo de pernoite)
v_pts_be = [[5.80, 0.08], [5.80, 1.25], [8.20, 0.25], [8.20, 0.08]]
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 = [[5.80, -0.08], [5.80, -1.25], [8.20, -0.25], [8.20, -0.08]]
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 em V
for v_x in [6.30, 6.80, 7.30, 7.80]:
    w_at = np.interp(v_x, x_hull, w_half) * 0.75
    ax.plot([v_x, v_x], [0.08, w_at], color='#94A3B8', lw=0.8, linestyle='--')
    ax.plot([v_x, v_x], [-0.08, -w_at], color='#94A3B8', lw=0.8, linestyle='--')

ax.text(7.0, 0, "BELICHE EM 'V'\n(Cama de Casal de Proa)", ha='center', va='center', fontsize=7.5, fontweight='bold', color='#1E293B')

# -------------------------------------------------------------------------
# 2. BANHEIRO FECHADO / WC MARÍTIMO (HEAD) — Boreste / Superior (x = 4.40 m a 5.80 m)
# -------------------------------------------------------------------------
# Antepara transversal em x = 4.40 m (lado de boreste)
ax.plot([4.40, 4.40], [0.45, w_half[int(4.40/8.5*150)]], color=c_bulkhead, lw=3.0)
# Parede longitudinal divisória com porta
ax.plot([4.40, 5.80], [0.45, 0.45], color=c_bulkhead, lw=2.5)

# Área do WC preenchida
wc_pts = [[4.40, 0.45], [4.40, 1.42], [5.80, 1.25], [5.80, 0.45]]
ax.add_patch(patches.Polygon(wc_pts, closed=True, facecolor=c_wc, alpha=0.5, edgecolor='none'))

# Vaso sanitário marítimo
ax.add_patch(patches.Ellipse((4.85, 0.95), 0.40, 0.30, facecolor='white', edgecolor='black', lw=1.2))
ax.add_patch(patches.Circle((4.85, 0.95), 0.10, facecolor='#BAE6FD', edgecolor='black', lw=0.8))
# Lavatório / Cuba
ax.add_patch(patches.Rectangle((5.35, 0.60), 0.35, 0.55, facecolor='white', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((5.52, 0.88), 0.12, facecolor='#38BDF8', edgecolor='black', lw=0.8))
ax.text(5.10, 1.25, "WC / Banheiro", ha='center', fontsize=7.0, fontweight='bold', color='#0369A1')

# -------------------------------------------------------------------------
# 3. COZINHA LINEAR (GALLEY) — Bombordo / Inferior (x = 4.20 m a 5.80 m)
# -------------------------------------------------------------------------
galley_w = 0.55
y_galley = -np.interp(5.0, x_hull, w_half)
ax.add_patch(patches.Rectangle((4.30, y_galley), 1.50, galley_w, facecolor=c_galley, edgecolor='#B45309', lw=1.2))

# Cuba da pia (Sink)
ax.add_patch(patches.Rectangle((4.45, y_galley + 0.10), 0.38, 0.35, facecolor='white', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((4.64, y_galley + 0.27), 0.05, facecolor='#38BDF8'))

# Fogão náutico de 2 bocas (Stove)
ax.add_patch(patches.Rectangle((5.05, y_galley + 0.08), 0.55, 0.38, facecolor='#CBD5E1', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((5.20, y_galley + 0.27), 0.10, facecolor='#DC2626', edgecolor='black'))
ax.add_patch(patches.Circle((5.45, y_galley + 0.27), 0.10, facecolor='#DC2626', edgecolor='black'))
ax.text(5.05, y_galley - 0.18, "Cozinha (Pia + Fogão)", ha='center', fontsize=7.0, fontweight='bold', color='#B45309')

# -------------------------------------------------------------------------
# 4. SALÃO PRINCIPAL (DINETE COM SOFÁ E MESA) — x = 2.40 m a 4.40 m
# -------------------------------------------------------------------------
# Sofá longitudinal a boreste (Settee)
sofa_be = patches.Rectangle((2.50, 0.65), 1.80, 0.65, facecolor=c_cushion, edgecolor='#475569', lw=1.2)
ax.add_patch(sofa_be)
for sx in [2.95, 3.40, 3.85]:
    ax.plot([sx, sx], [0.65, 1.30], color='#94A3B8', lw=0.8, linestyle='--')

# Sofá em U ou L a bombordo
sofa_bb = patches.Rectangle((2.50, -1.30), 1.70, 0.60, facecolor=c_cushion, edgecolor='#475569', lw=1.2)
ax.add_patch(sofa_bb)
for sx in [2.90, 3.35, 3.80]:
    ax.plot([sx, sx], [-1.30, -0.70], color='#94A3B8', lw=0.8, linestyle='--')

# Mesa central de refeições (Dinette Table)
table = patches.Rectangle((2.80, -0.35), 1.10, 0.70, facecolor=c_wood, edgecolor='#78350F', lw=1.5)
ax.add_patch(table)
ax.text(3.35, 0, "MESA DE REFEIÇÕES\n(Dinete Central)", ha='center', va='center', fontsize=7.0, fontweight='bold', color='white')
ax.text(3.40, 0.98, "Sofá / Cama Boreste", ha='center', va='center', fontsize=6.8, color='#1E293B')
ax.text(3.35, -1.00, "Sofá Bombordo", ha='center', va='center', fontsize=6.8, color='#1E293B')

# -------------------------------------------------------------------------
# 5. ANTEPARA DE POPA E COCKPIT EXTERNO — x = 0.0 m a 2.40 m
# -------------------------------------------------------------------------
# Antepara da cabine em x = 2.40 m com porta central
ax.plot([2.40, 2.40], [-w_half[int(2.40/8.5*150)], -0.35], color=c_bulkhead, lw=3.0)
ax.plot([2.40, 2.40], [0.35, w_half[int(2.40/8.5*150)]], color=c_bulkhead, lw=3.0)
# Escada de descida da cabine (Companionway Steps)
for step_x in [2.35, 2.20, 2.05]:
    ax.plot([step_x, step_x], [-0.30, 0.30], color='#B45309', lw=2.0)
ax.text(2.20, 0, "Escada", ha='center', va='center', fontsize=6.0, fontweight='bold', color='#78350F')

# Cockpit rebaixado com bancos laterais
# Banco lateral Boreste
ax.add_patch(patches.Rectangle((0.30, 0.55), 1.70, 0.55, facecolor='#E2E8F0', edgecolor='#475569', lw=1.2))
# Banco lateral Bombordo
ax.add_patch(patches.Rectangle((0.30, -1.10), 1.70, 0.55, facecolor='#E2E8F0', edgecolor='#475569', lw=1.2))

# Posto de comando / Console do timão (Roda de Leme no poço)
ax.add_patch(patches.Circle((1.10, 0), 0.28, facecolor='none', edgecolor=c_hull, lw=2.0))
ax.plot([1.10, 1.10], [-0.28, 0.28], color=c_hull, lw=1.5)
ax.plot([0.82, 1.38], [0, 0], color=c_hull, lw=1.5)
ax.text(1.10, -0.45, "Posto de Comando (Timão)", ha='center', fontsize=6.8, fontweight='bold', color=c_hull)

ax.text(1.15, 0.82, "Banco Cockpit BE", ha='center', fontsize=6.5, color='#475569')
ax.text(1.15, -0.82, "Banco Cockpit BB", ha='center', fontsize=6.5, color='#475569')

# Caixa de motor de centro / Paiol sob o piso do cockpit (x = 1.6 a 2.3m)
ax.add_patch(patches.Rectangle((1.55, -0.35), 0.75, 0.70, facecolor='none', edgecolor='#DC2626', lw=1.2, linestyle='--'))
ax.text(1.92, 0.45, "Compartimento Motor (Sob Piso)", ha='center', fontsize=6.0, fontweight='bold', color='#DC2626')

# -------------------------------------------------------------------------
# COTAS GERAIS DE ARQUITETURA NAVAL
# -------------------------------------------------------------------------
# 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", ha='center', va='top', fontsize=8.5, fontweight='bold', color=c_dim)

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

# Cotas dos Compartimentos
# Cabine de Proa: 2,70 m
ax.annotate('', xy=(5.80, 1.70), xytext=(8.50, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(7.15, 1.82, "Proa (2,70 m)", ha='center', fontsize=7.2, fontweight='bold')

# Salão / Cozinha: 3,40 m
ax.annotate('', xy=(2.40, 1.70), xytext=(5.80, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(4.10, 1.82, "Salão / Cozinha / WC (3,40 m)", ha='center', fontsize=7.2, fontweight='bold')

# Cockpit de Popa: 2,40 m
ax.annotate('', xy=(0, 1.70), xytext=(2.40, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(1.20, 1.82, "Cockpit (2,40 m)", ha='center', fontsize=7.2, 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 = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/arranjo_geral_planta_baixa_8_5m.png"
plt.savefig(output_planta, dpi=300, bbox_inches='tight')
plt.close()
print("Planta baixa similar gerada com sucesso!")
