import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path
import numpy as np

# Configurar estilo náutico/técnico
plt.style.use('default')
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(16, 11), gridspec_kw={'height_ratios': [1.1, 1]})
fig.patch.set_facecolor('#F8F9FA')

# ==========================================
# 1. PERFIL LATERAL / PLANO VÉLICO (KETCH)
# ==========================================
ax1.set_facecolor('#FFFFFF')
ax1.set_title("Veleiro Ketch 40' - Perfil Lateral & Plano Vélico Preliminar\n(Cockpit Central, Quilha Semi-Longa, Leme em Skeg)", fontsize=13, fontweight='bold', color='#0B2545', pad=12)

# Linha d'água (DWL)
ax1.axhline(0, color='#3A86FF', linestyle='--', linewidth=1.2, label="Linha d'Água de Projeto (DWL)")
ax1.fill_between([-1, 13], 0, -3, color='#EBF2FA', alpha=0.6)

# Linha do Casco (Perfil)
# X de 0 (popa) a 12.2 (proa)
hull_x = [0.0, 0.4, 1.2, 3.5, 6.0, 9.0, 11.5, 12.2, 12.0, 11.0, 8.0, 4.0, 0.8, 0.0]
hull_y = [1.2, 0.8, 1.3, 1.4, 1.35, 1.45, 1.7, 1.85, 1.3, 0.0, -0.6, -0.65, -0.2, 1.2]

# Casco Acima e Abaixo da Linha d'água
casco_poly = patches.Polygon([
    (0.0, 1.2), (0.4, 0.8), (1.2, 1.3), (3.5, 1.45), (5.0, 1.4), (7.0, 1.4), (9.0, 1.5), (11.5, 1.7), (12.2, 1.9),
    (12.0, 0.0), (10.5, -0.5), (8.5, -0.65), (7.5, -1.70), (4.5, -1.70), (3.8, -0.65), (1.5, -0.5), (0.8, -0.1), (0.0, 1.2)
], closed=True, edgecolor='#0B2545', facecolor='#D0DBE5', linewidth=2, zorder=3)
ax1.add_patch(casco_poly)

# Quilha Semi-Longa (Lastro)
quilha_poly = patches.Polygon([
    (4.5, -0.65), (7.5, -0.65), (7.5, -1.70), (4.5, -1.70)
], closed=True, edgecolor='#0B2545', facecolor='#4A6B82', linewidth=1.5, zorder=4)
ax1.add_patch(quilha_poly)
ax1.text(6.0, -1.2, "Quilha Semi-Longa\n(Lastro 3.5 t)", color='white', fontweight='bold', fontsize=8, ha='center', va='center', zorder=5)

# Leme em Skeg
skeg_poly = patches.Polygon([
    (2.0, -0.55), (2.3, -0.55), (2.0, -1.5), (1.8, -1.5)
], closed=True, edgecolor='#0B2545', facecolor='#4A6B82', linewidth=1.5, zorder=4)
leme_poly = patches.Polygon([
    (1.4, -0.55), (1.9, -0.55), (1.7, -1.5), (1.2, -1.4)
], closed=True, edgecolor='#0B2545', facecolor='#8DA9C4', linewidth=1.5, zorder=4)
ax1.add_patch(skeg_poly)
ax1.add_patch(leme_poly)
ax1.text(1.6, -1.0, "Leme +\nSkeg", color='#0B2545', fontweight='bold', fontsize=7.5, ha='center', va='center')

# Cabine e Cockpit Central no Perfil
roof_poly = patches.Polygon([
    (1.2, 1.3), (3.2, 1.6), (4.2, 1.6), (4.5, 1.25), (6.8, 1.25), (7.2, 1.65), (9.8, 1.65), (10.2, 1.55)
], closed=False, edgecolor='#0B2545', linewidth=2, zorder=4)
ax1.add_patch(roof_poly)
ax1.text(5.5, 1.45, "Cockpit Central\n(Protegido)", color='#0B2545', fontweight='bold', fontsize=8, ha='center', va='center')
ax1.text(2.2, 1.45, "Cabine Popa", color='#0B2545', fontsize=7.5, ha='center')
ax1.text(8.5, 1.45, "Salão / Proa", color='#0B2545', fontsize=7.5, ha='center')

# Mastros Ketch
# Mastro Principal (Main Mast) em X = 7.2 m, Altura = 14.5 m
ax1.plot([7.2, 7.2], [1.65, 15.0], color='#2B2D42', linewidth=4, zorder=5)
# Retranca Principal
ax1.plot([7.2, 2.5], [2.6, 2.8], color='#2B2D42', linewidth=3, zorder=5)
# Vela Grande (Mainsail)
ax1.fill([7.2, 7.2, 2.7], [2.8, 14.5, 3.0], color='#F4F1DE', edgecolor='#E07A5F', linewidth=1.5, alpha=0.85, zorder=2)
ax1.text(5.6, 7.0, "Vela Grande\n~36 m²", color='#B04A30', fontweight='bold', fontsize=8.5, ha='center')

