import Mapping from "../domain/Mapping";
import WebSite from "../domain/WebSite";
import WebSiteService from "../application/WebSiteService";

export default function attachRoutes(app, webSiteService: WebSiteService) {

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

    app.get("/website/:id", (req, res) => {
        const id = req.params.id;
        // console.log("WebSite GET: ",id);
        if (id == undefined) {
            res.status(400).send("id is undefined");
            return;
        }
        else {
            webSiteService.findWebSiteById(id)
                .then(webSite => {
                    if (!(webSite instanceof WebSite)) {
                        res.status(500).send("wrong type, not a webSite");
                    }
                    res.json({
                        id: webSite.id,
                        name: webSite.name,
                        url: webSite.url,
                        mappingList: webSite.mappingList
                    });
                })
                .catch((e) => {
                    // tslint:disable-next-line: no-console
                    console.error(e);
                    res.status(500).send(e);
                });
        }
    });

    app.post("/website/create",  (req, res) => {
        const {name, url} = req.body;
        const mappingListData = req.body.mappingList;
        const mappingList: Mapping[] = [];
        try {
            mappingListData.forEach((mappingData) => {
                mappingList.push(new Mapping(mappingData.match, mappingData.output, mappingData.context));
            });
        } catch (e) {
            console.error(e);
            return res.status(400).send({message: e.message});
        }
        const webSite = new WebSite(name, url);
        webSite.addMappingList(mappingList);
        webSiteService.createWebSite(webSite)
            .then(webSiteId => {
                    res.json(webSiteId);
                })
            .catch ((e) => {
                console.error(e);
                res.status(409).send({message: e.message});
            });
     });

    app.post("/website/update",  (req, res) => {
        const {id, name, url} = req.body;
        const mappingListData = req.body.mappingList;
        const mappingList: Mapping[] = [];
        try {
            mappingListData.forEach((mappingData) => {
                mappingList.push(new Mapping(mappingData.match, mappingData.output, mappingData.context));
            });
        } catch (e) {
            return res.status(400).send({message: "Invalid mapping file"});
        }
        try {
            const webSite = new WebSite(name, url, id);
            webSite.addMappingList(mappingList);
            // console.log('website update:', webSite);
            webSiteService.updateWebSite(webSite)
                .then((webSiteId) => {
                    res.json(webSiteId);
                });
        } catch (e) {
            res.status(409).send({message: e.message});
        }
    });
}
