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

# Set figure and styling
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig, (ax_profile, ax_plan) = plt.subplots(2, 1, figsize=(14, 11), gridspec_kw={'height_ratios': [1, 1.1]})

navy = '#1A365D'
marine = '#2B6CB0'
hull_fill = '#E2E8F0'
deck_fill = '#EDF2F7'
wl_color = '#3182CE'
accent_red = '#C53030'
wood = '#D69E2E'

# -------------------------------------------------------------
# 1. PROFILE VIEW (VISTA DE PERFIL)
# -------------------------------------------------------------
ax_profile.set_title("VISTA DE PERFIL (ELEVAÇÃO LATERAL) — CATAMARÃ OCEÂNICO 45'", fontsize=13, fontweight='bold', color=navy, pad=12)

# Hull Profile Coordinates (LOA = 13.72m)
# X: 0 (Stern) to 13.72 (Bow)
# Waterline at Y = 0.00
# Keel bottom at Y = -1.25
# Hull bottom at Y = -0.40
# Sheer line (Deck) at Y = +1.40 to +1.65

# Waterline line
ax_profile.axhline(0, color=wl_color, linestyle='--', linewidth=1.5, label='Linha d\'Água de Projeto (DWL, T=1.25m)')
ax_profile.fill_between([-1, 15], -2, 0, color='#EBF8FF', alpha=0.5)

# Hull polygon
hull_x = [0.0, 0.2, 2.0, 6.0, 10.0, 13.2, 13.72, 13.5, 12.8, 10.0, 6.0, 2.0, 0.0, 0.0]
hull_y = [1.35, -0.2, -0.38, -0.42, -0.35, 0.0, 1.65, 1.65, 1.60, 1.50, 1.45, 1.40, 1.35, 1.35]
ax_profile.plot(hull_x, hull_y, color=navy, linewidth=2)
ax_profile.fill(hull_x, hull_y, color=hull_fill, alpha=0.7)

# Fixed Low-Aspect Keel (Quilha Rasa Fixa)
keel_x = [5.2, 5.8, 8.2, 8.5, 5.2]
keel_y = [-0.42, -1.25, -1.25, -0.38, -0.42]
ax_profile.plot(keel_x, keel_y, color=accent_red, linewidth=1.8, label='Quilha Rasa Fixa de Sacrifício (Calado 1.25m)')
ax_profile.fill(keel_x, keel_y, color='#FEB2B2', alpha=0.8)

# Rudder (Leme protegido)
rudder_x = [0.8, 0.9, 1.4, 1.3, 0.8]
rudder_y = [-0.3, -1.05, -1.05, -0.3, -0.3]
ax_profile.plot(rudder_x, rudder_y, color='#4A5568', linewidth=1.5)
ax_profile.fill(rudder_x, rudder_y, color='#CBD5E0')

# Propeller & Saildrive
ax_profile.plot([2.2, 2.4], [-0.35, -0.85], color='black', linewidth=2)
ax_profile.scatter([2.4], [-0.85], color='orange', s=60, zorder=5, label='Saildrive 45hp')

# Coachroof / Saloon Cabin & Hardtop
saloon_x = [2.8, 2.8, 8.2, 8.8, 8.5, 2.8]
saloon_y = [1.42, 2.65, 2.65, 1.80, 1.48, 1.42]
ax_profile.plot(saloon_x, saloon_y, color=marine, linewidth=2)
ax_profile.fill(saloon_x, saloon_y, color='#BEE3F8', alpha=0.6)

# Saloon Windows
win_x = [3.5, 8.0, 8.3, 3.5, 3.5]
win_y = [1.75, 1.75, 2.2, 2.45, 1.75]
ax_profile.plot(win_x, win_y, color=navy, linewidth=1.2)
ax_profile.fill(win_x, win_y, color='#2B6CB0', alpha=0.3)

# Hardtop Roof (Painéis Solares)
hardtop_x = [1.5, 1.5, 7.8, 7.8]
hardtop_y = [2.70, 2.78, 2.78, 2.70]
ax_profile.plot(hardtop_x, hardtop_y, color=navy, linewidth=2.5)
ax_profile.fill(hardtop_x, hardtop_y, color='#CBD5E0')
ax_profile.text(4.2, 2.9, "Hardtop c/ Painéis Solares (1.500 Wp)", fontsize=8.5, fontweight='bold', color=navy)