# Mastro de Mezena (Mizzen Mast) em X = 3.2 m, Altura = 10.0 m
ax1.plot([3.2, 3.2], [1.6, 10.5], color='#2B2D42', linewidth=3.2, zorder=5)
# Retranca Mezena
ax1.plot([3.2, 0.4], [2.4, 2.5], color='#2B2D42', linewidth=2.5, zorder=5)
# Vela de Mezena (Mizzen)
ax1.fill([3.2, 3.2, 0.6], [2.5, 10.0, 2.6], color='#F4F1DE', edgecolor='#E07A5F', linewidth=1.5, alpha=0.85, zorder=2)
ax1.text(2.3, 5.5, "Mezena\n~16 m²", color='#B04A30', fontweight='bold', fontsize=8, ha='center')

# Estai de Proa e Vela de Proa (Genoa)
ax1.plot([7.2, 12.2], [14.5, 1.9], color='#2B2D42', linewidth=1.2, linestyle=':', zorder=3)
ax1.fill([7.2, 12.0, 7.3], [14.3, 2.1, 2.4], color='#F4F1DE', edgecolor='#E07A5F', linewidth=1.5, alpha=0.85, zorder=2)
ax1.text(9.2, 6.5, "Genoa / Buja\n~42 m²", color='#B04A30', fontweight='bold', fontsize=8.5, ha='center')

# Cotas no Perfil
ax1.annotate("", xy=(0, -2.1), xytext=(12.2, -2.1), arrowprops=dict(arrowstyle="<->", color="#0B2545", lw=1.2))
ax1.text(6.1, -2.35, "LOA = 12.20 m (40.0')", color='#0B2545', fontweight='bold', fontsize=9, ha='center')

ax1.set_xlim(-0.8, 13.0)
ax1.set_ylim(-2.6, 16.0)
ax1.set_ylabel("Altura / Calado (m)", fontsize=9, fontweight='bold', color='#0B2545')
ax1.grid(True, linestyle=':', alpha=0.4)
ax1.tick_params(colors='#0B2545')

# ==========================================
# 2. PLANTA BAIXA / ARRANJO GERAL (2 SUÍTES)
# ==========================================
ax2.set_facecolor('#FFFFFF')
ax2.set_title("Arranjo Geral Interno - Planta Baixa (2 Suítes + Salão + Cozinha em Corredor)", fontsize=13, fontweight='bold', color='#0B2545', pad=12)

# Contorno do Casco em Planta (Boca = 3.90 m -> Y de -1.95 a +1.95)
x_p = np.linspace(0, 12.2, 100)
# Curva da boca máxima
y_p = 1.95 * np.sin(np.pi * (x_p / 12.2) ** 0.65)

ax2.plot(x_p, y_p, color='#0B2545', linewidth=2.2)
ax2.plot(x_p, -y_p, color='#0B2545', linewidth=2.2)
ax2.plot([0, 0], [-y_p[0], y_p[0]], color='#0B2545', linewidth=2.2) # Popa aberta/espelho

# Preenchimento do Casco
ax2.fill_between(x_p, -y_p, y_p, color='#F0F4F8', zorder=1)

# Divisões / Anteparas (Bulkheads)
ante_x = [0.8, 3.4, 4.4, 7.0, 9.8, 11.5]
for x in ante_x:
    idx = np.argmin(np.abs(x_p - x))
    w = y_p[idx]
    ax2.plot([x, x], [-w, w], color='#134074', linewidth=1.5, linestyle='-', zorder=3)

# 1. Bico de Proa - Paiol de Âncora / Amarras
ax2.text(11.8, 0, "Paiol\nÂncora", color='#555', fontsize=7.5, ha='center', va='center', fontweight='bold')

# 2. Suíte de Proa (X de 9.8 a 11.5)
# Cama de Casal em V
ax2.fill([9.9, 11.3, 11.3, 9.9], [0.9, 0.4, -0.4, -0.9], color='#D8E2DC', edgecolor='#9D8189', lw=1, zorder=2)
ax2.text(10.6, 0, "Cama Casal (V)\nSuíte Proa", color='#1D2D44', fontsize=7.5, ha='center', va='center', fontweight='bold')
# Banheiro Privativo Proa (BE)
ax2.fill([8.2, 9.8, 9.8, 8.2], [-0.6, -0.6, -1.8, -1.7], color='#FFE5D9', edgecolor='#D08C5C', lw=1, zorder=2)
ax2.text(9.0, -1.2, "Suíte 1 (WC Proa)\n(Pia + Vaso + Ducha)", color='#8D4004', fontsize=7, ha='center', va='center', fontweight='bold')

# 3. Salão Principal (X de 7.0 a 9.8)
# Mesa em U a Bombordo (BB)
ax2.fill([7.2, 9.6, 9.6, 7.2], [1.8, 1.8, 0.5, 0.5], color='#D8E2DC', edgecolor='#406E8E', lw=1, zorder=2)
ax2.fill([7.8, 9.0, 9.0, 7.8], [1.5, 1.5, 0.8, 0.8], color='#E9ECEF', edgecolor='#406E8E', lw=1, zorder=3)
ax2.text(8.4, 1.15, "Mesa de Refeições\nem 'U' (Salão)", color='#1D2D44', fontsize=7.5, ha='center', va='center', fontweight='bold')

