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

# Configurar prancha técnica no estilo Inboard Profile e Planta Baixa Naval
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(18, 14), dpi=300)

c_hull = '#0F172A'       # Contorno do casco
c_keel = '#1E293B'       # Quilha corrida / Lastro de chumbo
c_bulkhead = '#0F172A'   # Anteparas estruturais
c_wood = '#B45309'       # Mobiliário de madeira / Mesa
c_cushion = '#E2E8F0'    # Estofamento
c_wl = '#0284C7'         # Linha d'Água (WL)
c_engine = '#EA580C'     # Motor diesel
c_wc = '#BAE6FD'         # Banheiro
c_dim = '#DC2626'        # Cotas vermelhas

# =========================================================================
# 1. PERFIL EM CORTE LONGITUDINAL (INBOARD PROFILE — Idêntico à imagem)
# =========================================================================
ax1.set_title("1. PERFIL EM CORTE LONGITUDINAL (INBOARD PROFILE) — VELEIRO CLÁSSICO DE CRUZEIRO 8,50 m (28 ft)\nQuilha Corrida Integrada (Full Keel), Leme Protegido no Calcanhar, Mastro Passante e Motor sob a Escada", fontsize=10.5, fontweight='bold', color='#0F172A', pad=10)

# Linha do convés e casaria (Sheer + Trunk Cabin clássico com degrau)
xs_prof = np.linspace(0, 8.50, 150)
sheer_prof = 0.80 + 0.45 * (xs_prof / 8.50)**2.2

# Casaria com perfil clássico em degrau (Trunk Cabin)
# x = 2.40m a 6.20m
cab_prof_x = [2.40, 2.40, 4.20, 6.20, 6.20]
cab_prof_y = [0.85, 1.45, 1.40, 1.15, 0.98]

# Perfil da Quilha Corrida (Full Keel clássico de cruzeiro oceânico)
# x de 0 a 8.5m: Proa x=8.5 (y=1.25), Roda de proa curva descendo até quilha em x=5.5m (y=-0.80),
# Quilha profunda até x=2.5m (y=-1.40m), subindo para o calcanhar do leme em x=0.8m (y=-0.75m), espelho em x=0 (y=0.80m)
keel_prof_x = [0, 0, 0.80, 2.20, 4.50, 6.20, 7.80, 8.50]
keel_prof_y = [0.80, -0.20, -0.75, -1.40, -1.35, -0.80, 0.20, 1.25]

# Preenchimento do casco interno
ax1.fill(np.concatenate([xs_prof, [8.50, 7.80, 6.20, 4.50, 2.20, 0.80, 0, 0]]),
         np.concatenate([sheer_prof, [1.25, 0.20, -0.80, -1.35, -1.40, -0.75, -0.20, 0.80]]),
         color='#F8FAFC', edgecolor=c_hull, lw=2.0)

# Lastro de Chumbo / Ferro na Quilha Corrida (Abaixo de y = -0.70m)
ballast_x = [1.20, 2.00, 4.20, 5.20, 4.50, 2.20, 1.20]
ballast_y = [-0.70, -0.70, -0.70, -0.75, -1.35, -1.40, -0.70]
ax1.fill(ballast_x, ballast_y, color='#475569', edgecolor='black', lw=1.2, label="Lastro de Chumbo Integrado (1.400 kg)")
ax1.text(3.20, -1.05, "LASTRO DE QUILHA (CHUMBO / FERRO)", ha='center', va='center', fontsize=7.2, fontweight='bold', color='white')

# Linha d'Água (WL = 0,00 m)
ax1.axhline(0, color=c_wl, lw=1.5, linestyle='--', label="Linha d'Água (LWL = 6,80 m | Calado = 1,40 m)")

# Casaria desenhada
ax1.plot([2.40, 2.40, 4.20, 6.20, 6.20], [0.85, 1.45, 1.40, 1.15, 0.98], color=c_hull, lw=2.0)
ax1.fill([2.40, 2.40, 4.20, 6.20, 6.20, 2.40], [0.85, 1.45, 1.40, 1.15, 0.98, 0.85], color='#FEF3C7', alpha=0.5)

# Vigias na casaria
for vx in [2.90, 3.60, 4.40, 5.20]:
    ax1.add_patch(patches.Rectangle((vx, 1.10), 0.45, 0.18, facecolor='#38BDF8', edgecolor='black', lw=1.0))

