import {Request, Response} from "express";

import {boulderRouteStore} from "../services/boulderRoute-store";
let imgCounter = 0;
export class IndexController {
    sortByDifficulty(req: Request, res: Response): void {
        req.settings.orderBy = "routeDifficulty";
        req.settings.orderDirection = (req.settings.orderDirection == 1) ? -1 : 1;
        res.redirect("/");
    }

    sortByTitle(req: Request, res: Response): void {
        req.settings.orderBy = "boulderRouteTitle";
        req.settings.orderDirection = (req.settings.orderDirection == 1) ? -1 : 1;
        res.redirect("/");
    }

    sortByCreationDate(req: Request, res: Response): void {
        req.settings.orderBy = "boulderRouteCreated";
        req.settings.orderDirection = (req.settings.orderDirection == 1) ? -1 : 1;
        res.redirect("/");
    }

    toggleStyle(req: Request, res: Response): void {
        req.settings.dark = !req.settings.dark;
        res.redirect(req.body.path);
    }

    toggleCompleted(req: Request, res: Response): void {
        req.settings.filterCompleted = !req.settings.filterCompleted;
        res.redirect("/");
    }

    async showAllRoutes(req: Request, res: Response) {
        const query = req.settings.filterCompleted ? {boulderRouteDone: false} : {};

        const boulderRoutes = await boulderRouteStore.get(
            req.settings.orderBy,
            req.settings.orderDirection,
            query
        );

        res.render("allRoutes", {
            current: req.url,
            boulderRoutes: boulderRoutes,
            dark: req.settings.dark,
            filterCompleted: req.settings.filterCompleted,
            orderBy: req.settings.orderBy,
            orderDirection: req.settings.orderDirection,
            activeNode: Number(process.env.APPID)
        });
    }

    addBoulderRoute(req: Request, res: Response): void {
        res.render("detailView", {current: req.url, dark: req.settings.dark});
    }

    async saveBoulderRoute(req: Request, res: Response): Promise<void> {
        const newBoulderRoute = await boulderRouteStore.add(
            req.body.title,
            req.body.description,
            req.body.difficulty,
            req.body.done,
            'Boulder' + imgCounter + '.png'
        );
        imgCounter++;
        if (req.body.submit == "Save") {
            res.redirect(`/${newBoulderRoute?._id}`);
        } else if (req.body.submit == "Save and New") {
            res.redirect("/add");
        } else {
            res.redirect("/");
        }
    }

    async updateBoulderRoute(req: Request, res: Response): Promise<void> {
        const doneValue = (req.body.done == "on");
        await boulderRouteStore.update(
            doneValue,
            req.body.title,
            req.body.description,
            req.body.difficulty,
            req.params.id
        );

        if (req.body.submit == "Save") {
            res.redirect(`/${req.params.id}`);
        } else if (req.body.submit == "Save and New") {
            res.redirect("/add");
        } else {
            res.redirect("/");
        }
    }

    async detailView(req: Request, res: Response): Promise<void> {
        const boulderRoute = await boulderRouteStore.getOne(req.params.id);

        if (boulderRoute) {
            res.render("detailView", {current: req.url, boulderRoute: boulderRoute[0], dark: req.settings.dark});
        } else {
            res.render("error", {current: req.url, msg: "Boulder Route not found in database.", dark: req.settings.dark});
        }
    }
}

export const indexController = new IndexController();
