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

# Copy sketch photo
os.system("cp /home/magdalena/.hermes/cache/images/img_1d5f6e275b4d.jpg /home/magdalena/eee/alunos/rafael-rosa-batista/croqui_papel_quadriculado_ciclone2.jpg")

# 7-station sketch
stations_x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
half_breadth_deck = np.array([0.80, 1.08, 1.22, 1.25, 1.23, 1.05, 0.65, 0.00])
half_breadth_dwl  = np.array([0.65, 0.95, 1.12, 1.18, 1.15, 0.92, 0.50, 0.00])
half_breadth_wl1  = np.array([0.45, 0.70, 0.88, 0.95, 0.92, 0.70, 0.32, 0.00])

poly_deck = np.poly1d(np.polyfit(stations_x, half_breadth_deck, 4))
poly_dwl  = np.poly1d(np.polyfit(stations_x, half_breadth_dwl, 4))
poly_wl1  = np.poly1d(np.polyfit(stations_x, half_breadth_wl1, 4))

x_fine = np.linspace(0.0, 7.0, 200)
y_deck = np.clip(poly_deck(x_fine), 0, None)
y_dwl = np.clip(poly_dwl(x_fine), 0, None)
y_wl1 = np.clip(poly_wl1(x_fine), 0, None)
y_deck[-1] = 0.0
y_dwl[-1] = 0.0
y_wl1[-1] = 0.0

plt.style.use('default')
fig = plt.figure(figsize=(16, 12), facecolor='#F8FAFC')
gs = fig.add_gridspec(3, 2, height_ratios=[1.1, 1.1, 1.2], width_ratios=[2.2, 1.0], hspace=0.35, wspace=0.25)

ax_profile = fig.add_subplot(gs[0, 0])
ax_plan = fig.add_subplot(gs[1, 0])
ax_body = fig.add_subplot(gs[2, 0])
ax_info = fig.add_subplot(gs[:, 1])

# 1. SHEER PLAN
ax_profile.set_facecolor('#FFFFFF')
ax_profile.set_title("1. PERFIL LATERAL (SHEER PLAN) - SAVEIRO CICLONE 2 (7,0 m)", fontsize=11, fontweight='bold', color='#1A365D', pad=8)
ax_profile.axhline(0.60, color='#2B6CB0', linestyle='--', linewidth=1.2, label="Linha d'Agua DWL (Calado 0,60 m)")
ax_profile.fill_between([-0.5, 7.5], 0.60, -0.2, color='#EBF8FF', alpha=0.5)

sheer_x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
sheer_z = np.array([1.25, 1.15, 1.10, 1.08, 1.12, 1.22, 1.40, 1.65])
poly_sheer = np.poly1d(np.polyfit(sheer_x, sheer_z, 3))
z_sheer_fine = poly_sheer(x_fine)

keel_x = np.array([0.0, 0.5, 1.5, 3.5, 5.5, 6.5, 7.0])
keel_z = np.array([0.00, 0.00, 0.00, 0.02, 0.15, 0.65, 1.65])
poly_keel = np.poly1d(np.polyfit(keel_x, keel_z, 4))
z_keel_fine = np.clip(poly_keel(x_fine), 0, None)
z_keel_fine[-1] = z_sheer_fine[-1]

ax_profile.plot(x_fine, z_sheer_fine, color='#1A365D', linewidth=2.2, label="Borda / Linha de Tosamento")
ax_profile.plot(x_fine, z_keel_fine, color='#1A365D', linewidth=2.2, label="Quilha / Roda de Proa")
ax_profile.plot([0.0, 0.0], [z_keel_fine[0], z_sheer_fine[0]], color='#1A365D', linewidth=2.2, label="Espelho de Popa")
ax_profile.fill_between(x_fine, z_keel_fine, z_sheer_fine, color='#CBD5E0', alpha=0.35)

for st in stations_x:
    ax_profile.axvline(st, color='#A0AEC0', linestyle=':', linewidth=0.8)
    ax_profile.text(st, -0.12, f"Bal. {int(st)}", color='#4A5568', fontsize=7.5, ha='center')

ax_profile.plot([7.0, 8.5], [1.65, 1.75], color='#744210', linewidth=3, label="Botalo de Proa (1,50 m)")
ax_profile.plot([4.2, 4.2], [1.10, 7.5], color='#744210', linewidth=3.5, label="Mastro de Madeira (~7,5 m)")
ax_profile.annotate("", xy=(0, 1.85), xytext=(7.0, 1.85), arrowprops=dict(arrowstyle="<->", color="#1A365D", lw=1.2))
ax_profile.text(3.5, 1.95, "LOA = 7,00 m (23,0')", color='#1A365D', fontweight='bold', fontsize=8.5, ha='center')

