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

# Configurar prancha técnica estilo CAD Naval colorida
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
c_v_berth = '#86EFAC'    # Verde claro (Camarote de Proa)
c_head = '#F472B6'       # Rosa (Banheiro / WC)
c_closet = '#FBCFE8'     # Rosa claro (Armário / Hanging Locker)
c_saloon = '#FEF08A'     # Amarelo claro (Salão / Dinete)
c_galley = '#CBD5E1'     # Cinza claro (Cozinha e Navegação)
c_quarter = '#93C5FD'    # Azul claro (Beliches de Popa / Quarter Berths)
c_engine = '#FED7AA'     # Laranja claro (Caixa do motor)
c_wood = '#B45309'       # Mesa de madeira
c_dim = '#DC2626'        # Cotas vermelhas

ax = fig.add_subplot(1, 1, 1)
ax.set_title("PLANTA BAIXA DE ARRANJO GERAL INTERNO (INTERIOR LAYOUT PLAN) — 8,50 m × 2,85 m\nLayout Colorido: Proa em V (Verde), WC/Armário (Rosa), Salão (Amarelo), Cozinha/Nav (Cinza) e 2 Quarter Berths (Azul)", 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

# Fundo geral
ax.fill_between(x_hull, -w_half, w_half, color='#F8FAFC', alpha=0.9)
ax.axhline(0, color='#94A3B8', lw=0.8, linestyle=':')

# Púlpito de Proa
ax.plot([8.5, 8.85, 8.5], [0.15, 0, -0.15], color=c_hull, lw=2.0)

# =========================================================================
# 1. PROA: V-BERTH / CAMAROTE DE PROA (VERDE) — x = 6.00 a 8.30 m
# =========================================================================
# Antepara em x = 6.00 m
ax.plot([6.00, 6.00], [-w_half[int(6.00/8.5*150)], w_half[int(6.00/8.5*150)]], color=c_bulkhead, lw=3.0)

# Almofada V-berth Boreste
v_pts_be = [[6.00, 0.08], [6.00, 1.20], [8.10, 0.25], [8.10, 0.08]]
poly_v_be = patches.Polygon(v_pts_be, closed=True, facecolor=c_v_berth, edgecolor='#15803D', lw=1.2)
ax.add_patch(poly_v_be)

# Almofada V-berth Bombordo
v_pts_bb = [[6.00, -0.08], [6.00, -1.20], [8.10, -0.25], [8.10, -0.08]]
poly_v_bb = patches.Polygon(v_pts_bb, closed=True, facecolor=c_v_berth, edgecolor='#15803D', lw=1.2)
ax.add_patch(poly_v_bb)

# Costuras do colchão
for vx in [6.50, 7.00, 7.50]:
    w_at = np.interp(vx, x_hull, w_half) * 0.75
    ax.plot([vx, vx], [0.08, w_at], color='#15803D', lw=0.8, linestyle='--')
    ax.plot([vx, vx], [-0.08, -w_at], color='#15803D', lw=0.8, linestyle='--')

ax.text(7.05, 0, "CAMAROTE DE PROA\n(V-Berth — 2 Pessoas)", ha='center', va='center', fontsize=7.2, fontweight='bold', color='#14532D')

# =========================================================================
# 2. BANHEIRO (WC) & ARMÁRIO / ROUPARIA (ROSA) — x = 5.10 a 6.00 m
# =========================================================================
# Antepara em x = 5.10 m
ax.plot([5.10, 5.10], [-w_half[int(5.10/8.5*150)], w_half[int(5.10/8.5*150)]], color=c_bulkhead, lw=2.5)

# Banheiro (Boreste / Superior)
wc_pts = [[5.10, 0.45], [5.10, 1.38], [6.00, 1.20], [6.00, 0.45]]
ax.add_patch(patches.Polygon(wc_pts, closed=True, facecolor=c_head, edgecolor='#BE185D', lw=1.2))
ax.plot([5.10, 6.00], [0.45, 0.45], color=c_bulkhead, lw=2.0) # Parede da porta
ax.add_patch(patches.Ellipse((5.55, 0.95), 0.38, 0.28, facecolor='white', edgecolor='black', lw=1.0))
ax.add_patch(patches.Circle((5.55, 0.95), 0.08, facecolor='#BAE6FD'))
ax.text(5.55, 1.22, "WC / Banheiro", ha='center', fontsize=6.5, fontweight='bold', color='#831843')

# Armário / Closet / Rouparia (Bombordo / Inferior)
closet_pts = [[5.10, -0.45], [5.10, -1.38], [6.00, -1.20], [6.00, -0.45]]
ax.add_patch(patches.Polygon(closet_pts, closed=True, facecolor=c_closet, edgecolor='#DB2777', lw=1.2))
ax.plot([5.10, 6.00], [-0.45, -0.45], color=c_bulkhead, lw=2.0)
ax.text(5.55, -0.90, "Armário / Closet\n(Hanging Locker)", ha='center', va='center', fontsize=6.2, fontweight='bold', color='#9D174D')

# Corredor central de passagem
ax.text(5.55, 0, "Passagem", ha='center', va='center', fontsize=5.8, color='#64748B')

# =========================================================================
# 3. SALÃO PRINCIPAL / DINETE (AMARELO) — x = 3.20 a 5.10 m
# =========================================================================
# Sofá a Boreste (Settee)
sofa_be_pts = [[3.20, 0.70], [3.20, 1.40], [5.10, 1.38], [5.10, 0.70]]
ax.add_patch(patches.Polygon(sofa_be_pts, closed=True, facecolor=c_saloon, edgecolor='#CA8A04', lw=1.2))
for sx in [3.70, 4.20, 4.70]:
    ax.plot([sx, sx], [0.70, 1.40], color='#CA8A04', lw=0.8, linestyle='--')

# Sofá em U a Bombordo (Dinette)
sofa_bb_pts = [[3.20, -0.70], [3.20, -1.40], [5.10, -1.38], [5.10, -0.70]]
ax.add_patch(patches.Polygon(sofa_bb_pts, closed=True, facecolor=c_saloon, edgecolor='#CA8A04', lw=1.2))
for sx in [3.70, 4.20, 4.70]:
    ax.plot([sx, sx], [-0.70, -1.40], color='#CA8A04', lw=0.8, linestyle='--')

# Mesa Central de Refeições com abas rebatíveis (Drop-leaf Table)
table = patches.Rectangle((3.60, -0.40), 1.10, 0.80, facecolor=c_wood, edgecolor='#78350F', lw=1.5)
ax.add_patch(table)
ax.text(4.15, 0, "MESA DINETE\n(Abas Dobráveis)", ha='center', va='center', fontsize=6.8, fontweight='bold', color='white')

# Mastro / Apoio de compressão (junto à mesa)
ax.add_patch(patches.Circle((4.75, 0), 0.10, facecolor='#1E293B', edgecolor='black', lw=1.0))
ax.text(4.75, -0.22, "Mastro", ha='center', fontsize=5.8, fontweight='bold', color='#1E293B')

ax.text(4.15, 1.05, "Sofá-Cama Boreste", ha='center', va='center', fontsize=6.5, color='#713F12')
ax.text(4.15, -1.05, "Sofá-Cama Bombordo", ha='center', va='center', fontsize=6.5, color='#713F12')

# =========================================================================
# 4. COZINHA (GALLEY) E MESA DE NAVEGAÇÃO (CINZA) — x = 2.10 a 3.20 m
# =========================================================================
# Antepara em x = 2.10 m (início da descida do cockpit)
ax.plot([2.10, 2.10], [-w_half[int(2.10/8.5*150)], w_half[int(2.10/8.5*150)]], color=c_bulkhead, lw=2.5)

# Cozinha em L a Bombordo (Galley)
galley_pts = [[2.10, -0.65], [2.10, -1.35], [3.20, -1.35], [3.20, -0.65]]
ax.add_patch(patches.Polygon(galley_pts, closed=True, facecolor=c_galley, edgecolor='#475569', lw=1.2))
# Pia (Sink)
ax.add_patch(patches.Rectangle((2.30, -1.25), 0.35, 0.32, facecolor='white', edgecolor='black', lw=0.8))
ax.add_patch(patches.Circle((2.48, -1.09), 0.05, facecolor='#38BDF8'))
# Fogão com cardan (Stove)
ax.add_patch(patches.Rectangle((2.75, -1.28), 0.40, 0.38, facecolor='#FEF3C7', edgecolor='black', lw=0.8))
ax.add_patch(patches.Circle((2.87, -1.09), 0.08, facecolor='#DC2626'))
ax.add_patch(patches.Circle((3.03, -1.09), 0.08, facecolor='#DC2626'))
ax.text(2.65, -0.85, "Cozinha (Galley)\nPia + Fogão Cardan", ha='center', fontsize=6.2, fontweight='bold', color='#1E293B')

# Mesa de Navegação a Boreste (Chart Table)
chart_pts = [[2.10, 0.65], [2.10, 1.35], [3.20, 1.35], [3.20, 0.65]]
ax.add_patch(patches.Polygon(chart_pts, closed=True, facecolor=c_galley, edgecolor='#475569', lw=1.2))
ax.add_patch(patches.Rectangle((2.35, 0.75), 0.75, 0.50, facecolor='#FEF3C7', edgecolor='black', lw=0.8))
ax.text(2.72, 1.00, "Mesa de Cartas\ne Painel Elétrico", ha='center', va='center', fontsize=6.2, fontweight='bold', color='#1E293B')

# =========================================================================
# 5. POPA: MOTOR CENTRAL E 2 BELICHES DE QUARTO (AZUL) — x = 0 a 2.10 m
# =========================================================================
# Caixa do motor central sob a escada (Engine Box)
engine_box = patches.Rectangle((1.30, -0.35), 0.80, 0.70, facecolor=c_engine, edgecolor='#EA580C', lw=1.5)
ax.add_patch(engine_box)
# Degraus da escada sobre a caixa do motor
for st_x in [1.95, 1.80, 1.65, 1.50]:
    ax.plot([st_x, st_x], [-0.30, 0.30], color='#B45309', lw=2.0)
ax.text(1.70, 0, "MOTOR DIESEL\n(Sob Escada)", ha='center', va='center', fontsize=6.0, fontweight='bold', color='#C2410C')

# Beliche de Quarto Boreste (Quarter Berth BE — Azul)
qb_be_pts = [[0.20, 0.45], [0.20, 1.15], [2.10, 1.35], [2.10, 0.45]]
poly_qb_be = patches.Polygon(qb_be_pts, closed=True, facecolor=c_quarter, edgecolor='#1D4ED8', lw=1.2)
ax.add_patch(poly_qb_be)
for qx in [0.70, 1.20, 1.70]:
    w_at = np.interp(qx, x_hull, w_half)
    ax.plot([qx, qx], [0.45, w_at], color='#1D4ED8', lw=0.8, linestyle='--')
ax.text(1.15, 0.85, "QUARTER BERTH (BE)\n(Cama de Popa — 1 Pessoa)", ha='center', va='center', fontsize=6.2, fontweight='bold', color='#1E3A8A')

# Beliche de Quarto Bombordo (Quarter Berth BB — Azul)
qb_bb_pts = [[0.20, -0.45], [0.20, -1.15], [2.10, -1.35], [2.10, -0.45]]
poly_qb_bb = patches.Polygon(qb_bb_pts, closed=True, facecolor=c_quarter, edgecolor='#1D4ED8', lw=1.2)
ax.add_patch(poly_qb_bb)
for qx in [0.70, 1.20, 1.70]:
    w_at = np.interp(qx, x_hull, w_half)
    ax.plot([qx, qx], [-0.45, -w_at], color='#1D4ED8', lw=0.8, linestyle='--')
ax.text(1.15, -0.85, "QUARTER BERTH (BB)\n(Cama de Popa — 1 Pessoa)", ha='center', va='center', fontsize=6.2, fontweight='bold', color='#1E3A8A')

# =========================================================================
# 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 (28 pés)", 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 longitudinais dos ambientes
# Proa: 2,50 m
ax.annotate('', xy=(6.00, 1.70), xytext=(8.50, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(7.25, 1.82, "Camarote Proa (2,50 m)", ha='center', fontsize=7.0, fontweight='bold')

# WC / Armário: 0,90 m
ax.annotate('', xy=(5.10, 1.70), xytext=(6.00, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(5.55, 1.82, "WC / Closet (0,90 m)", ha='center', fontsize=7.0, fontweight='bold')

# Salão: 1,90 m
ax.annotate('', xy=(3.20, 1.70), xytext=(5.10, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(4.15, 1.82, "Salão Dinete (1,90 m)", ha='center', fontsize=7.0, fontweight='bold')

# Cozinha / Navegação: 1,10 m
ax.annotate('', xy=(2.10, 1.70), xytext=(3.20, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(2.65, 1.82, "Cozinha/Nav (1,10 m)", ha='center', fontsize=7.0, fontweight='bold')

# Quarter Berths na Popa: 2,10 m
ax.annotate('', xy=(0, 1.70), xytext=(2.10, 1.70), arrowprops=dict(arrowstyle='<->', color='#0F172A', lw=1.0))
ax.text(1.05, 1.82, "Camas de Popa (2,10 m)", ha='center', fontsize=7.0, fontweight='bold')

# Legenda de Cores
box_leg = (
    "LEGENDA DO ARRANJO GERAL (PERNOITE 5 A 6 PESSOAS):\n"
    "■ VERDE:  Camarote de Proa (Cama de Casal em V — 2 pessoas)\n"
    "■ ROSA:   Banheiro Marítimo Fechado (WC) + Armário Rouparia\n"
    "■ AMARELO: Salão Principal com Dinete Central (2 camas de solteiro)\n"
    "■ CINZA:  Cozinha em L (Pia + Fogão cardan) + Mesa de Navegação\n"
    "■ AZUL:   2 Beliches de Popa / Quarter Berths (2 pessoas deitadas)\n"
    "■ LARANJA: Praça de Máquinas (Motor Diesel 15–20 CV sob a escada)"
)
ax.text(0.1, -1.45, box_leg, fontsize=7.2, family='monospace', color='#0F172A',
        bbox=dict(boxstyle='square,pad=0.5', facecolor='#FEF3C7', edgecolor='#D97706', lw=1.0))

ax.set_xlim(-0.6, 9.2)
ax.set_ylim(-2.2, 2.2)
ax.set_aspect('equal')
ax.axis('off')

plt.tight_layout()
output_planta_colorida = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/arranjo_geral_planta_colorida_8_5m.png"
plt.savefig(output_planta_colorida, dpi=300, bbox_inches='tight')
plt.close()
print("Planta colorida gerada com sucesso!")
