import contentDisposition from 'content-disposition'
import Router from 'koa-router'

import { setAccessTokenCookie } from '@things-factory/auth-base'
import { getRepository } from '@things-factory/shell'

import { headlessModel } from '../controllers/headless-model'
import { screenshot } from '../controllers/screenshot'
import { Process } from '../service/process/process'
import { pdf } from '../controllers/pdf'
import { fonts } from '../controllers/fonts'

export const standaloneProcessServiceRouter = new Router()

// for process headless
standaloneProcessServiceRouter.get('/process-headless-full/:id', async (context, next) => {
  const { domain, user } = context.state
  const { id } = context.params

  const { model, base } = await headlessModel({ domain, id })
  model.fonts = await fonts()

  setAccessTokenCookie(context, await user.sign({ domain }))

  await context.render('internal-process-full-feature-view', { model, base })
})

// for process headless
standaloneProcessServiceRouter.get('/process-headless/:id', async (context, next) => {
  const { domain, user } = context.state
  const { id } = context.params

  const { model, base } = await headlessModel({ domain, id })
  model.fonts = await fonts()

  setAccessTokenCookie(context, await user.sign({ domain }))

  await context.render('internal-process-service-view', { model, base })
})

// for process thumbnail
standaloneProcessServiceRouter.get('/process-thumbnail/:id', async (context, next) => {
  const { domain } = context.state
  const { id } = context.params

  const { name, thumbnail } = await getRepository(Process).findOneBy({ domain: { id: domain.id }, id })

  if (thumbnail) {
    const index = thumbnail.indexOf(';base64,')
    const base64 = thumbnail.substring(index + 8)
    const buffer = Buffer.from(base64, 'base64')

    context.type = 'image/png'
    context.set('Content-Disposition', contentDisposition(`thumbnail-${name}.png`))
    context.body = buffer
  } else {
    context.throw(404, 'thumbnail not found')
  }
})

// for webpage scrap
standaloneProcessServiceRouter.get('/process-screenshot/:id', async (context, next) => {
  const { domain } = context.state
  const { id } = context.params

  const { model, process } = await headlessModel({ domain, id })
  const data = Object.keys(context.query).length ? context.query : null

  context.type = 'image/png'
  context.set('Content-Disposition', contentDisposition(`${process.name}.png`))
  context.body = await screenshot({ id, model, data, options: { encoding: 'binary', type: 'png' }, context })
})

// for webpage scrap
standaloneProcessServiceRouter.get('/process/pdf/:id', async (context, next) => {
  const { domain } = context.state
  const { id } = context.params

  const { model, process } = await headlessModel({ domain, id })
  const data = Object.keys(context.query).length ? context.query : null

  const result = await pdf({
    id,
    model,
    data,
    options: {
      format: 'A4'
    },
    context
  })

  context.type = 'application/pdf'
  context.set('Content-Disposition', contentDisposition(`${process.name}.pdf`))
  context.body = result
})
