import express from 'express'
export const convertTemplateRoute = express.Router();
import {mimeToExtension} from './report-template'
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

/** javascript-obfuscator:disable
 * @swagger
 * tags:
 *   name: office-convert
 *   description: ใช้แปลงไฟล์จากเอกสารที่ libreoffice แปลงได้ x
*/

/** javascript-obfuscator:disable
 * @swagger
 * /api/v1/report-template/convert:
 *   post:
 *     summary: Create document from template
 *     tags: [office-convert]
 *     parameters:
 *       - name: report_name
 *         in: header
 *         description: นามสกุลไฟล์ที่อัปโหลดเข้ามา(ตอนนี้ไม่จำเป็นต้องใช้เพราะ soffice ตรวจสอบเอง)
 *         required: true
 *         schema:
 *           type: string
 *           example: report
 *     requestBody:
 *       required: true
 *       content:
 *          application/octet-stream:
 *           schema:
 *             type: string
 *             format: binary
 *     responses:
 *       201:
 *         description: file was converted.
 *         content:
 *           application/pdf:
 *             schema:
 *               type: string
 *               format: binary
 *           application/vnd.oasis.opendocument.text:
 *             schema:
 *               type: string
 *               format: binary
 *           application/vnd.openxmlformats-officedocument.wordprocessingml.document:
 *             schema:
 *               type: string
 *               format: binary
 *           application/msword:
 *             schema:
 *               type: string
 *               format: binary
 *           image/png:
 *             schema:
 *               type: string
 *               format: binary
 *           image/jpeg:
 *             schema:
 *               type: string
 *               format: binary
 *       500:
 *         description: Server error
 *
 */
convertTemplateRoute.post("/convert", async function (req, res) {
    try {
      if (!req.headers['content-type'] ||
        !req.headers['accept'] ||
        !req.headers['report_name'] ||
        req.headers['content-type'] !== "application/octet-stream") {
        res.statusCode = 400;
        res.statusMessage = 'Require header: content-type(application/octet-stream) accept, report_name';
        res.end(res.statusMessage);
        console.log(req.headers['content-type'],req.headers['accept'],req.headers['report_name']) 
        return
      }
      let outputMediaType = mimeToExtension(req.headers['accept']);
      let reportName = req.headers['report_name']
      console.log('output: ' + outputMediaType);
      const libreOfficeFileConverter = new LibreOfficeFileConverter({
        childProcessOptions: {
          timeout: 60 * 1000,
        },
      });
      const buffer = await libreOfficeFileConverter.convertBuffer(req.body, outputMediaType);
      res.statusCode = 201;
      res.setHeader('Content-Type', req.headers['accept']);
      res.setHeader('Content-Disposition', `attachment;filename=${reportName}.${outputMediaType}`);
      res.setHeader('Content-Length', buffer.length);
      res.end(buffer);
    } catch (ex) {
      res.statusCode = 500;
      res.statusMessage = 'Internal Server Error';
      res.end(res.statusMessage);
      console.error(`Error during convert with soffice:`, ex);
    }
  })
  