ax_profile.set_xlim(-0.5, 8.8)
ax_profile.set_ylim(-0.25, 7.8)
ax_profile.set_ylabel("Altura Z (m)", fontsize=8.5, fontweight='bold', color='#1A365D')
ax_profile.legend(loc='upper right', fontsize=7.5, framealpha=0.9)
ax_profile.grid(True, linestyle=':', alpha=0.5)

# 2. HALF-BREADTH PLAN
ax_plan.set_facecolor('#FFFFFF')
ax_plan.set_title("2. PLANTA DE LINHAS D'AGUA E CONVES (HALF-BREADTH PLAN)", fontsize=11, fontweight='bold', color='#1A365D', pad=8)
ax_plan.axhline(0, color='#E53E3E', linestyle='-.', linewidth=1.2, label="Linha de Centro (CL)")

ax_plan.plot(x_fine, y_deck, color='#1A365D', linewidth=2.2, label="Conves / Borda (Bmax = 2,50 m)")
ax_plan.plot(x_fine, -y_deck, color='#1A365D', linewidth=2.2)

ax_plan.plot(x_fine, y_dwl, color='#2B6CB0', linestyle='--', linewidth=1.4, label="Linha d'Agua DWL (Calado 0,60 m)")
ax_plan.plot(x_fine, -y_dwl, color='#2B6CB0', linestyle='--', linewidth=1.4)

ax_plan.plot(x_fine, y_wl1, color='#4FD1C5', linestyle=':', linewidth=1.2, label="Linha d'Agua WL 0,30 m")
ax_plan.plot(x_fine, -y_wl1, color='#4FD1C5', linestyle=':', linewidth=1.2)

ax_plan.plot([0, 0], [-half_breadth_deck[0], half_breadth_deck[0]], color='#1A365D', linewidth=2.2)
ax_plan.fill_between(x_fine, -y_deck, y_deck, color='#E2E8F0', alpha=0.4)

for st, y_val in zip(stations_x, half_breadth_deck):
    ax_plan.plot([st, st], [-y_val, y_val], color='#A0AEC0', linestyle=':', linewidth=0.8)
    ax_plan.plot(st, y_val, 'o', color='#2B6CB0', markersize=3.5)
    ax_plan.plot(st, -y_val, 'o', color='#2B6CB0', markersize=3.5)
    ax_plan.text(st, -1.55, f"Bal. {int(st)}", color='#4A5568', fontsize=7.5, ha='center')

ax_plan.annotate("", xy=(3.0, -1.25), xytext=(3.0, 1.25), arrowprops=dict(arrowstyle="<->", color="#C53030", lw=1.5))
ax_plan.text(3.15, 0.0, "Boca Max = 2,50 m\n(Baliza 3)", color='#C53030', fontweight='bold', fontsize=8, va='center')

ax_plan.annotate("", xy=(0.0, -0.80), xytext=(0.0, 0.80), arrowprops=dict(arrowstyle="<->", color="#1A365D", lw=1.0))
ax_plan.text(-0.15, 0.0, "Espelho Popa\n1,60 m", color='#1A365D', fontsize=7.5, ha='right', va='center')

ax_plan.set_xlim(-0.8, 7.8)
ax_plan.set_ylim(-1.75, 1.75)
ax_plan.set_xlabel("Comprimento Longitudinal X (m) [Balizas 0 a 7]", fontsize=8.5, fontweight='bold', color='#1A365D')
ax_plan.set_ylabel("Semi-Boca Y (m)", fontsize=8.5, fontweight='bold', color='#1A365D')
ax_plan.legend(loc='upper right', fontsize=7.5, framealpha=0.9)
ax_plan.grid(True, linestyle=':', alpha=0.5)

# 3. BODY PLAN
ax_body.set_facecolor('#FFFFFF')
ax_body.set_title("3. PLANO DE BALIZAS (BODY PLAN / SECOES TRANSVERSAIS)", fontsize=11, fontweight='bold', color='#1A365D', pad=8)
ax_body.axvline(0, color='#E53E3E', linestyle='-.', linewidth=1.2)
ax_body.axhline(0.60, color='#2B6CB0', linestyle='--', linewidth=1.0, label="DWL 0,60 m")

colors_sec = ['#2D3748', '#4A5568', '#718096', '#E53E3E', '#3182CE', '#00B4D8', '#48BB78', '#38A169']

