const fs = require('fs');
const express = require('express');

class sensei_image_api {
    constructor(port) {
        if (port && !isNaN(port)) {
            this.port = port;
        }
    }

    start() {
        const app = express();
        // Stop errors from showing up in the console.
        process.on('uncaughtException', expection => {
            return;
        });
        process.on('warning', warning => {
            return;
        });
        process.on('unhandledRejection', rejection => {
            return;
        });

        app.get('/', (req, res) => {
            res.setHeader('Content-Type', 'application/json');
            res.end(JSON.stringify({
                error: 'Nothing to see here.'
            }));
        });


        // This will make life a lot easier.
        function getModule(name, req, res) {
            try {
                require(`${__dirname}/modules/${name}`)(req, res);
            } catch (err) {
                res.setHeader('Content-Type', 'application/json');
                res.send(JSON.stringify({
                    error: err
                }));
            }
        }

        app.get('/grayscale', (req, res) => {
            getModule('grayscale', req, res);
        });

        app.get('/rotate', async (req, res) => {
            getModule('rotate', req, res);
        });

        app.get('/flip', async (req, res) => {
            getModule('flip', req, res);
        });

        app.get('/flop', async (req, res) => {
            getModule('flop', req, res);
        });

        app.get('/blur', async (req, res) => {
            getModule('blur', req, res);
        });

        app.get('/brazzers', async (req, res) => {
            getModule('brazzers', req, res);
        });

        app.get('/pornhub', async (req, res) => {
            getModule('pornhub', req, res);
        });

        app.get('/cpiban', async (req, res) => {
            getModule('cpiban', req, res);
        });

        app.get('/wasted', async (req, res) => {
            getModule('wasted', req, res);
        });

        app.get('/logan', async (req, res) => {
            getModule('logan', req, res);
        });

        app.get('/hypercam', async (req, res) => {
            getModule('hypercam', req, res);
        });

        app.get('/iX', async (req, res) => {
            getModule('ix', req, res);
        });

        app.get('/gas', async (req, res) => {
            getModule('gas', req, res);
        });

        app.get('/etika', async (req, res) => {
            getModule('etika', req, res);
        });

        app.get('/trump', async (req, res) => {
            getModule('trump', req, res);
        });

        // Currently not used in Sensei.
        app.get('/slap', async (req, res) => {
            getModule('slap', req, res);
        });

        app.get('/graphic', async (req, res) => {
            getModule('graphic', req, res);
        });

        app.get('/wdt', async (req, res) => {
            getModule('wdt', req, res);
        });

        app.get('/perfect', async (req, res) => {
            getModule('perfect', req, res);
        });

        app.get('/damn', async (req, res) => {
            getModule('damn', req, res);
        });

        app.get('/14reasons', async (req, res) => {
            getModule('14reasons', req, res);
        });

        app.get('/fortnite', async (req, res) => {
            getModule('fortnite', req, res);
        });


        app.listen(this.port || 60, async () => {
            console.log(`[Server] Image API Server successfully started on port ${this.port || 60}!`);

            let files = await fs.readdirSync(`${__dirname}/modules`);
            let modules = [];
            await files.forEach(file => {
                if (file.endsWith('.js')) {
                    modules.push(file);
                }
            })
            console.log(`[Modules] Loaded a total of ${modules.length} modules.`);
        })

    }
}

module.exports = sensei_image_api;