# Mast & Rigging Indication
ax_profile.plot([6.8, 6.8], [2.78, 8.5], color='#4A5568', linewidth=3.5, label='Mastro de Alumínio / Fracionado')
ax_profile.plot([6.8, 13.5], [8.2, 1.65], color='#718096', linestyle=':', linewidth=1.2, label='Estai de Proa (Forestay)')
ax_profile.plot([6.8, 0.5], [8.2, 1.4], color='#718096', linestyle=':', linewidth=1.2)
ax_profile.plot([6.8, 1.8], [3.2, 3.2], color=navy, linewidth=3) # Boom / Retranca

# Lifelines (Guarda-mancebo com tela de segurança para cães)
ax_profile.plot([0.2, 13.5], [2.05, 2.30], color='#E53E3E', linestyle='-.', linewidth=1.2, label='Guarda-mancebo c/ Rede de Proteção (Pets)')

# Annotations & Dimensions
ax_profile.annotate('Vão Central (Clearance ≥ 0.85m)', xy=(5.5, 0.85), xytext=(5.5, 0.2),
            arrowprops=dict(arrowstyle='<->', color=accent_red, lw=1.2),
            fontsize=8.5, fontweight='bold', color=accent_red, ha='center')

ax_profile.set_xlim(-1, 15)
ax_profile.set_ylim(-1.6, 9.0)
ax_profile.set_xlabel('Comprimento (m) [LOA: 13,72 m / 45 ft]', fontsize=9.5, fontweight='bold', color=navy)
ax_profile.set_ylabel('Altura / Calado (m)', fontsize=9.5, fontweight='bold', color=navy)
ax_profile.legend(loc='upper right', fontsize=8, framealpha=0.9)

# -------------------------------------------------------------
# 2. PLAN VIEW / ARRANJO GERAL (VISTA SUPERIOR DE CONVÉS)
# -------------------------------------------------------------
ax_plan.set_title("ARRANJO GERAL DO CONVÉS E SALÃO (VISTA SUPERIOR) — BOCA TOTAL: 7,50 m", fontsize=13, fontweight='bold', color=navy, pad=12)

# Coordinates: X: 0 (Popa) to 13.72 (Proa). Y: -3.75 (BB) to +3.75 (BE)
# Port Hull (BB, Y = -3.75 to -2.45)
# Starboard Hull (BE, Y = +2.45 to +3.75)
# Bridge Deck / Saloon (Y = -2.45 to +2.45, X = 2.0 to 8.5)
# Trampoline (Y = -2.45 to +2.45, X = 8.5 to 13.2)

# Starboard Hull (BE)
hull_be_x = [0.0, 1.5, 5.0, 9.0, 12.0, 13.72, 13.2, 11.0, 7.0, 3.0, 0.0, 0.0]
hull_be_y = [2.6, 2.45, 2.45, 2.50, 2.70, 3.10, 3.75, 3.75, 3.75, 3.65, 3.4, 2.6]
ax_plan.plot(hull_be_x, hull_be_y, color=navy, linewidth=2)
ax_plan.fill(hull_be_x, hull_be_y, color=hull_fill, alpha=0.8)

# Port Hull (BB)
hull_bb_x = [0.0, 1.5, 5.0, 9.0, 12.0, 13.72, 13.2, 11.0, 7.0, 3.0, 0.0, 0.0]
hull_bb_y = [-2.6, -2.45, -2.45, -2.50, -2.70, -3.10, -3.75, -3.75, -3.75, -3.65, -3.4, -2.6]
ax_plan.plot(hull_bb_x, hull_bb_y, color=navy, linewidth=2)
ax_plan.fill(hull_bb_x, hull_bb_y, color=hull_fill, alpha=0.8)

# Central Bridge Deck / Cockpit + Saloon Structure
bridge_x = [1.8, 1.8, 8.5, 8.5, 1.8]
bridge_y = [-2.5, 2.5, 2.5, -2.5, -2.5]
ax_plan.plot(bridge_x, bridge_y, color=navy, linewidth=1.8)
ax_plan.fill(bridge_x, bridge_y, color='#EDF2F7')

# Trampoline Area (Proa)
trampoline = patches.Rectangle((8.5, -2.45), 4.2, 4.9, linewidth=1.2, edgecolor=marine, facecolor='#EBF8FF', linestyle='--', hatch='///', alpha=0.7)
ax_plan.add_patch(trampoline)
ax_plan.text(10.6, 0.0, "TRAMPOLIM DE PROA\n(Rede de Alta Tenacidade / Espaço Pets)", fontsize=8.5, ha='center', va='center', fontweight='bold', color=marine)

# Crossbeam (Viga de Proa)
ax_plan.plot([12.7, 12.7], [-3.1, 3.1], color=navy, linewidth=3.5)
ax_plan.text(12.7, 0.0, "VIGA ESTRUTURAL DE PROA", fontsize=7.5, color='white', ha='center', va='center', bbox=dict(boxstyle='round,pad=0.2', facecolor=navy, edgecolor='none'))