# 1.1. Compartimentos Internos no Perfil
# Paiol da Amarra (Forepeak)
ax1.plot([7.50, 7.50], [0.10, 1.15], color=c_bulkhead, lw=2.0)
ax1.text(7.90, 0.65, "Paiol da\nAmarra", ha='center', fontsize=6.5, fontweight='bold', color='#475569')

# Beliche em V de Proa (Cama com colchão)
ax1.fill([6.00, 7.50, 7.50, 6.00], [0.45, 0.45, 0.55, 0.55], color=c_cushion, edgecolor='black', lw=1.0)
ax1.fill([6.00, 7.50, 7.50, 6.00], [-0.40, -0.40, 0.45, 0.45], color='#E2E8F0', edgecolor='black', lw=0.8)
ax1.text(6.75, 0.70, "Beliche de Proa (V-Berth)", ha='center', fontsize=7.0, fontweight='bold', color='#1E293B')
ax1.text(6.75, 0.05, "Paióis sob o leito / Água", ha='center', fontsize=6.2, color='#64748B')

# Mastro Passante (Keel-stepped Mast - apoiado na quilha)
ax1.plot([5.20, 5.20], [-0.75, 2.30], color=c_wood, lw=6, label="Mastro Passante apoiado na Quilha")
ax1.text(5.35, 2.10, "Mastro Passante", fontsize=7.0, fontweight='bold', color='#78350F')

# Antepara principal em x = 5.00 m
ax1.plot([5.00, 5.00], [-0.75, 1.35], color=c_bulkhead, lw=2.5)

# Salão Central (Sofá e Mesa de refeições)
ax1.fill([3.20, 4.90, 4.90, 3.20], [0.25, 0.25, 0.35, 0.35], color=c_cushion, edgecolor='black', lw=1.0)
ax1.fill([3.20, 4.90, 4.90, 3.20], [-0.50, -0.50, 0.25, 0.25], color='#FED7AA', edgecolor='black', lw=0.8)
# Mesa central
ax1.add_patch(patches.Rectangle((3.60, 0.35), 0.90, 0.35, facecolor=c_wood, edgecolor='black', lw=1.2))
ax1.text(4.05, 0.85, "Salão Central / Sofá-Cama", ha='center', fontsize=7.0, fontweight='bold', color='#1E293B')

# Cozinha (Fogão cardan e bancada)
ax1.add_patch(patches.Rectangle((2.55, 0.30), 0.60, 0.45, facecolor='#CBD5E1', edgecolor='black', lw=1.2))
ax1.text(2.85, 0.95, "Cozinha", ha='center', fontsize=6.8, fontweight='bold', color='#0F172A')

# Escada de acesso (Companionway Steps)
for step_i, (sx, sy) in enumerate([(2.45, 0.75), (2.35, 0.55), (2.25, 0.35), (2.15, 0.15)]):
    ax1.plot([sx, sx + 0.15], [sy, sy], color='#B45309', lw=3.0)

# Motor Diesel Auxiliar sob a escada (x = 1.30 a 2.10 m)
ax1.add_patch(patches.Rectangle((1.35, -0.25), 0.75, 0.55, facecolor='#FED7AA', edgecolor=c_engine, lw=1.5))
ax1.text(1.72, 0.05, "Motor Diesel\n(20 CV)", ha='center', va='center', fontsize=6.5, fontweight='bold', color=c_engine)

# Linha de eixo e Hélice no túnel do pé da quilha
ax1.plot([0.70, 1.40], [-0.55, -0.15], color='#64748B', lw=3.5)
ax1.add_patch(patches.Rectangle((0.55, -0.68), 0.12, 0.28, facecolor='#F59E0B', edgecolor='#9A3412', lw=1.0))

# Leme fixado no calcanhar da quilha (Keel-hung rudder)
ax1.add_patch(patches.Polygon([[0.05, 0.10], [0.65, -0.65], [0.75, -0.75], [0.10, -0.15]], closed=True, facecolor=c_wood, edgecolor='black', lw=1.5))
ax1.plot([0.10, 0.75], [-0.15, -0.75], color='black', lw=3.5, label="Eixo do Leme no Calcanhar")
ax1.text(0.35, -0.45, "Leme de\nCalcanhar", ha='center', fontsize=6.5, fontweight='bold', color='white')

