import email, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from fpdf import FPDF, HTMLMixin
import pandas as pd

def tls():
    port = 587  # For starttls
    smtp_server = "mail.privateemail.com"
    sender_email = "contact@onesheepforalifetime.com"
    receiver_email = "scheidl.max@gmail.com"
    password = "Schaf3gal!"
    subject = "Your Wool Report is ready."
    body = "Thanks for trying our wool simulator."

    # Create a multipart message and set headers
    message = MIMEMultipart()
    message["From"] = "One Sheep <contact@onesheepforalifetime.com>"
    message["To"] = receiver_email
    message["Subject"] = subject
        
    # Add body to email
    message.attach(MIMEText(body, "plain"))

    filename = "/var/www/html/os4al_web/main/woolreport.pdf"  # In same directory as script

    # Open PDF file in binary mode
    with open(filename, "rb") as attachment:
        # Add file as application/octet-stream
        # Email client can usually download this automatically as attachment
        part = MIMEBase("application", "octet-stream")
        part.set_payload(attachment.read())

    # Encode file in ASCII characters to send by email    
    encoders.encode_base64(part)

    # Add header as key/value pair to attachment part
    part.add_header(
        "Content-Disposition",
        f"attachment; filename= {filename}",
    )

    # Add attachment to message and convert message to string
    message.attach(part)
    text = message.as_string()

        # Log in to server using secure context and send email
    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.ehlo()  # Can be omitted
        server.starttls(context=context)
        server.ehlo()  # Can be omitted
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, text)

class PDF(FPDF, HTMLMixin):
    pass

def create_pdf():
    data = pd.read_csv("/var/www/html/os4al_web/main/report/reports.csv")
    data = data.iloc[-1].tolist()
    
    name = data[2]
    debug = 0

    if name != "":
        name =  name + " 's"

    else:
        name = "Your"

    used_wool = str(data[4])
    leftover_wool = str(data[5])

    g_title = []
    g_usedy = []
    g_stich = []
    g_tension = []
    x = 0

    print(data)
    garments = data[6]
    garments = garments.split(',')
    garments[0] = " " + garments[0]

    for i in range(len(garments)):
        garments[i] = (garments[i][2:-1])

    for i in range(data[0]):
        g_title.append(garments[0 + x])
        g_usedy.append(garments[1 + x])
        g_stich.append(garments[2 + x])
        g_tension.append(garments[3 + x])
        x = x + 4

    h = 14
    sh = 10
    p = 8


    width = 57
    image_width = width - 8
    height = 102 + len(g_title) * 35

    pdf =  PDF('P', 'mm', (width, height))
    # Add a page
    pdf.add_page()
    pdf.set_margins(4.5,0,4.5)
    pdf.set_font("Arial", size = h)
    
     
    # create a cell
    #pdf.write(10, name +  "Wool Report")
    pdf.set_font("Arial", size = h, style="B")
    pdf.cell(0,1, txt= "", border=debug, align="C", ln=1)
    pdf.cell(0,5, txt= name, border=debug, align="C", ln=1)
    pdf.cell(0,5, txt= "Wool Report", border=debug, align="C", ln=1)
    pdf.set_font("Arial", size = p, style="")
    pdf.cell(0,5, txt= " ",border= debug,align="C",ln=1)
    pdf.cell(0,5, txt= "*****************************************",border= debug,align="C",ln=1)

    
    pdf.cell(0,5, txt= " ",border= debug,align="C",ln=1)
    pdf.set_font("Arial", style="BU", size = sh)
    pdf.cell(0,5, txt= "Wool used",  border=debug, align="C",ln=1)
    pdf.set_font("Arial", size = p)
    pdf.cell(0,5, txt= used_wool + " grams" , border=debug, align="C",ln=1)


    pdf.cell(0,5, txt= " ",border= debug,align="C",ln=1)
    pdf.set_font("Arial", size = sh, style="BU" ) 
    pdf.cell(0,5, txt= "Wool donated",border=debug, align="C",ln=1)
    pdf.set_font("Arial", size = p)
    pdf.cell(0,5, txt= leftover_wool + " grams" , border=debug, align="C",ln=1)
    pdf.cell(0,5, txt= "" , border=debug, align="C",ln=1)
    
    pdf.set_font("Arial", size = p, style="")
    pdf.cell(0,5, txt= "*****************************************",border= debug,align="C",ln=1)

    for i in range(len(g_title)):
        pdf.cell(0,5, txt= " ",border= debug,align="C",ln=1)
        pdf.set_font("Arial", size = sh, style="B" )
        pdf.cell(0,5, txt= g_title[i], border=debug, align="C",ln=1)
        pdf.set_font("Arial", size = p)
        pdf.cell(0,5, txt= g_usedy[i] + " grams" , border=debug, align="C",ln=1)
        pdf.cell(0,5, txt= g_stich[i] , border=debug, align="C",ln=1)
        pdf.cell(0,5, txt= g_tension[i] , border=debug, align="C",ln=1)
        pdf.cell(0,5, txt= " ",border= debug,align="C",ln=1)
        pdf.cell(0,5, txt= "*****************************************",border= debug,align="C",ln=1)    


    pdf.cell(0,10, txt= "onesheepforalifetime.com",border= debug,align="C",ln=1)
    pdf.set_font("Arial", size = p)

    fpath = "/var/www/html/os4al_web/main/woolreport.pdf"
    pdf.output(fpath)


    #return(link)



create_pdf()
tls()