import PrintService from "../application/PrintService";

const ERROR_STATUS: number = 500;
const SUCCESS_STATUS: number = 200;

export default function attachRoutes(app, printService: PrintService): void {

    app.get("/printer/ping", (req, res) => {
        res.send("alive");
    });

    app.post("/printer/print/natural:sessionId", (req, res) => {
        const {sessionId} = req.params;
        printService.printSession(sessionId)
        .then((print) => {
            res.send({print});
        })
        .catch( (e) => {
            console.log(e);
            res.status(ERROR_STATUS).send("error");
        });
    });

    app.post("/printer/print/puppeteer/", (req, res) => {

        const {sessionId, headless, timeout} = req.body;

        printService.printPuppeteerSession(sessionId, {headless, timeout})
        .then((print) => {
            res.send({print});
        })
        .catch( (e) => {
            console.log(e);
            res.status(ERROR_STATUS).send("error");
        });
    });

}
