import {PDFDocument, StandardFonts, rgb} from "pdf-lib"



const getCoordinatesForPDFLib = (x ,y, page) => {
    /*
    * @param x the wanted X Parameter in Millimeters from Top Left
    * @param y the wanted Y Parameter in Millimeters from Top Left
    * @param page the page Object
    *
    * @returns x,y object
    * */


    let retX = x * 2.83
    let retY = page.getHeight()-(y*2.83)

    return {
        x: retX,
        y: retY
    }

}




export const useCreatePdf = async (invoiceData,backgroundSourceBuffer) => {

    const uri = ref("test")
    const genPDF = async () => {
        const pdfDoc = await PDFDocument.create()

        const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
        const fontBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold)

        let pages = []
        let pageCounter = 1



        //const backgroundPdfSourceBuffer = await fetch("/Briefpapier.pdf").then((res) => res.arrayBuffer())
        const backgroudPdf = await PDFDocument.load(backgroundSourceBuffer)

        const firstPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[0])
        const secondPageBackground = await pdfDoc.embedPage(backgroudPdf.getPages()[backgroudPdf.getPages().length > 1 ? 1 : 0])

        //console.log("TEST")
        const page1 = pdfDoc.addPage()

        //console.log(page1.getSize().width/2.83)

        page1.drawPage(firstPageBackground, {
            x: 0,
            y: 0,
        })
        //console.log(page1.getSize())
        pages.push(page1)
        //console.log(pages)


        //Falzmarke 1
        pages[pageCounter - 1].drawLine({
            start: getCoordinatesForPDFLib(0,105,page1),
            end: getCoordinatesForPDFLib(7,105,page1),
            thickness: 0.25,
            color: rgb(0,0,0),
            opacity: 1
        })

        //Lochmarke
        pages[pageCounter - 1].drawLine({
            start: getCoordinatesForPDFLib(0,148.5,page1),
            end: getCoordinatesForPDFLib(7,148.5,page1),
            thickness: 0.25,
            color: rgb(0,0,0),
            opacity: 1
        })

        //Falzmarke 2
        pages[pageCounter - 1].drawLine({
            start: getCoordinatesForPDFLib(0,210,page1),
            end: getCoordinatesForPDFLib(7,210,page1),
            thickness: 0.25,
            color: rgb(0,0,0),
            opacity: 1
        })


        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,45,page1),
            end: getCoordinatesForPDFLib(105,45,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        pages[pageCounter - 1].drawText(invoiceData.adressLine, {
            ...getCoordinatesForPDFLib(21,48, page1),
            size:6,
            color:rgb(0,0,0),
            lineHeight:6,
            opacity: 1,
            maxWidth: 240
        })


        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,50,page1),
            end: getCoordinatesForPDFLib(105,50,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/




        pages[pageCounter - 1].drawText(invoiceData.recipient.name, {
            ...getCoordinatesForPDFLib(21,55, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        if(invoiceData.recipient.contact && !invoiceData.recipient.special) {
            pages[pageCounter - 1].drawText(invoiceData.recipient.contact, {
                ...getCoordinatesForPDFLib(21,60, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(invoiceData.recipient.street, {
                ...getCoordinatesForPDFLib(21,65, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(`${invoiceData.recipient.zip} ${invoiceData.recipient.city}`, {
                ...getCoordinatesForPDFLib(21,70, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        } else if(invoiceData.recipient.contact && invoiceData.recipient.special) {
            pages[pageCounter - 1].drawText(invoiceData.recipient.contact, {
                ...getCoordinatesForPDFLib(21,60, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(invoiceData.recipient.special, {
                ...getCoordinatesForPDFLib(21,65, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })


            pages[pageCounter - 1].drawText(invoiceData.recipient.street, {
                ...getCoordinatesForPDFLib(21,70, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(`${invoiceData.recipient.zip} ${invoiceData.recipient.city}`, {
                ...getCoordinatesForPDFLib(21,75, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        } else if(!invoiceData.recipient.contact && !invoiceData.recipient.special) {

            pages[pageCounter - 1].drawText(invoiceData.recipient.street, {
                ...getCoordinatesForPDFLib(21,60, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(`${invoiceData.recipient.zip} ${invoiceData.recipient.city}`, {
                ...getCoordinatesForPDFLib(21,65, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        } else {
            pages[pageCounter - 1].drawText(invoiceData.recipient.contact, {
                ...getCoordinatesForPDFLib(21,60, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(invoiceData.recipient.special, {
                ...getCoordinatesForPDFLib(21,65, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })


            pages[pageCounter - 1].drawText(invoiceData.recipient.street, {
                ...getCoordinatesForPDFLib(21,70, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
            pages[pageCounter - 1].drawText(`${invoiceData.recipient.zip} ${invoiceData.recipient.city}`, {
                ...getCoordinatesForPDFLib(21,75, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        }






        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,90,page1),
            end: getCoordinatesForPDFLib(105,90,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/


        //Rechts

        /*page1.drawLine({
            start: getCoordinatesForPDFLib(125,50,page1),
            end: getCoordinatesForPDFLib(200,50,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        pages[pageCounter - 1].drawText(invoiceData.info.documentNumberTitle, {
            ...getCoordinatesForPDFLib(126,55, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText(invoiceData.info.documentNumber ? invoiceData.info.documentNumber : "XXXX", {
            y: getCoordinatesForPDFLib(126,55, page1).y,
            x: getCoordinatesForPDFLib(126,55,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.documentNumber ? invoiceData.info.documentNumber : "XXXX",10),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })
        pages[pageCounter - 1].drawText("Kundennummer", {
            ...getCoordinatesForPDFLib(126,60, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText(invoiceData.info.customerNumber, {
            y: getCoordinatesForPDFLib(126,60, page1).y,
            x: getCoordinatesForPDFLib(126,60,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.customerNumber,10),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText("Belegdatum", {
            ...getCoordinatesForPDFLib(126,65, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText(invoiceData.info.documentDate, {
            y: getCoordinatesForPDFLib(126,65, page1).y,
            x: getCoordinatesForPDFLib(126,65,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.documentDate,10),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        if(invoiceData.info.deliveryDateType !== "Kein Lieferdatum anzeigen") {
            pages[pageCounter - 1].drawText(invoiceData.info.deliveryDateType, {
                ...getCoordinatesForPDFLib(126,70, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText(invoiceData.info.deliveryDate, {
                y: getCoordinatesForPDFLib(126,70, page1).y,
                x: getCoordinatesForPDFLib(126,70,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.deliveryDate,10),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        }



        pages[pageCounter - 1].drawText("Ansprechpartner", {
            ...getCoordinatesForPDFLib(126,75, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText(invoiceData.info.contactPerson, {
            y: getCoordinatesForPDFLib(126,75, page1).y,
            x: getCoordinatesForPDFLib(126,75,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactPerson,10),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        if(invoiceData.info.contactTel && invoiceData.info.contactEMail) {
            pages[pageCounter - 1].drawText("Telefon", {
                ...getCoordinatesForPDFLib(126,80, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText(invoiceData.info.contactTel, {
                y: getCoordinatesForPDFLib(126,80, page1).y,
                x: getCoordinatesForPDFLib(126,80,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactTel,10),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText("E-Mail", {
                ...getCoordinatesForPDFLib(126,85, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText(invoiceData.info.contactEMail, {
                y: getCoordinatesForPDFLib(126,85, page1).y,
                x: getCoordinatesForPDFLib(126,85,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactEMail,10),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        } else if(invoiceData.info.contactTel && !invoiceData.info.contactEMail) {
            pages[pageCounter - 1].drawText("Telefon", {
                ...getCoordinatesForPDFLib(126,80, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText(invoiceData.info.contactTel, {
                y: getCoordinatesForPDFLib(126,80, page1).y,
                x: getCoordinatesForPDFLib(126,80,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactTel,10),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        } else if(!invoiceData.info.contactTel && invoiceData.info.contactEMail) {
            pages[pageCounter - 1].drawText("E-Mail", {
                ...getCoordinatesForPDFLib(126,80, page1),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })

            pages[pageCounter - 1].drawText(invoiceData.info.contactEMail, {
                y: getCoordinatesForPDFLib(126,80, page1).y,
                x: getCoordinatesForPDFLib(126,80,page1).x + 210 - font.widthOfTextAtSize(invoiceData.info.contactEMail,10),
                size:10,
                color:rgb(0,0,0),
                lineHeight:10,
                opacity: 1,
                maxWidth: 240
            })
        }



        /*pages[pageCounter - 1].drawText("Projekt:", {
            ...getCoordinatesForPDFLib(126,90, page1),
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 240
        })

        pages[pageCounter - 1].drawText(invoiceData.info.project, {
            y: getCoordinatesForPDFLib(126,90, page1).y,
            x: getCoordinatesForPDFLib(126,90,page1).x + 50 /!*+ 210 - font.widthOfTextAtSize(invoiceData.info.project,10)*!/,
            size:10,
            color:rgb(0,0,0),
            lineHeight:10,
            opacity: 1,
            maxWidth: 150
        })*/

        /*page1.drawLine({
            start: getCoordinatesForPDFLib(125,90,page1),
            end: getCoordinatesForPDFLib(200,90,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        //Title

        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,95,page1),
            end: getCoordinatesForPDFLib(200,95,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        pages[pageCounter - 1].drawText(invoiceData.title, {
            ...getCoordinatesForPDFLib(20,100, page1),
            size:13,
            color:rgb(0,0,0),
            lineHeight:15,
            opacity: 1,
            maxWidth: 500
        })

        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,105,page1),
            end: getCoordinatesForPDFLib(200,105,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        if(invoiceData.description) {
            pages[pageCounter - 1].drawText(invoiceData.description, {
                ...getCoordinatesForPDFLib(20,112, page1),
                size:13,
                color:rgb(0,0,0),
                lineHeight:15,
                opacity: 1,
                maxWidth: 500
            })
        }


        pages[pageCounter - 1].drawText(invoiceData.startText,{
            ...getCoordinatesForPDFLib(20,119, page1),
            size: 10,
            color: rgb(0,0,0),
            lineHeight: 10,
            opacity: 1,
            maxWidth: 500
        })

        /*page1.drawLine({
            start: getCoordinatesForPDFLib(20,115,page1),
            end: getCoordinatesForPDFLib(200,115,page1),
            thickness: 0.5,
            color: rgb(0,0,0),
            opacity: 1
        })*/

        pages[pageCounter - 1].drawRectangle({
            ...getCoordinatesForPDFLib(20,140, page1),
            width: 180 * 2.83,
            height: 8 * 2.83,
            color: rgb(0,0,0),
            opacity: 0.25
        })

        //Header

        pages[pageCounter - 1].drawText("Pos", {
            ...getCoordinatesForPDFLib(21,137, page1),
            size:12,
            color:rgb(0,0,0),
            lineHeight:12,
            opacity: 1,
            maxWidth: 240,
            font: fontBold
        })

        pages[pageCounter - 1].drawText("Menge", {
            ...getCoordinatesForPDFLib(35,137, page1),
            size:12,
            color:rgb(0,0,0),
            lineHeight:12,
            opacity: 1,
            maxWidth: 240,
            font: fontBold
        })

        pages[pageCounter - 1].drawText("Bezeichnung", {
            ...getCoordinatesForPDFLib(52,137, page1),
            size:12,
            color:rgb(0,0,0),
            lineHeight:12,
            opacity: 1,
            maxWidth: 240,
            font: fontBold
        })

        if( invoiceData.type !== "deliveryNotes") {
            pages[pageCounter - 1].drawText("Einheitspreis", {
                ...getCoordinatesForPDFLib(135,137, page1),
                size:12,
                color:rgb(0,0,0),
                lineHeight:12,
                opacity: 1,
                maxWidth: 240,
                font: fontBold
            })
            pages[pageCounter - 1].drawText("Gesamt", {
                y: getCoordinatesForPDFLib(25,137, page1).y,
                x: getCoordinatesForPDFLib(25,137,page1).x + 490 - fontBold.widthOfTextAtSize("Gesamt",12),
                size:12,
                color:rgb(0,0,0),
                lineHeight:12,
                opacity: 1,
                maxWidth: 240,
                font: fontBold
            })
        }





        let rowHeight = 145.5

        let pageIndex = 0


        invoiceData.rows.forEach((row,index) => {



            if(!["pagebreak","title","text"].includes(row.mode)){

                console.log(row)

                pages[pageCounter - 1].drawText(String(row.pos), {
                    ...getCoordinatesForPDFLib(21,rowHeight, page1),
                    size:10,
                    color:rgb(0,0,0),
                    lineHeight:10,
                    opacity: 1,
                    maxWidth: 240
                })

                pages[pageCounter - 1].drawText(`${row.quantity} ${row.unit}`, {
                    ...getCoordinatesForPDFLib(35,rowHeight, page1),
                    size:10,
                    color:rgb(0,0,0),
                    lineHeight:10,
                    opacity: 1,
                    maxWidth: 240
                })

                console.log(row.text.match(/.{1,35}/g))

                if(invoiceData.type !== "deliveryNotes") {
                    pages[pageCounter - 1].drawText(row.text.match(/.{1,35}/g).join("\n"), {
                        ...getCoordinatesForPDFLib(52,rowHeight, page1),
                        size:10,
                        color:rgb(0,0,0),
                        lineHeight:10,
                        opacity: 1,
                        font: fontBold
                    })
                } else {
                    pages[pageCounter - 1].drawText(row.text.match(/.{1,80}/g).join("\n"), {
                        ...getCoordinatesForPDFLib(52,rowHeight, page1),
                        size:10,
                        color:rgb(0,0,0),
                        lineHeight:10,
                        opacity: 1,
                        font: fontBold
                    })
                }



                //let textLineBreaks = (row.text.match(/\n/g) || []).length
                let textAutoLines = (invoiceData.type !== "deliveryNotes" ? row.text.match(/.{1,35}/g) : row.text.match(/.{1,80}/g) || []).length
                console.log(textAutoLines)

                console.log(row)


                if(row.descriptionText) {
                    if(invoiceData.type !== "deliveryNotes") {
                        pages[pageCounter - 1].drawText(row.descriptionText.match(/.{1,70}/g).join("\n"), {
                            ...getCoordinatesForPDFLib(52,rowHeight + ( textAutoLines * 4), page1),
                            size:10,
                            color:rgb(0,0,0),
                            lineHeight:10,
                            opacity: 1,
                        })
                    } else {
                        pages[pageCounter - 1].drawText(row.descriptionText.match(/.{1,80}/g).join("\n"), {
                            ...getCoordinatesForPDFLib(52,rowHeight + ( textAutoLines * 4), page1),
                            size:10,
                            color:rgb(0,0,0),
                            lineHeight:10,
                            opacity: 1,
                        })
                    }


                }


                if(invoiceData.type !== "deliveryNotes") {
                    pages[pageCounter - 1].drawText(row.price, {
                        ...getCoordinatesForPDFLib(135,rowHeight, page1),
                        size:10,
                        color:rgb(0,0,0),
                        lineHeight:10,
                        opacity: 1,
                        maxWidth: 240
                    })

                    pages[pageCounter - 1].drawText(row.rowAmount, {
                        y: getCoordinatesForPDFLib(25,rowHeight, page1).y,
                        x: getCoordinatesForPDFLib(25,rowHeight,page1).x + 490 - font.widthOfTextAtSize(row.rowAmount,10),
                        size:10,
                        color:rgb(0,0,0),
                        lineHeight:10,
                        opacity: 1,
                        maxWidth: 240,
                    })

                    if(row.discountPercent > 0) {
                        pages[pageCounter - 1].drawText(row.discountText, {
                            y: getCoordinatesForPDFLib(25,rowHeight + 5, page1).y,
                            x: getCoordinatesForPDFLib(25,rowHeight + 5,page1).x + 490 - font.widthOfTextAtSize(row.discountText,8),
                            size:8,
                            color:rgb(0,0,0),
                            lineHeight:10,
                            opacity: 1,
                            maxWidth: 240,
                        })
                    }
                }





                if(row.descriptionText) {
                    let lineBreaks = (row.descriptionText.match(/\n/g) || []).length
                    let autoLines = (row.descriptionText.match(/.{1,70}/g) || []).length
                    rowHeight += 12 + (lineBreaks + autoLines + textAutoLines) * 2.5
                } else {
                    rowHeight += 12 + textAutoLines * 2.5
                }


                pageIndex += 1


            } else if(row.mode === 'pagebreak') {

                const page = pdfDoc.addPage()

                page.drawPage(secondPageBackground, {
                    x: 0,
                    y: 0,
                })

                //Falzmarke 1
                page.drawLine({
                    start: getCoordinatesForPDFLib(0,105,page1),
                    end: getCoordinatesForPDFLib(7,105,page1),
                    thickness: 0.25,
                    color: rgb(0,0,0),
                    opacity: 1
                })

                //Lochmarke
                page.drawLine({
                    start: getCoordinatesForPDFLib(0,148.5,page1),
                    end: getCoordinatesForPDFLib(7,148.5,page1),
                    thickness: 0.25,
                    color: rgb(0,0,0),
                    opacity: 1
                })

                //Falzmarke 2
                page.drawLine({
                    start: getCoordinatesForPDFLib(0,210,page1),
                    end: getCoordinatesForPDFLib(7,210,page1),
                    thickness: 0.25,
                    color: rgb(0,0,0),
                    opacity: 1
                })

                page.drawRectangle({
                    ...getCoordinatesForPDFLib(20,25, page1),
                    width: 180 * 2.83,
                    height: 8 * 2.83,
                    color: rgb(0,0,0),
                    opacity: 0.25
                })

                //Header

                page.drawText("Pos", {
                    ...getCoordinatesForPDFLib(21,22, page1),
                    size:12,
                    color:rgb(0,0,0),
                    lineHeight:12,
                    opacity: 1,
                    maxWidth: 240,
                    font: fontBold
                })

                page.drawText("Menge", {
                    ...getCoordinatesForPDFLib(35,22, page1),
                    size:12,
                    color:rgb(0,0,0),
                    lineHeight:12,
                    opacity: 1,
                    maxWidth: 240,
                    font: fontBold
                })

                page.drawText("Bezeichnung", {
                    ...getCoordinatesForPDFLib(52,22, page1),
                    size:12,
                    color:rgb(0,0,0),
                    lineHeight:12,
                    opacity: 1,
                    maxWidth: 240,
                    font: fontBold
                })

                if(invoiceData.type !== "deliveryNotes"){
                    page.drawText("Einheitspreis", {
                        ...getCoordinatesForPDFLib(135,22, page1),
                        size:12,
                        color:rgb(0,0,0),
                        lineHeight:12,
                        opacity: 1,
                        maxWidth: 240,
                        font: fontBold
                    })

                    page.drawText("Gesamt", {
                        y: getCoordinatesForPDFLib(25,22, page1).y,
                        x: getCoordinatesForPDFLib(25,22,page1).x + 490 - fontBold.widthOfTextAtSize("Gesamt",12),
                        size:12,
                        color:rgb(0,0,0),
                        lineHeight:12,
                        opacity: 1,
                        maxWidth: 240,
                        font: fontBold
                    })
                }



                pageCounter += 1;
                pageIndex = 0;
                rowHeight = 30;

                pages.push(page)
                //console.log(pages)




            } else if (row.mode === 'title') {
                if(index === 0 ||pageIndex === 0) {
                    rowHeight += 3
                }

                pages[pageCounter - 1].drawText(String(row.pos), {
                    ...getCoordinatesForPDFLib(21,rowHeight -3, page1),
                    size:10,
                    color:rgb(0,0,0),
                    lineHeight:10,
                    opacity: 1,
                    maxWidth: 240,
                    font: fontBold
                })


                pages[pageCounter - 1].drawRectangle({
                    ...getCoordinatesForPDFLib(20,rowHeight, page1),
                    width: 180 * 2.83,
                    height: 8 * 2.83,
                    color: rgb(0,0,0),
                    opacity: 0.25
                })

                pages[pageCounter - 1].drawText(row.text, {
                    ...getCoordinatesForPDFLib(35,rowHeight -3, page1),
                    size:12,
                    color:rgb(0,0,0),
                    lineHeight:12,
                    opacity: 1,
                    maxWidth: 500,
                    font: fontBold
                })

                rowHeight += 8
            } else if (row.mode === 'text') {
                if(index === 0 ||pageIndex === 0) {
                    rowHeight += 3
                }

                let textAutoLines = (row.text.match(/.{1,60}/g) || []).length

                pages[pageCounter - 1].drawRectangle({
                    ...getCoordinatesForPDFLib(20,rowHeight, page1),
                    width: 180 * 2.83,
                    height: 8 * 2.83,
                    color: rgb(0,0,0),
                    opacity: 0.25
                })

                pages[pageCounter - 1].drawText(row.text.match(/.{1,60}/g).join("\n"), {
                    ...getCoordinatesForPDFLib(21,rowHeight - 2.6, page1),
                    size:12,
                    color:rgb(0,0,0),
                    lineHeight:12,
                    opacity: 1,
                    font: fontBold
                })

                //let textLineBreaks = (row.text.match(/\n/g) || []).length
                //console.log(textAutoLines)


                if(row.descriptionText) {
                    pages[pageCounter - 1].drawText(row.descriptionText.match(/.{1,100}/g).join("\n"), {
                        ...getCoordinatesForPDFLib(21,rowHeight + ( textAutoLines * 4), page1),
                        size:10,
                        color:rgb(0,0,0),
                        lineHeight:10,
                        opacity: 1,
                    })

                    let addHeight = (row.descriptionText.match(/.{1,80}/g) || []).length * 4 + 10
                    console.log(addHeight)
                    rowHeight += addHeight
                } else {
                    rowHeight += 6
                }



            }


        })


        //Pos 1





        //Footer
        //console.log(rowHeight)
        //rowHeight += 25

        if(invoiceData.type !== "deliveryNotes"){
            pages[pageCounter - 1].drawRectangle({
                ...getCoordinatesForPDFLib(20,rowHeight, page1),
                width: 180 * 2.83,
                height: 8 * 2.83,
                color: rgb(0,0,0),
                opacity: 0.25
            })

            pages[pageCounter - 1].drawRectangle({
                ...getCoordinatesForPDFLib(20,rowHeight +16, page1),
                width: 180 * 2.83,
                height: 8 * 2.83,
                color: rgb(0,0,0),
                opacity: 0.25
            })

            pages[pageCounter - 1].drawText("Nettobetrag", {
                ...getCoordinatesForPDFLib(21,rowHeight-3, page1),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font: fontBold
            })

            pages[pageCounter - 1].drawText(invoiceData.total.totalNet, {
                y: getCoordinatesForPDFLib(21,rowHeight-3, page1).y,
                x: getCoordinatesForPDFLib(21,rowHeight-3,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.totalNet,11),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font:fontBold
            })

            pages[pageCounter - 1].drawText("zzgl. 19% MwSt", {
                ...getCoordinatesForPDFLib(21,rowHeight+5, page1),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font: fontBold
            })

            pages[pageCounter - 1].drawText(invoiceData.total.total19, {
                y: getCoordinatesForPDFLib(21,rowHeight+5, page1).y,
                x: getCoordinatesForPDFLib(21,rowHeight+5,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.total19,11),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font:fontBold
            })

            pages[pageCounter - 1].drawText("Gesamtsumme", {
                ...getCoordinatesForPDFLib(21,rowHeight+13, page1),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font: fontBold
            })

            pages[pageCounter - 1].drawText(invoiceData.total.totalGross, {
                y: getCoordinatesForPDFLib(21,rowHeight+13, page1).y,
                x: getCoordinatesForPDFLib(21,rowHeight+13,page1).x + 500 - fontBold.widthOfTextAtSize(invoiceData.total.totalGross,11),
                size:11,
                color:rgb(0,0,0),
                lineHeight:11,
                opacity: 1,
                maxWidth: 240,
                font:fontBold
            })
        }


        pages[pageCounter - 1].drawText(invoiceData.endText,{
            ...getCoordinatesForPDFLib(20,rowHeight+22, page1),
            size: 10,
            color: rgb(0,0,0),
            lineHeight: 10,
            opacity: 1,
            maxWidth: 500
        })

        //console.log(await pdfDoc.saveAsBase64({dataUri: true}))

        uri.value = await pdfDoc.saveAsBase64({dataUri: true})
        //console.log(uri.value)

    }

    await genPDF()

    //const pdfBytes = await pdfDoc.save()

    //const pdfDataUri = await pdfDoc.saveAsBase64({dataUri: true})
    return uri.value
    //let blob = new Blob(pdfBytes, {type: "application/pdf"})
    /*let link = document.createElement('a')
    link.href = pdfDataUri//window.URL.createObjectURL(blob)
    link.download = "test.pdf"
    link.click()*/

}