# Mesa de Navegação / Escritório a Estibordo (BE)
ax2.fill([7.2, 8.0, 8.0, 7.2], [-0.6, -0.6, -1.7, -1.6], color='#E2EAFC', edgecolor='#3A86FF', lw=1, zorder=2)
ax2.text(7.6, -1.15, "Mesa Nav. /\nHome-Office", color='#1D2D44', fontsize=7, ha='center', va='center', fontweight='bold')

# 4. Centro / Cockpit Central & Motor (X de 4.4 a 7.0)
# Cockpit Central (Acima) / Praça de Máquinas (Abaixo do piso)
ax2.fill([4.5, 6.9, 6.9, 4.5], [0.9, 0.9, -0.9, -0.9], color='#FFF1E6', edgecolor='#C9184A', lw=1.5, linestyle='--', zorder=3)
ax2.text(5.7, 0, "COCKPIT CENTRAL (Convés)\n& Motor Diesel 50 HP (Porão)", color='#C9184A', fontsize=8, ha='center', va='center', fontweight='bold')

# Cozinha em Corredor a Bombordo (BB)
ax2.fill([4.5, 6.9, 6.9, 4.5], [1.0, 1.0, 1.85, 1.85], color='#D8F3DC', edgecolor='#2D6A4F', lw=1, zorder=2)
ax2.text(5.7, 1.45, "Cozinha (Galley)\nFogão Cardan + Pia + Gelad.", color='#1B4332', fontsize=7.5, ha='center', va='center', fontweight='bold')

# Passagem a Estibordo (BE)
ax2.text(5.7, -1.45, "Corredor Técnico /\nPaiol & Acesso", color='#495057', fontsize=7, ha='center', va='center')

# Escada Central
for y_step in np.linspace(-0.35, 0.35, 5):
    ax2.plot([6.9, 7.15], [y_step, y_step], color='#0B2545', lw=1.5, zorder=4)
ax2.text(7.02, 0.45, "Escada", color='#0B2545', fontsize=6.5, ha='center', fontweight='bold')

# 5. Suíte Master de Popa (X de 0.8 a 3.4)
# Cama de Casal Master
ax2.fill([1.0, 3.2, 3.2, 1.0], [0.8, 0.8, -0.8, -0.8], color='#D8E2DC', edgecolor='#9D8189', lw=1.2, zorder=2)
ax2.text(2.1, 0, "Cama Casal Central\nSUÍTE MASTER POPA", color='#1D2D44', fontsize=8, ha='center', va='center', fontweight='bold')
# Banheiro Master com Box (BB)
ax2.fill([1.5, 3.3, 3.3, 1.5], [0.9, 0.9, 1.75, 1.75], color='#FFE5D9', edgecolor='#D08C5C', lw=1, zorder=2)
ax2.text(2.4, 1.35, "Suíte 2 Master\n(WC + Box Separado)", color='#8D4004', fontsize=7, ha='center', va='center', fontweight='bold')

# 6. Popa Aberta & Solário (X de 0.0 a 0.8)
ax2.text(0.4, 0, "Plataforma\nPopa Aberta", color='#0B2545', fontsize=7.5, ha='center', va='center', fontweight='bold')

# Legendas de Lados
ax2.text(12.0, 2.1, "BOMBORDO (BB)", color='#0B2545', fontsize=8, fontweight='bold', ha='right')
ax2.text(12.0, -2.1, "ESTIBORDO / BORESTE (BE)", color='#0B2545', fontsize=8, fontweight='bold', ha='right')

# Cotas em Planta
ax2.annotate("", xy=(0, -2.4), xytext=(12.2, -2.4), arrowprops=dict(arrowstyle="<->", color="#0B2545", lw=1.2))
ax2.text(6.1, -2.65, "LOA = 12.20 m (40.0')", color='#0B2545', fontweight='bold', fontsize=9, ha='center')

ax2.annotate("", xy=(6.1, -1.95), xytext=(6.1, 1.95), arrowprops=dict(arrowstyle="<->", color="#0B2545", lw=1.2))
ax2.text(6.35, 1.1, "Boca Máx = 3.90 m", color='#0B2545', fontweight='bold', fontsize=8, va='center', rotation=90)

ax2.set_xlim(-0.8, 13.0)
ax2.set_ylim(-2.8, 2.6)
ax2.set_xlabel("Comprimento Longitudinal (m)", fontsize=9, fontweight='bold', color='#0B2545')
ax2.set_ylabel("Meia-Boca (m)", fontsize=9, fontweight='bold', color='#0B2545')
ax2.grid(True, linestyle=':', alpha=0.4)
ax2.tick_params(colors='#0B2545')

plt.tight_layout()
output_img = "/home/magdalena/eee/alunos/andre-veleiro-40pe/desenho_tecnico_veleiro_40ft_ketch.png"
plt.savefig(output_img, dpi=300, bbox_inches='tight')
print("Desenho gerado com sucesso em:", output_img)
