package com.lutao.cloud.iot.mqtt.core.server.http.handler;

import org.tio.http.common.Method;
import org.tio.http.common.RequestLine;

import java.util.*;

/**
 * mqtt http api 路由
 *

 */
public final class MqttHttpRoutes {
	private static final LinkedList<HttpFilter> FILTERS = new LinkedList<>();
	private static final Map<String, HandlerInfo> ROUTS = new HashMap<>();

	/**
	 * 注册路由
	 *
	 * @param filter HttpFilter
	 */
	public static void addFilter(HttpFilter filter) {
		FILTERS.add(filter);
	}

	/**
	 * 注册路由
	 *
	 * @param index  index
	 * @param filter HttpFilter
	 */
	public static void addFilter(int index, HttpFilter filter) {
		FILTERS.add(index, filter);
	}

	/**
	 * 读取所以的过滤器
	 *
	 * @return 过滤器集合
	 */
	public static List<HttpFilter> getFilters() {
		return Collections.unmodifiableList(FILTERS);
	}

	/**
	 * 注册路由
	 *
	 * @param method  请求方法
	 * @param path    路径
	 * @param handler HttpHandler
	 */
	public static void register(Method method, String path, HttpHandler handler) {
		ROUTS.put(path, new HandlerInfo(method, handler));
	}

	/**
	 * 读取路由
	 *
	 * @param path 路径
	 * @return HandlerInfo
	 */
	public static HandlerInfo getHandler(String path) {
		return ROUTS.get(path);
	}

	/**
	 * 读取路由
	 *
	 * @param requestLine RequestLine
	 * @return HttpHandler
	 */
	public static HandlerInfo getHandler(RequestLine requestLine) {
		return getHandler(requestLine.getPath());
	}

}
