const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const mongoose = require("mongoose");
const sharp = require("sharp");
const app = express();

// const history = [];

app.use(bodyParser.json());
app.use(express.static("chat"));
mongoose.connect("mongodb://localhost/chat", { useNewUrlParser: true });

const db = mongoose.connection;

const messageSchema = new mongoose.Schema({
  nick: String,
  message: String,
  image: String,
  replyTo: mongoose.Types.ObjectId,
});

const Message = mongoose.model("Message", messageSchema);
const Image = mongoose.model("Image", messageSchema);

db.on("error", console.error.bind(console, "connection error:"));

//MESSAGE
app.get("/message", async (req, res) => res.send(await Message.find()));

app.get("/message/:id", async (req, res) => {
  res.send(
    await Message.findOne({ _id: mongoose.Types.ObjectId(req.params.id) })
  );
});

app.put("/message/:id", async (req, res) => {
  res.send(
    await Message.updateOne(
      { _id: mongoose.Types.ObjectId(req.params.id) },
      {
        nick: req.body.nick,
        message: req.body.message,
      }
    )
  );
});

app.delete("/message/:id", async (req, res) => {
  res.send(
    await Message.remove({ _id: mongoose.Types.ObjectId(req.params.id) })
  );
});

app.post("/message", async (req, res) => {
  let newMessage = new Message(req.body);
  await newMessage.save();
  res.status(201).send(newMessage);
});

//IMAGE
app.get("/image", async (req, res) => res.send(await Image.find()));

app.get("/image/:id", async (req, res) =>
  res.send(await Image.findOne({ _id: mongoose.Types.ObjectId(req.params.id) }))
);

app.delete("/image/:id", async (req, res) => {
  res.send(await Image.remove({ image: req.params.id }));
});

app.post("/image", async (req, res) => {
  let newImage = new Image(req.body);
  await newImage.save();
  res.status(201).send(newImage);
});

// app.get("/image", (req, res) => {
//   fs.readdirSync("chat/image").forEach((image) => {
//     console.log(image);
//   });
//   res.end(JSON.stringify(fs.readdirSync("chat/image")));
// });

// app.post("/image", (req, res) => {
//   let fileName = Math.random().toString("36");
//   fileName = `image/${fileName}`;
//   let fileStream = fs.createWriteStream("chat/" + fileName);

//   req.pipe(fileStream);
//   req.on("end", () => {
//     res.end(fileName);
//   });
// });

// app.get("/message", (req, res) => {
//   res.send(JSON.stringify(history));
// });

// app.get("/", (req, res) => {
//   res.send(JSON.stringify(history));
// });

// app.get("/message/:messageId", (req, res) => {
//   res.send(JSON.stringify(history[req.params.messageId]));
// });

// app.put("/message/:messageId", (req, res) => {
//   history[req.params.messageId] = req.body;
//   res.send(JSON.stringify(history[req.params.messageId]));
// });

// app.delete("/message/:messageId", (req, res) => {
//   history[req.params.messageId] = null;
//   res.send(JSON.stringify(history[req.params.messageId]));
// });

// app.post("/message", (req, res) => {
//   history.push(req.body);
//   console.log(req.body);
//   res.status(201).send(req.body);
// });

app.listen(5000, () => console.log("listen on 5000 port"));