for i in range(8):
    x_pos = stations_x[i]
    b_top = half_breadth_deck[i]
    z_top = poly_sheer(x_pos)
    z_bot = max(0.0, poly_keel(x_pos))
    
    z_pts = np.linspace(z_bot, z_top, 30)
    normalized_z = (z_pts - z_bot) / (z_top - z_bot + 1e-5)
    y_pts = b_top * (normalized_z ** 0.55)
    
    if i <= 3:
        ax_body.plot(-y_pts, z_pts, label=f"Bal. {i} (Popa)", color=colors_sec[i], linewidth=1.6)
        ax_body.text(-y_pts[-1]-0.05, z_pts[-1], f"{i}", fontsize=7.5, fontweight='bold', color=colors_sec[i], ha='right')
    else:
        ax_body.plot(y_pts, z_pts, label=f"Bal. {i} (Proa)", color=colors_sec[i], linewidth=1.6)
        ax_body.text(y_pts[-1]+0.05, z_pts[-1], f"{i}", fontsize=7.5, fontweight='bold', color=colors_sec[i], ha='left')

ax_body.text(-0.7, 0.15, "SECOES DE POPA\n(Balizas 0 a 3)", color='#4A5568', fontsize=8, fontweight='bold', ha='center')
ax_body.text(0.7, 0.15, "SECOES DE PROA\n(Balizas 4 a 7)", color='#3182CE', fontsize=8, fontweight='bold', ha='center')

ax_body.set_xlim(-1.5, 1.5)
ax_body.set_ylim(-0.1, 1.8)
ax_body.set_xlabel("Semi-Boca Y (m) [Bombordo (-) / Boreste (+)]", fontsize=8.5, fontweight='bold', color='#1A365D')
ax_body.set_ylabel("Altura Z (m)", fontsize=8.5, fontweight='bold', color='#1A365D')
ax_body.grid(True, linestyle=':', alpha=0.5)

# 4. INFO PANEL
ax_info.axis('off')
ax_info.set_facecolor('#F8FAFC')

info_text = (
    "TABELA DE COTAS & OFFSET 2D\n"
    "=====================================\n"
    "Embarcacao: Saveiro Ciclone 2\n"
    "Aluno: Rafael Rosa Batista\n"
    "Orientacao: Profa. Magdalena (Brana/EEE)\n"
    "-------------------------------------\n"
    "DIMENSOES PRINCIPAIS:\n"
    "- Comprimento Total (LOA): 7,00 m\n"
    "- Boca Maxima (B): 2,50 m (Baliza 3)\n"
    "- Pontal Moldado (D): 1,10 m\n"
    "- Calado de Projeto (T): 0,60 m\n"
    "- Espelho de Popa: 1,60 m de largura\n"
    "- Razao L/B: 2,80 (Alta Estabilidade)\n"
    "-------------------------------------\n"
    "OFFSET DAS BALIZAS (Semi-Bocas em m):\n"
    "Baliza 0 (Popa): Y = 0,80 m (B = 1,60 m)\n"
    "Baliza 1 (1,0m): Y = 1,08 m (B = 2,16 m)\n"
    "Baliza 2 (2,0m): Y = 1,22 m (B = 2,44 m)\n"
    "Baliza 3 (3,0m): Y = 1,25 m (B = 2,50 m) [BMAX]\n"
    "Baliza 4 (4,0m): Y = 1,23 m (B = 2,46 m)\n"
    "Baliza 5 (5,0m): Y = 1,05 m (B = 2,10 m)\n"
    "Baliza 6 (6,0m): Y = 0,65 m (B = 1,30 m)\n"
    "Baliza 7 (7,0m): Y = 0,00 m (Proa/Bico)\n"
    "-------------------------------------\n"
    "CARACTERISTICAS DA FORMA:\n"
    "* Entrada Fina de Proa (Balizas 6 e 7)\n"
    "* Centro de Carena recuado (~Baliza 3)\n"
    "* Quilha Corrida em Jaqueira/Massaranduba\n"
    "* Fundo arredondado com bojo suave\n"
    "* Excelente reserva de flutuabilidade\n"
    "====================================="
)

ax_info.text(0.05, 0.95, info_text, transform=ax_info.transAxes,
             fontsize=8.0, fontfamily='monospace', verticalalignment='top',
             bbox=dict(boxstyle='round,pad=0.6', facecolor='#FFFFFF', edgecolor='#CBD5E0', linewidth=1.2))

out_img_path = "/home/magdalena/eee/alunos/rafael-rosa-batista/Rascunho_2D_Plano_Linhas_Saveiro_Ciclone_2.png"
plt.savefig(out_img_path, dpi=300, bbox_inches='tight')
plt.close()
print("Rascunho 2D Image generated successfully at:", out_img_path)
