import os
'''
def handle_uploaded_file(csv_file):
    csv_file_name = str(csv_file)
    data_path = config.getInstance().requireConfig("data_path")
    path = os.path.join(data_path, csv_file_name)
    with open(path, 'wb+') as destination:
        for chunk in csv_file.chunks():
            destination.write(chunk)
'''
def aktualisiere_config(config, data):
    if data["briefpapier"]:
        uploaded_file = data["briefpapier"]
        data_path = config.requireConfig("data_path")
        # copy briefpapier into data_path
        with open(os.path.join(data_path, uploaded_file.name), 'wb+') as destination:
            for chunk in uploaded_file.chunks():
                destination.write(chunk)
        config.set_config("briefpapier", uploaded_file.name)
    
    if data["basisbeitrag"]:
        config.set_config("beitrag_basis", data["basisbeitrag"])
        
    if data["zusatzbeitrag"]:
        config.set_config("beitrag_zusatz", data["zusatzbeitrag"])
    
def erstellepdf(text):
    with open(r'G:\SVBaL\python\eh_util\eh_app\test.tex', 'w') as f: # öffnet ein neues Dokument mit dem Namen test.tex
        f.write(text) # schreibt in dieses neue Dokument
        
    # TODO: AUfruf PDF latex tex -> pdf

def erstelle_ehmeldung(data):
    from PyPDF2 import PdfFileWriter, PdfFileReader
    from datetime import date
    import io
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=letter)
    
    # Adressfeld
    can.setFont("Helvetica", 12)
    can.drawString(205, 618, data["VorZuname"])
    can.drawString(205, 600, data["VorZunamePartner"])
    can.drawString(205, 583, data["Wohnanschrift"])
    can.drawString(205, 566, data["Telefon"])
    can.drawString(365, 566, data["Email"])
    can.drawString(205, 549, data["Geburtsdatum"])
    versichertes_object = data["VersichertesObjekt"]
    if versichertes_object == "":
        versichertes_object = data["Wohnanschrift"]
    can.drawString(205, 500, versichertes_object)
    can.drawString(193, 466, str(data["AnzahlWohnungen"]))
    if data["Selbstgenutzt"]:
        can.drawString(350, 466, "X")
    if data["Eigentumswohnung"]:
        can.drawString(188, 449, "X")
    if data["Gewerblich"]:
        can.drawString(350, 449, "X")
    can.drawString(140, 376, date.today().strftime('%d.%m.%Y'))
    can.drawString(350, 376, "Maschinell erstellt.")
        
    can.save()
    
    #move to the beginning of the StringIO buffer
    packet.seek(0)
    
    # create a new PDF with Reportlab
    new_pdf = PdfFileReader(packet)
    # read your existing PDF
    existing_pdf = PdfFileReader(open("/home/sparky2021/SVBaL/Aktuell/BeitrittserklarungSVBaL.pdf", "rb"))
    output = PdfFileWriter()
    # add the "watermark" (which is the new pdf) on the existing page
    page = existing_pdf.pages[0]
    page.mergePage(new_pdf.pages[0])
    output.addPage(page)
    # finally, write "output" to a real file
    output_stream = open("meldung.pdf", "wb")
    output.write(output_stream)
    output_stream.close()
      
