/*
 * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blendb.
 *
 * blendb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * blendb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
 */

import * as express from "express";
import { Request } from "../types";

/**
 * Constroller responsable for the static part from the API. In other
 * words, controller responsable for return the meta data stored in the
 * engine object that API users can use to create queries.
 */
export class EngineCtrl {
    /**
     * Auxiliary function that returns engine information.
     * @param list - List of objects to return
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
    private static respondList(list: any[], fileName: string, req: Request, res: express.Response, next: express.NextFunction) {
        let format = "json";
        if (req.query.format) {
            format = req.query.format;
        }

        if (format === "json") {
            res.status(200).json(list);
        }

        else {
            req.csvParser(list, format, (error: Error, csv: string) => {
                if (error) {
                    res.status(500).json({
                        message: "Error generating csv file. " +
                        "Try json format.",
                        error: error
                    });
                    return;
                }

                const disposition = "attachment;filename=" + fileName + ".csv";
                res.setHeader("Content-Type", "text/csv");
                res.setHeader("Content-disposition", disposition);
                res.status(200).send(csv);
            });
        }
    }
    /**
     * Route that returns the list of available metrics.
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
    public static metrics(req: Request, res: express.Response, next: express.NextFunction) {
        const metrics = req.engine.getMetricsDescription();
        EngineCtrl.respondList(metrics, "metrics", req, res, next);
    }

    /**
     * Route that returns the list of available dimensions.
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
    public static dimensions(req: Request, res: express.Response, next: express.NextFunction) {
        const dimensions = req.engine.getDimensionsDescription();
        EngineCtrl.respondList(dimensions, "dimensions", req, res, next);
    }

    /**
     * Route that returns the list of available enumerable types.
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
    public static enumTypes(req: Request, res: express.Response, next: express.NextFunction) {
        const enumTypes = req.engine.getEnumTypesDescription();
        EngineCtrl.respondList(enumTypes, "enums", req, res, next);
    }

    /**
     * Route that returns the list of available data sources for insertion.
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
    public static sources(req: Request, res: express.Response, next: express.NextFunction) {
       const sources = req.engine.getSourcesDescription();
       EngineCtrl.respondList(sources, "sources", req, res, next);
    }
}