import fs from "fs";
import express from "express";
const router = express.Router();

import { extractFrames } from "../../src/app";

router.get("/", async (req: any, res: any, next: any) => {
  // return list of sprites
  try {
    res.json({ sprites: [1] });
  } catch (err) {
    next(err);
  }
});

router.get("/:id", async (req: any, res: any, next: any) => {
  // return requested sprites
  try {
    const files = ["./public/sprites/sprite-0.jpeg"]
    res.json({ sprites: files.map(file=> fs.readFileSync(file, 'utf8'))
     });
  } catch (err) {
    next(err);
  }
});

router.post("/", async (req: any, res: any, next: any) => {
  // generate sprites
  const { videoId } = req.body;

  if (!videoId)
    res.json({ error: "Post `{ videoId: number; }` to generate a sprite." });

  res.json({ error: "Not implemented" });
  try {
    extractFrames("./frames.mp4", videoId);
  } catch (err) {
    next(err);
  }
});

router.put("/:id", async (req: any, res: any, next: any) => {
  // update submitted sprite
  try {
    res.json({ error: "Not implemented" });
  } catch (err) {
    next(err);
  }
});

router.post("/:id/delete", async (req: any, res: any, next: any) => {
  // trash sprite
  try {
    res.json({ error: "Not implemented" });
  } catch (err) {
    next(err);
  }
});

module.exports = router;
