import { ExpressJsonMiddleware, ExpressUrlencodedMiddleware, InternalServerErrorHandler, NotFoundErrorHandler, PoweredByHeaderMiddleware, SessionMiddleware, SwaggerUiServeMiddleware, SwaggerUiSetupMiddleware } from '../../middlewares';
import Registry from './Registry';

type SystemRegistryConfig = {
	json: boolean,
	urlencoded: boolean,
	sessions: boolean,
	swagger: boolean
};

class SystemRegistry extends Registry {
	private readonly json: boolean;
	private readonly urlencoded: boolean;
	private readonly sessions: boolean;
	private readonly swagger: boolean;

	public constructor(config: SystemRegistryConfig) {
		super();

		this.json = config.json;
		this.urlencoded = config.urlencoded;
		this.sessions = config.sessions;
		this.swagger = config.swagger;

		return this;
	}

	protected async registerRoutes(): Promise<Registry> {
		return this;
	}

	protected async registerMiddlewares(): Promise<Registry> {
		const json = this.json, urlencoded = this.urlencoded, swagger = this.swagger, sessions = this.sessions, server = this.getServer()!;
		
		json && this.registerHttpHandler(new ExpressJsonMiddleware(server));
		urlencoded && this.registerHttpHandler(new ExpressUrlencodedMiddleware(server));
		sessions && this.registerHttpHandler(new SessionMiddleware(server));
		swagger && this.registerHttpHandler(new SwaggerUiServeMiddleware(server));
		swagger && this.registerHttpHandler(new SwaggerUiSetupMiddleware(server));
		this.registerHttpHandler(new InternalServerErrorHandler(server));
		this.registerHttpHandler(new NotFoundErrorHandler(server));
		this.registerHttpHandler(new PoweredByHeaderMiddleware(server));

		return this;
	}	
}

export default SystemRegistry;