from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors

def gerar_pdf(caminho, titulo_doc, subtitulo_doc, conteudo_tabela, notas):
    doc = SimpleDocTemplate(caminho, pagesize=A4, rightMargin=36, leftMargin=36, topMargin=36, bottomMargin=36)
    styles = getSampleStyleSheet()
    
    title_style = ParagraphStyle(
        'DocTitle',
        parent=styles['Heading1'],
        fontSize=18,
        leading=22,
        textColor=colors.HexColor('#0f2d4a'),
        spaceAfter=6
    )
    subtitle_style = ParagraphStyle(
        'DocSub',
        parent=styles['Normal'],
        fontSize=10,
        leading=14,
        textColor=colors.HexColor('#555555'),
        spaceAfter=14
    )
    h2_style = ParagraphStyle(
        'H2',
        parent=styles['Heading2'],
        fontSize=13,
        leading=16,
        textColor=colors.HexColor('#1b4b72'),
        spaceBefore=10,
        spaceAfter=6
    )
    body_style = ParagraphStyle(
        'Body',
        parent=styles['Normal'],
        fontSize=9.5,
        leading=14,
        textColor=colors.HexColor('#222222'),
        spaceAfter=6
    )
    cell_style = ParagraphStyle(
        'Cell',
        parent=styles['Normal'],
        fontSize=8.5,
        leading=11,
        textColor=colors.HexColor('#222222')
    )
    cell_head = ParagraphStyle(
        'CellH',
        parent=styles['Normal'],
        fontSize=8.5,
        leading=11,
        fontName='Helvetica-Bold',
        textColor=colors.white
    )

    story = []
    story.append(Paragraph(titulo_doc, title_style))
    story.append(Paragraph(subtitulo_doc, subtitle_style))
    story.append(Spacer(1, 8))

    story.append(Paragraph("1. Formula e Fundamento Teorico (Larsson & Eliasson, Fig. 4.21)", h2_style))
    story.append(Paragraph("<b>Dellenbaugh Angle = 279 &times; (As &times; HA) / (m &times; GM)</b>", body_style))
    story.append(Paragraph("Avalia o adernamento estatico provocado por vento constante de aproximadamente 15 a 17 nos no traves (Forca 4 Beaufort).", body_style))
    story.append(Spacer(1, 6))

    story.append(Paragraph("2. Parametros e Resultados por Condicao Operacional", h2_style))
    
    # Montar tabela
    table_data = []
    # cabecalho
    table_data.append([Paragraph(h, cell_head) for h in conteudo_tabela[0]])
    for row in conteudo_tabela[1:]:
        table_data.append([Paragraph(str(c), cell_style) for c in row])
        
    t = Table(table_data, colWidths=[140, 60, 60, 60, 75, 120])
    t.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), colors.HexColor('#0f2d4a')),
        ('ALIGN', (0,0), (-1,-1), 'LEFT'),
        ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
        ('GRID', (0,0), (-1,-1), 0.5, colors.HexColor('#cccccc')),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.white, colors.HexColor('#f8f9fa')]),
        ('TOPPADDING', (0,0), (-1,-1), 5),
        ('BOTTOMPADDING', (0,0), (-1,-1), 5),
    ]))
    story.append(t)
    story.append(Spacer(1, 10))

    story.append(Paragraph("3. Analise Naval e Conclusao de Engenharia", h2_style))
    for n in notas:
        story.append(Paragraph(n, body_style))

    doc.build(story)