# Cockpit Rebaixado e Paióis de Popa (Lazarette)
ax1.fill([0.50, 2.20, 2.20, 0.50], [0.20, 0.20, 0.75, 0.75], color='#E2E8F0', edgecolor='black', lw=1.2)
ax1.text(1.35, 0.45, "Cockpit Rebaixado\n(Auto-esgotante)", ha='center', va='center', fontsize=6.8, fontweight='bold', color='#475569')
ax1.text(0.25, 0.45, "Paiol de\nPopa", ha='center', fontsize=6.2, color='#64748B')

# Cotas
ax1.annotate('', xy=(0, -1.65), xytext=(8.50, -1.65), arrowprops=dict(arrowstyle='<->', color=c_dim, lw=1.5))
ax1.text(4.25, -1.82, "Comprimento Total (LOA) = 8,50 m (28 pés)", ha='center', va='top', fontsize=8.0, fontweight='bold', color=c_dim)

ax1.annotate('', xy=(3.80, 0), xytext=(3.80, -1.40), arrowprops=dict(arrowstyle='<->', color='#0284C7', lw=1.2))
ax1.text(3.95, -0.70, "Calado T = 1,40 m", ha='left', va='center', fontsize=7.5, fontweight='bold', color='#0284C7')

ax1.set_xlim(-0.6, 9.2)
ax1.set_ylim(-2.0, 2.6)
ax1.set_aspect('equal')
ax1.axis('off')
ax1.legend(loc='upper right', fontsize=7.2)


# =========================================================================
# 2. PLANTA BAIXA DE ARRANJO GERAL (ACORDO PERFEITO COM O CORTE)
# =========================================================================
ax2.set_title("2. PLANTA BAIXA DE ARRANJO GERAL (TOP-DOWN ACORDO PERFEITO)\nDisposição dos Ambientes Internos: Cabine de Proa em V, WC, Cozinha, Dinete e Cockpit", fontsize=10.5, fontweight='bold', color='#0F172A', pad=10)

# Contorno do Casco em Planta
w_half_plan = np.where(xs_prof < 4.5, 1.10 + 0.30 * (xs_prof / 4.5)**1.3, 1.40 * np.sqrt(np.maximum(0, 1 - ((xs_prof - 4.5)/4.0)**2)))
ax2.plot(xs_prof, w_half_plan, color=c_hull, lw=2.2)
ax2.plot(xs_prof, -w_half_plan, color=c_hull, lw=2.2)
ax2.plot([0, 0], [-1.10, 1.10], color=c_hull, lw=2.2)
ax2.fill_between(xs_prof, -w_half_plan, w_half_plan, color='#F8FAFC')
ax2.axhline(0, color='#94A3B8', lw=1.0, linestyle=':')

# 2.1. Cabine de Proa (V-Berth) — x = 5.80 a 8.20 m
ax2.plot([5.80, 5.80], [-1.22, 1.22], color=c_bulkhead, lw=2.5) # Antepara
ax2.fill([5.80, 5.80, 8.00, 8.00], [0.08, 1.22, 0.25, 0.08], color=c_cushion, edgecolor='black', lw=1.0)
ax2.fill([5.80, 5.80, 8.00, 8.00], [-0.08, -1.22, -0.25, -0.08], color=c_cushion, edgecolor='black', lw=1.0)
ax2.text(6.90, 0, "BELICHE EM 'V'\n(2 Pessoas)", ha='center', va='center', fontsize=7.0, fontweight='bold', color='#1E293B')

# Mastro Passante
ax2.add_patch(patches.Circle((5.20, 0), 0.12, facecolor=c_wood, edgecolor='black', lw=1.2, zorder=5))
ax2.text(5.20, -0.32, "Mastro", ha='center', fontsize=6.2, fontweight='bold', color='#78350F')

# 2.2. Banheiro Fechado (WC - Head) — Boreste / x = 4.40 a 5.80 m
ax2.plot([4.40, 4.40], [0.45, 1.38], color=c_bulkhead, lw=2.5)
ax2.plot([4.40, 5.80], [0.45, 0.45], color=c_bulkhead, lw=2.0)
ax2.fill([4.40, 4.40, 5.80, 5.80], [0.45, 1.38, 1.22, 0.45], color=c_wc, alpha=0.5)
ax2.add_patch(patches.Ellipse((4.90, 0.95), 0.38, 0.28, facecolor='white', edgecolor='black', lw=1.0))
ax2.text(5.10, 1.20, "WC / Banheiro", ha='center', fontsize=6.5, fontweight='bold', color='#0369A1')

