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

# Configuração de estilo técnico naval
plt.style.use('seaborn-v0_8-whitegrid' if 'seaborn-v0_8-whitegrid' in plt.style.available else 'default')
fig = plt.figure(figsize=(14, 10), dpi=300)

# Cores
c_hull = '#0F2B48'
c_wl = '#0077B6'
c_deck = '#E2E8F0'
c_detail = '#D90429'
c_text = '#1E293B'
c_tunnel = '#94A3B8'

# 1. PLANTA BAIXA / VISTA SUPERIOR (Top View)
ax1 = fig.add_subplot(2, 2, (1, 2))
ax1.set_title("1. PLANTA BAIXA / ARRANJO GERAL DE CONVÉS (Top View)\nComprimento: 8,50 m | Boca Total: 3,60 m", fontsize=11, fontweight='bold', color=c_hull, pad=10)

# Eixos de centro dos cascos: Y = +1.39 e Y = -1.39 (distância entre eixos = 2.78m)
y_be = 1.39
y_bb = -1.39
b_half = 0.41 # semi-largura de cada casco (0.82m total)

# Casco Boreste (Superior no desenho)
x_hull = np.linspace(0, 8.5, 100)
# Curva do casco (aferentamento na proa e popa)
w_be_ext = np.where(x_hull < 6.0, b_half * (1 - ((6.0 - x_hull)/6.0)**2 * 0.4), b_half * np.sqrt(np.maximum(0, 1 - ((x_hull - 6.0)/2.5)**2)))
w_be_int = np.where(x_hull < 6.0, b_half * 0.95, b_half * np.sqrt(np.maximum(0, 1 - ((x_hull - 6.0)/2.5)**2)))

# Contorno Casco Boreste
ax1.plot(x_hull, y_be + w_be_ext, color=c_hull, lw=2)
ax1.plot(x_hull, y_be - w_be_int, color=c_hull, lw=2)
ax1.plot([0, 0], [y_be - b_half*0.9, y_be + b_half*0.9], color=c_hull, lw=2) # Espelho de popa

# Contorno Casco Bombordo
ax1.plot(x_hull, y_bb + w_be_int, color=c_hull, lw=2)
ax1.plot(x_hull, y_bb - w_be_ext, color=c_hull, lw=2)
ax1.plot([0, 0], [y_bb - b_half*0.9, y_bb + b_half*0.9], color=c_hull, lw=2) # Espelho de popa

# Conexão de Proa (Arco frontal unindo os cascos conforme croqui)
theta = np.linspace(-np.pi/2, np.pi/2, 50)
x_bow_arc = 8.5 + 0.3 * np.cos(theta)
y_bow_arc = (y_be + y_bb)/2 + 1.8 * np.sin(theta)
# Linha de fechamento de proa
ax1.plot([7.5, 8.5], [y_be + b_half*0.5, y_be], color=c_hull, lw=2)
ax1.plot([7.5, 8.5], [y_bb - b_half*0.5, y_bb], color=c_hull, lw=2)
ax1.plot([8.5, 8.5], [y_bb, y_be], color=c_hull, lw=2, linestyle='--')

# Ponte Central / Convés
bridge_x = [0.8, 7.8, 7.8, 0.8]
bridge_y = [y_bb + b_half*0.9, y_bb + b_half*0.9, y_be - b_half*0.9, y_be - b_half*0.9]
bridge = patches.Polygon(xy=list(zip(bridge_x, bridge_y)), closed=True, facecolor='#F1F5F9', edgecolor=c_tunnel, lw=1.5, zorder=1)
ax1.add_patch(bridge)

# Travessa estrutural central (conforme croqui)
ax1.add_patch(patches.Rectangle((4.0, y_bb - b_half), 0.4, 3.6, facecolor='#CBD5E1', edgecolor=c_hull, lw=1, zorder=2))

# Posto de Comando / Console (no bordo, conforme croqui)
console = patches.Rectangle((3.8, y_be - 0.7), 1.2, 0.9, facecolor='#3B82F6', edgecolor='#1D4ED8', lw=1.5, zorder=3)
ax1.add_patch(console)
# Timão / Volante
circle = patches.Circle((4.4, y_be - 0.25), 0.2, facecolor='white', edgecolor=c_hull, lw=1.5, zorder=4)
ax1.add_patch(circle)
ax1.text(4.4, y_be - 0.25, '+', ha='center', va='center', fontsize=11, color=c_hull, zorder=5)
ax1.text(4.4, y_be + 0.35, 'Comando\n(Console)', ha='center', va='bottom', fontsize=8, fontweight='bold', color=c_hull)

