
import Parent from '../base/serve.mjs'

export default class ServeHandler extends Parent {
  async serveIndex(ctx) {
    let payload = {
      headerDescription: 'Small fansubbing and scanlation group translating and encoding our favourite shows from Japan.',
      headerImage: this.frontend + '/assets/img/heart.png',
      headerTitle: 'NFP Moe - Anime/Manga translation group',
      headerUrl: this.frontend + ctx.url,
      payloadData: null,
      payloadTree: null,
      version: this.version,
      nonce: ctx.state.nonce,
    }

    try {
      payload.payloadTree = JSON.stringify(await this.pageRoutes.getPageTree(ctx, true))
      if (ctx.url === '/' || (ctx.url.startsWith('/page/') && ctx.url.lastIndexOf('/') === 5)) {
        ctx.params.path = null
        if (ctx.url.lastIndexOf('/') === 5) {
          ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
        }
        payload.payloadData = JSON.stringify(await this.pageRoutes.getPage(ctx, true))
      }
      console.log('url', ctx.url)
    } catch (e) {
      ctx.log.error(e)
    }

    ctx.body = this.template(payload)
    ctx.type = 'text/html; charset=utf-8'
  }
}