# 2.3. Cozinha Linear (Galley) — Bombordo / x = 4.30 a 5.80 m
ax2.add_patch(patches.Rectangle((4.40, -1.35), 1.40, 0.55, facecolor='#CBD5E1', edgecolor='black', lw=1.0))
ax2.add_patch(patches.Rectangle((4.50, -1.25), 0.35, 0.35, facecolor='white', edgecolor='black', lw=0.8)) # Pia
ax2.add_patch(patches.Rectangle((5.05, -1.25), 0.55, 0.35, facecolor='#FEF3C7', edgecolor='black', lw=0.8)) # Fogão
ax2.text(5.10, -0.65, "Cozinha (Pia + Fogão)", ha='center', fontsize=6.5, fontweight='bold', color='#B45309')

# 2.4. Salão com Sofás e Mesa Central — x = 2.40 a 4.40 m
ax2.add_patch(patches.Rectangle((2.50, 0.65), 1.80, 0.65, facecolor=c_cushion, edgecolor='black', lw=1.0)) # Sofá BE
ax2.add_patch(patches.Rectangle((2.50, -1.30), 1.70, 0.60, facecolor=c_cushion, edgecolor='black', lw=1.0)) # Sofá BB
ax2.add_patch(patches.Rectangle((2.85, -0.35), 1.10, 0.70, facecolor=c_wood, edgecolor='black', lw=1.2)) # Mesa
ax2.text(3.40, 0, "MESA DINETE", ha='center', va='center', fontsize=6.8, fontweight='bold', color='white')
ax2.text(3.40, 0.98, "Sofá / Cama BE", ha='center', va='center', fontsize=6.5, color='#1E293B')
ax2.text(3.35, -1.00, "Sofá BB", ha='center', va='center', fontsize=6.5, color='#1E293B')

# 2.5. Antepara da Casaria, Escada e Cockpit — x = 0 a 2.40 m
ax2.plot([2.40, 2.40], [-1.22, -0.35], color=c_bulkhead, lw=2.5)
ax2.plot([2.40, 2.40], [0.35, 1.22], color=c_bulkhead, lw=2.5)
for step_x in [2.30, 2.15, 2.00]:
    ax2.plot([step_x, step_x], [-0.30, 0.30], color='#B45309', lw=2.0)
ax2.text(2.15, 0, "Escada", ha='center', va='center', fontsize=5.8, fontweight='bold', color='#78350F')

# Cockpit externo com bancos
ax2.add_patch(patches.Rectangle((0.30, 0.50), 1.70, 0.55, facecolor='#E2E8F0', edgecolor='black', lw=1.0))
ax2.add_patch(patches.Rectangle((0.30, -1.05), 1.70, 0.55, facecolor='#E2E8F0', edgecolor='black', lw=1.0))
ax2.add_patch(patches.Circle((1.10, 0), 0.28, facecolor='none', edgecolor=c_hull, lw=1.8)) # Timão
ax2.text(1.10, -0.42, "Timão", ha='center', fontsize=6.5, fontweight='bold', color=c_hull)

# Cotas na planta
ax2.annotate('', xy=(0, -1.70), xytext=(8.50, -1.70), arrowprops=dict(arrowstyle='<->', color=c_dim, lw=1.5))
ax2.text(4.25, -1.88, "Comprimento Total (LOA) = 8,50 m | Boca Máxima (B) = 2,80 m", ha='center', va='top', fontsize=8.0, fontweight='bold', color=c_dim)

ax2.set_xlim(-0.6, 9.2)
ax2.set_ylim(-2.1, 2.1)
ax2.set_aspect('equal')
ax2.axis('off')

plt.tight_layout()
output_veleiro_inboard = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/planta_e_perfil_veleiro_quilha_corrida.png"
plt.savefig(output_veleiro_inboard, dpi=300, bbox_inches='tight')
plt.close()
print("Prancha de Perfil e Planta Baixa gerada com sucesso!")