# Saloon Inner Layout (X: 4.2 to 8.4)
saloon_box = patches.Rectangle((4.5, -2.3), 3.9, 4.6, linewidth=1.5, edgecolor=marine, facecolor='#FFFFFF')
ax_plan.add_patch(saloon_box)
ax_plan.text(6.45, 1.2, "MESA DE NAVEGAÇÃO\n& SALÃO INTERNO", fontsize=8, ha='center', va='center', fontweight='bold', color=navy)
ax_plan.text(6.45, -1.2, "COZINHA COMPLETA\n(Bancada em U)", fontsize=8, ha='center', va='center', fontweight='bold', color=navy)

# Cockpit Area (Flush Deck / Nível Único) (X: 1.8 to 4.5)
cockpit_box = patches.Rectangle((1.8, -2.3), 2.7, 4.6, linewidth=1.2, edgecolor='#718096', facecolor='#FEFCBF', alpha=0.6)
ax_plan.add_patch(cockpit_box)
ax_plan.text(3.15, 0.0, "COCKPIT EXTERNO (NÍVEL ÚNICO)\nMesa de Refeições & Posto de Comando\n[Circulação Livre p/ 3 Pessoas + 2 Cães]", fontsize=8, ha='center', va='center', fontweight='bold', color='#744210')

# Cabin Annotations in Hulls
# Starboard Hull (BE) - Suíte do Armador
ax_plan.text(1.2, 3.1, "CABINE POPA (BE)\nCama Casal", fontsize=7.5, ha='center', va='center', color=navy)
ax_plan.text(6.0, 3.1, "ESCRITÓRIO / CIRCULAÇÃO", fontsize=7, ha='center', va='center', color='#4A5568')
ax_plan.text(10.5, 3.1, "BANHEIRO AMPLO\n(Box Fechado)", fontsize=7.5, ha='center', va='center', color=navy)

# Port Hull (BB) - Cabines de Hóspedes / Tripulação
ax_plan.text(1.2, -3.1, "CABINE POPA (BB)\nCama Casal", fontsize=7.5, ha='center', va='center', color=navy)
ax_plan.text(6.0, -3.1, "BANHEIRO BB / CORREDOR", fontsize=7, ha='center', va='center', color='#4A5568')
ax_plan.text(10.5, -3.1, "CABINE PROA (BB)\nCama Casal / Beliche", fontsize=7.5, ha='center', va='center', color=navy)

# Sugar Scoops (Plataformas de Popa com degraus suaves)
ax_plan.fill([0, 0.8, 0.8, 0], [2.6, 2.5, 3.5, 3.4], color='#CBD5E0')
ax_plan.fill([0, 0.8, 0.8, 0], [-2.6, -2.5, -3.5, -3.4], color='#CBD5E0')
ax_plan.text(0.4, 3.0, "DEGRAUS\nACESSO", fontsize=6.5, ha='center', va='center', color=navy)
ax_plan.text(0.4, -3.0, "DEGRAUS\nACESSO", fontsize=6.5, ha='center', va='center', color=navy)

# Safety Netting Boundary (Rede perimétrica cães)
ax_plan.plot([-0.2, 13.8, 13.8, -0.2, -0.2], [-3.9, -3.9, 3.9, 3.9, -3.9], color=accent_red, linestyle=':', linewidth=1.5, label='Perímetro de Proteção c/ Rede (Segurança dos Pets)')

# Keel Positions (Abaixo dos cascos)
ax_plan.plot([5.5, 8.2], [3.1, 3.1], color=accent_red, linewidth=4, label='Quilhas Rasas Fixas (Abaixo dos Cascos)')
ax_plan.plot([5.5, 8.2], [-3.1, -3.1], color=accent_red, linewidth=4)

ax_plan.set_xlim(-1, 15)
ax_plan.set_ylim(-4.5, 4.5)
ax_plan.set_xlabel('Comprimento (m) [LOA: 13,72 m]', fontsize=9.5, fontweight='bold', color=navy)
ax_plan.set_ylabel('Boca (m) [BOA: 7,50 m]', fontsize=9.5, fontweight='bold', color=navy)
ax_plan.legend(loc='lower left', fontsize=8, framealpha=0.9)

plt.tight_layout()
output_img = '/home/magdalena/eee/alunos/rodolfo-g-giordano/esboco_catamara_45_rodolfo.png'
plt.savefig(output_img, dpi=300)
print(f"Esboço gerado com sucesso em: {output_img}")