# 1. Costa Cruiser
tabela_costa = [
    ["Condicao Operacional", "m (kg)", "As (m2)", "HA (m)", "GM (m)", "Dellenbaugh / Status"],
    ["Oceanica Referencia G2 (KSS 2,10m)", "2.450", "40,0", "4,45", "1,05", "19,3 deg (Ideal / Faixa 15,4-21,4)"],
    ["Oceanica Plena Carga LC3 (Ocean-7)", "2.800", "40,0", "4,45", "0,95", "18,7 deg (Ideal / Firme e Docil)"],
    ["Quilha Recolhida / Porto (0,48m)", "2.450", "40,0", "4,45", "0,69", "29,4 deg (Tender / Proibido velejar)"]
]
notas_costa = [
    "&bull; <b>Posicionamento no Grafico de Aula:</b> Para Lwl = 7,20 m, a faixa recomendada de cruzeiro situa-se entre 15,4 deg (Duro/Stiff) e 21,4 deg (Mole/Tender). O Costa Cruiser Oceanica 25 pontua em <b>18,7 deg a 19,3 deg</b>, situando-se perfeitamente no centro da faixa verde de conforto.",
    "&bull; <b>Comportamento no Mar:</b> O barco veleja com adernamento estavel e previsivel sob brisa fresca, permitindo que a aresta do chine trabalhe com sustentacao hidrodinamica e conforto a bordo.",
    "&bull; <b>Diretriz Operacional Inegociavel:</b> A quilha KSS deve permanecer 100% estendida (2,10 m) para navegacao a vela. Com a quilha recolhida o adernamento salta para quase 30 deg, situacao restrita exclusivamente a manobras a motor eletrico em aguas rasas."
]
gerar_pdf(
    '/home/magdalena/eee/alunos/sidinei-costa/Avaliacao_Dellenbaugh_Costa_Cruiser_25.pdf',
    'AVALIACAO DO ANGULO DE DELLENBAUGH &bull; COSTA CRUISER 25 v4.2',
    'Projetista: Sidinei Costa &bull; BRANA Projetos Navais &bull; Data: Outubro / 2026',
    tabela_costa,
    notas_costa
)

# 2. Teoboat Grupo 9
tabela_teo = [
    ["Condicao de Carga", "m (kg)", "As (m2)", "HA (m)", "GM (m)", "Dellenbaugh / Status"],
    ["Crianca Iniciante (4 anos / 20 kg)", "45", "2,0", "0,95", "1,15", "10,2 deg (Hiper-Estavel / Seguro)"],
    ["Crianca Media (6 anos / 30 kg)", "55", "2,0", "0,95", "1,07", "9,0 deg (Hiper-Estavel / Seguro)"],
    ["Condicao de Projeto Nominal (75 kg)", "75", "2,0", "0,95", "1,02", "6,9 deg (Super Duro / Plataforma)"],
    ["Crianca Limite (8 anos / 55 kg)", "85", "2,0", "0,95", "0,98", "6,4 deg (Super Duro / Plataforma)"]
]
notas_teo = [
    "&bull; <b>Conceito Bicicleta de Rodinhas Nautica:</b> Em veleiros convencionais com Lwl = 1,75 m, o angulo tipico da curva seria de 37 a 43 deg, o que assustaria uma crianca de 4 a 8 anos. O Teoboat alcanca apenas <b>6,4 deg a 10,2 deg</b>.",
    "&bull; <b>Geometria de Alta Estabilidade:</b> A boca excepcional de 0,98 m para 1,90 m de comprimento (razao L/B = 1,94) produz inercia de linha d'agua massiva, que somada ao assento rebaixado confere GM proximo de 1,0 m.",
    "&bull; <b>Seguranca Passiva e Pedagogica:</b> O centro velico baixo (HA = 0,95 m) anula riscos de capotamento sob vento normal de ate 15 nos, garantindo navegacao plana e confianca total para o aprendizado infantil."
]
gerar_pdf(
    '/home/magdalena/eee/grupos/grupo-9/Avaliacao_Dellenbaugh_Teoboat_Grupo_9.pdf',
    'AVALIACAO DO ANGULO DE DELLENBAUGH &bull; TEOBOAT (GRUPO 9)',
    'Projetistas: Karina Menezes, Monique, Carolina Sapucaia, Danrley e Sidnei &bull; EEE / BRANA &bull; Data: Outubro / 2026',
    tabela_teo,
    notas_teo
)
print("PDFs gerados com sucesso!")
