import { defineStore } from 'pinia'
import { useDataStore } from '@/stores/dataStore'
import { useGlobalStore } from '@/stores/globalStore'
import htmlToPdfMake from 'html-to-pdfmake'

export const usePreviewStore = defineStore('previewStore', () => {
  const dataStore = useDataStore()
  const globalStore = useGlobalStore()

  const DownloadPDF = async (content: string) => {
    var pdfData = `<style>
    .form-item{
      width: 100%;
      border: 1px solid #f2f2f2;
      float:left;
    }
      .form-item-half{
      width:49%;
      }
  .display-title{
    width: 100%;
    padding: 4px 0;
    border-bottom: 1px solid #f2f2f2;
    margin-bottom: 8px;
  }
    </style>`

    pdfData += content

    let pdf: any = await dataStore.dataPost('Auth/GeneratePdfFromHtml/', {
      data: pdfData,
      options: {
        responseType: 'arraybuffer'
      },
      full: true
    })

    var filename = ''
    var dispos = pdf.headers['content-disposition']
    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
    var matches = filenameRegex.exec(dispos)

    if (matches != null && matches[1]) {
      filename = matches[1].replace(/['"]/g, '')
      filename = decodeURIComponent(filename)
    }

    const file = new Blob([pdf.data], { type: 'application/pdf' })
    const url = URL.createObjectURL(file)

    const link = document.createElement('a')
    link.href = url
    link.download = globalStore.CreateSlug(filename)
    document.body.appendChild(link)
    link.dispatchEvent(
      new MouseEvent('click', { bubbles: true, cancelable: true, view: window })
    )
    link.remove()
    window.URL.revokeObjectURL(link.href)
  }

  const CreatePdfFromHtml = (make: any, content: string) => {
    const converted = htmlToPdfMake(content)
    var tableLayouts = {
      pdfTableLayout: {
        hLineWidth: function () {
          return 1
        },
        vLineWidth: function () {
          return 1
        },
        hLineColor: function () {
          return '#f2f2f2'
        },
        vLineColor: function () {
          return '#f2f2f2'
        },
        paddingLeft: function () {
          return 4
        },
        paddingRight: function () {
          return 4
        },
        paddingTop: function () {
          return 4
        },
        paddingBottom: function () {
          return 4
        }
      },
      pdfTableLayoutNoLine: {
        hLineWidth: function () {
          return 0
        },
        vLineWidth: function () {
          return 0
        },
        hLineColor: function () {
          return '#fff'
        },
        vLineColor: function () {
          return '#fff'
        },
        paddingLeft: function () {
          return 0
        },
        paddingRight: function () {
          return 0
        },
        paddingTop: function () {
          return 0
        },
        paddingBottom: function () {
          return 4
        }
      }
    }
    make.addTableLayouts(tableLayouts)

    const docDefinition = {
      pageSize: 'A4',
      pageMargins: [20, 40, 20, 40],
      content: converted
    }
    make.createPdf(docDefinition).download()
  }

  const PrintContent = (id: number) => {
    var el: HTMLElement = document.getElementById('pre' + id) as HTMLElement
    var printContent = el.innerHTML
    document.body.classList.add('print')
    const printEl = document.createElement('div')

    printEl.className = 'print-c'
    printEl.innerHTML = printContent
    document.body.appendChild(printEl)
    window.print()
    document.body.classList.remove('print')
    printEl.remove()
  }

  return {
    DownloadPDF,
    PrintContent,
    CreatePdfFromHtml
  }
})
