import * as dotenv from 'dotenv';
import { GenericController, GenericService, IParserResponse, SecureRouter } from '@3kles/3kles-corebe';
import { AuthService, IONConnector, IIonRoute, IonApp, IonBroker } from '../src';
import { MessageBroker } from '@3kles/3kles-amqpbroker';

dotenv.config();

const authService = new AuthService(null);

const iONConnector = new IONConnector(authService, {
    protocol: 'https',
    context: 'IONSERVICES',
    middleware: '/process/application'
});

class ParserCustom implements IParserResponse {
    parseResponse(data: any) {
        const response = Buffer.concat(data).toString();
        try {
            return JSON.parse(response);
        } catch (_) {
            return response;
        }
    }
}

iONConnector.setResponseParser(new ParserCustom());
iONConnector.setErrorParser(new ParserCustom());

const endpoints: IIonRoute[] = [
    {
        routes: {
            ping: {
                method: 'GET',
                path: '/ping',
                option: {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: '/ping'
                },
            },
        }
    },
    {
        routingKey: 'pulse.alert',
        middleware: 'pulse/alert',
        routes: {
            create: {
                method: 'POST',
                path: '/create',
                option: {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/alert/create'
                },
            },
            cancel: {
                method: 'POST',
                path: '/cancel',
                option: {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/alert/cancel'
                },
            },
            redistribute: {
                method: 'POST',
                path: '/redistribute',
                option: {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/alert/redistribute'
                },
            }
        }
    },
    {
        routingKey: 'pulse.notification',
        middleware: 'pulse/notification',
        routes: {
            create: {
                method: 'POST',
                path: '/create',
                option: {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/notification/create'
                },
            },
            cancel: {
                method: 'POST',
                path: '/cancel',
                option: {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/notification/cancel'
                },
            }
        }
    },
    {
        routingKey: 'pulse.workflow',
        middleware: 'pulse/workflow',
        routes: {
            cancel: {
                method: 'POST',
                path: '/cancel',
                option: {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/workflow/cancel'
                },
            },
            start: {
                method: 'POST',
                path: '/start',
                option: {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/workflow/start'
                },
            },
            interface: {
                method: 'GET',
                path: '/start',
                option: {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    path: 'v1/pulse/workflow/interface'
                },
            },
        }
    },
];

(async () => {
    const broker = await MessageBroker.initInstance('0', { prefetch: 50 });
    const app = new IonApp();

    endpoints.forEach(endpoint => {
        app.addRoute(new SecureRouter(authService,
            new GenericController(new GenericService(iONConnector, endpoint.routes))).router,
            endpoint.middleware || undefined);

        if (endpoint.routingKey) {
            app.addIonBroker(new IonBroker(broker, iONConnector, endpoint));
        }

    });
    const port = process.env.PORT ? +process.env.PORT : 80;
    app.startApp(port);
    app.startBroker();
})();