# Praça de Pesca (Popa)
ax1.text(2.0, 0, 'PRAÇA DE PESCA LIVRE\n(~14 m²)', ha='center', va='center', fontsize=9, fontweight='bold', color='#475569')

# Guincho de Espinhel
guincho = patches.Circle((1.5, 0.8), 0.35, facecolor='#EF4444', edgecolor='#991B1B', lw=1.5, zorder=3)
ax1.add_patch(guincho)
ax1.text(1.5, 0.8, 'Guincho\nEspinhel', ha='center', va='center', fontsize=7, fontweight='bold', color='white', zorder=4)

# Porões de Pescado (um em cada casco)
ax1.add_patch(patches.Rectangle((2.8, y_be - 0.3), 1.6, 0.6, facecolor='#93C5FD', edgecolor=c_hull, lw=1, zorder=2))
ax1.text(3.6, y_be, 'Porão 1 (500 kg)', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

ax1.add_patch(patches.Rectangle((2.8, y_bb - 0.3), 1.6, 0.6, facecolor='#93C5FD', edgecolor=c_hull, lw=1, zorder=2))
ax1.text(3.6, y_bb, 'Porão 2 (500 kg)', ha='center', va='center', fontsize=7, fontweight='bold', color=c_hull)

# Hélices / Motores na Popa
prop1 = patches.Circle((-0.2, y_be), 0.22, facecolor='#F59E0B', edgecolor='#B45309', lw=1.5, zorder=3)
prop2 = patches.Circle((-0.2, y_bb), 0.22, facecolor='#F59E0B', edgecolor='#B45309', lw=1.5, zorder=3)
ax1.add_patch(prop1)
ax1.add_patch(prop2)
ax1.text(-0.6, y_be, 'Motor 1\n(25-30 HP)', ha='right', va='center', fontsize=7.5, fontweight='bold', color='#B45309')
ax1.text(-0.6, y_bb, 'Motor 2\n(25-30 HP)', ha='right', va='center', fontsize=7.5, fontweight='bold', color='#B45309')

# Cotas
ax1.annotate('', xy=(0, -2.2), xytext=(8.5, -2.2), arrowprops=dict(arrowstyle='<->', color='black', lw=1.2))
ax1.text(4.25, -2.45, 'Comprimento Total (LOA) = 8,50 m', ha='center', va='top', fontsize=8.5, fontweight='bold')

ax1.annotate('', xy=(8.8, y_bb - b_half), xytext=(8.8, y_be + b_half), arrowprops=dict(arrowstyle='<->', color='black', lw=1.2))
ax1.text(9.0, 0, 'Boca Total\nB = 3,60 m', ha='left', va='center', fontsize=8.5, fontweight='bold')

ax1.set_xlim(-1.2, 10.0)
ax1.set_ylim(-2.6, 2.6)
ax1.set_aspect('equal')
ax1.set_xlabel("Comprimento (metros)", fontsize=8)
ax1.set_ylabel("Largura (metros)", fontsize=8)


# 2. VISTA LATERAL / PERFIL (Profile View)
ax2 = fig.add_subplot(2, 2, 3)
ax2.set_title("2. PERFIL LONGITUDINAL (Side View)", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Linha do Casco Lateral
x_prof = [0, 0, 5.0, 7.8, 8.5, 8.0, 0]
y_prof = [0.9, -0.6, -0.6, -0.3, 0.9, 0.9, 0.9] # Fundo, proa lançada, convés

ax2.fill([0, 8.0, 8.5, 7.8, 5.0, 0], [0.9, 0.9, 0.9, -0.3, -0.6, -0.6], color='#E2E8F0', edgecolor=c_hull, lw=2)

# Linha d'Água (WL = 0.0)
ax2.axhline(0, color=c_wl, lw=2, linestyle='-', label="Linha d'Água (LWL = 8,00 m)")
ax2.fill_between([0, 8.0], [-0.6, 0], [0, 0], color='#BAE6FD', alpha=0.5)

# Casaria / Console em perfil
ax2.add_patch(patches.Rectangle((3.8, 0.9), 1.2, 1.0, facecolor='#3B82F6', edgecolor='#1D4ED8', lw=1.5))
ax2.text(4.4, 1.4, 'Comando', ha='center', va='center', fontsize=7.5, color='white', fontweight='bold')

# Hélice / Skeg na popa
ax2.plot([-0.2, -0.2], [-0.65, -0.25], color='#B45309', lw=3)
ax2.plot([-0.35, -0.05], [-0.45, -0.45], color='#B45309', lw=2)
ax2.text(-0.3, -0.45, 'Hélice', ha='right', va='center', fontsize=7, color='#B45309', fontweight='bold')

# Cotas de Calado e Pontal
ax2.annotate('', xy=(1.0, -0.6), xytext=(1.0, 0), arrowprops=dict(arrowstyle='<->', color='#B91C1C', lw=1.2))
ax2.text(1.2, -0.3, 'Calado T = 0,60 m', ha='left', va='center', fontsize=7.5, color='#B91C1C', fontweight='bold')

ax2.annotate('', xy=(6.5, -0.6), xytext=(6.5, 0.9), arrowprops=dict(arrowstyle='<->', color=c_hull, lw=1.2))
ax2.text(6.65, 0.15, 'Pontal D = 1,50 m', ha='left', va='center', fontsize=7.5, color=c_hull, fontweight='bold')

ax2.set_xlim(-1.0, 9.5)
ax2.set_ylim(-1.0, 2.3)
ax2.set_aspect('equal')
ax2.set_xlabel("Comprimento (m)", fontsize=8)
ax2.set_ylabel("Altura (m)", fontsize=8)
ax2.legend(loc='upper right', fontsize=7)


# 3. VISTA FRONTAL / SEÇÃO TRANSVERSAL (Body Plan / Front View)
ax3 = fig.add_subplot(2, 2, 4)
ax3.set_title("3. SEÇÃO TRANSVERSAL / BALIZA MESTRA (Front View)", fontsize=10, fontweight='bold', color=c_hull, pad=8)

# Dois cascos em corte transversal
# Casco BB (Esquerda: centro em X = -1.39)
bb_cx = -1.39
be_cx = 1.39
bw = 0.41 # semi-largura = 0.82m total

# Forma do semi-casco em V arredondado
def draw_hull_section(ax, cx):
    # Fundo em V com bojo suave
    xs = [cx - bw, cx - bw*0.9, cx, cx + bw*0.9, cx + bw]
    ys = [0.9, -0.2, -0.6, -0.2, 0.9]
    ax.plot(xs, ys, color=c_hull, lw=2)
    ax.fill(xs, ys, color='#E2E8F0', alpha=0.9)
    # Linha d'água interna
    ax.fill_between([cx - bw*0.85, cx + bw*0.85], [-0.6, -0.6], [0, 0], color='#BAE6FD', alpha=0.6)

draw_hull_section(ax3, bb_cx)
draw_hull_section(ax3, be_cx)

# Ponte Central (Túnel) ligando os dois cascos
bridge_x_sec = [bb_cx + bw, be_cx - bw]
bridge_y_top = [0.9, 0.9]
bridge_y_bot = [0.35, 0.35] # vão livre do túnel (clearance = 0.35 + 0.23 calado = 0.58m da água)

ax3.plot([bb_cx + bw, be_cx - bw], [0.9, 0.9], color=c_hull, lw=2.5) # Piso convés
ax3.plot([bb_cx + bw, be_cx - bw], [0.35, 0.35], color=c_tunnel, lw=2) # Teto túnel
ax3.fill_between(bridge_x_sec, [0.35, 0.35], [0.9, 0.9], color='#F1F5F9')

# Linha d'água geral
ax3.axhline(0, color=c_wl, lw=1.8, linestyle='-', label="Linha d'Água (WL)")

# Cotas na Seção
# Boca de cada casco
ax3.annotate('', xy=(bb_cx - bw, -0.8), xytext=(bb_cx + bw, -0.8), arrowprops=dict(arrowstyle='<->', color=c_hull, lw=1))
ax3.text(bb_cx, -0.95, 'b = 0,82 m\n(Semi-casco)', ha='center', va='top', fontsize=7, fontweight='bold')

# Vão do túnel
ax3.annotate('', xy=(0, 0), xytext=(0, 0.35), arrowprops=dict(arrowstyle='<->', color='#D97706', lw=1.2))
ax3.text(0.1, 0.17, 'Clearance\nHc = 0,58 m', ha='left', va='center', fontsize=7.5, color='#D97706', fontweight='bold')

# Boca total
ax3.annotate('', xy=(bb_cx - bw, 1.2), xytext=(be_cx + bw, 1.2), arrowprops=dict(arrowstyle='<->', color='black', lw=1.2))
ax3.text(0, 1.3, 'Boca Total B = 3,60 m', ha='center', va='bottom', fontsize=8, fontweight='bold')

ax3.set_xlim(-2.4, 2.4)
ax3.set_ylim(-1.2, 1.8)
ax3.set_aspect('equal')
ax3.set_xlabel("Largura (m)", fontsize=8)
ax3.set_ylabel("Altura (m)", fontsize=8)
ax3.legend(loc='upper right', fontsize=7)

plt.tight_layout()
output_img = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/desenho_powercat_8_5m.png"
plt.savefig(output_img, dpi=300, bbox_inches='tight')
plt.close()
print("Desenho gerado com sucesso!")
