from fpdf import FPDF
import PospacAnalysis as pa
import os

"""
This script gets a directory from the user (i.e. 2021-02-22-SantaNella-van-test) and generates a PDF containing all analysis plots from that dataset
"""
#Extension of FPDF for modifying the header on each page
class PDF(FPDF):
    
    def header(self):
        # Arial bold 15
        self.set_font('Helvetica', 'I', 10)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, self.title, 0, 0, 'C')
        # Line break
        self.ln(5)


#Ask user for the directory and set up the pdf settings
directory = pa.getDirFromUser(path = '.')
pdf = PDF()
pdf.set_title(directory)
pdf.set_auto_page_break(auto = True, margin = 10) #Page breaks when the plots will run over the page
pdf.alias_nb_pages() 
pdf.set_font('Helvetica', 'B', 12)
path = directory + '/Plots/'  #Set up the path to the plots assuming it is in Dataset/Plots 

#Get the subdirectory from the user. This will be the name of the analysis test (i.e. pospac_singlebase)
subDir = pa.getDirFromUser(path = path)

#Add a page and set the settings for where to put the first plot
pdf.add_page()
pdf.cell(200,20, txt = subDir,
        ln = 1, align = 'C')
path += subDir + '/'
plotList = os.listdir(path)
x = 30
y = 30
count = 0

#For each plot, add it to the pdf with a specified offset
for plotName in plotList:
    if plotName.endswith('.csv'):
        continue
    pdf.image(path + plotName, x = x, y = y, w = 150) #CDF
    count += 1
    if count == 2:
        pdf.add_page()
        y = 30
        count = 0
    else:
        y += 130

#Save the pdf to the directory
pdf.output(path + subDir + '_analysis_plots.pdf', 'F')
