var url = require("url");
var System = require("../../base/system");
const chnelapi = System.getObject("api.common.channels")
var channelCache = {}
module.exports = function (app) {
	app.get("/autologin", (req, res, next) => {
		//读取cookie,获取cookie值，
		//Authorization: 
		//Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImRjMmM1MzMwMjNhMTExZTlhOTU3NjliODBiNDI4Y2QxIn0.eyJpZCI6IjViZDk2MWJkZWQxN2E5MmI1MDFjZWQ0ZCIsIm5hbWUiOiLpnZLph4kxMSIsImVtcGxveWVlIjoiNWQ5ZWNlODc1Zjk3ZGQzMzEwNzI4ZjY4IiwicGhvbmUiOiI3TFlYZ29ySkdGM2hBbHdqQTZGelFnPT0iLCJvcmdpZCI6IjViY2ZlOTVjYjBhMDFmMDAwOTI4NGJjMyIsInBsYXRmb3JtIjoiZDVmYTU4Yjk3NGExMjBlZTYxMTMzM2MwMDNjOWZlYzAiLCJ1bmlvbmlkIjoib2RuWjR0OERoV1V0N0lXT09maEF0UTdqM1docyIsImp0aSI6Imo3R0hLRGFzNDEzQTQ2OGkiLCJleHAiOjE1OTYwODEzMTEsImlhdCI6MTU5NTQ3NjUxMX0.I196XbcCR3k7r1O8uwoI2HUhow0UhGHKzii5wOR39vLmuNOUatNY7ccQTCHijqysbSCePP43Iuglrl2gLQASf5uAr9uqTzhTc3O4Z5iF3oR2qR1vlBxrzTDF8xuhKHQ4tO6Hbsib0FdOYVeU8fe0g_GZiIcuYIv-f8VsiAGMF9TEES129CDwZacJfVViw4EXVnXSivAzQG4JgGSwUACvPT140bCZ1XPeHi0GXmgtEN77NjWfOGI5RYGC0Gq3CGevlSWERjb3mUscAIjoXsOxlWNwy7dc1u1A7uX1a6bUhLU1wtiQ6md4TnyeR5OdYeVqGlIe3hZGcH_E3QrQMq988Q

	})
	app.all("*", async function (req, res, next) {
		try {
			let channel = null
			let hostaddr = req.headers["x-forwarded-host"]
			if (!channelCache[hostaddr]) {
				channel = await chnelapi.getChannels(req.headers["x-forwarded-host"])
				channelCache[hostaddr] = channel
			} else {
				channel = channelCache[hostaddr]
			}
			if (!channel) {
				next()
			} else {
				let rtn = await chnelapi.channelHandle(channel, req.path + "/" + req.body.actionType, req.body)
				if (rtn != null) {
					res.end(JSON.stringify(rtn));
				} else {
					res.end(JSON.stringify({ status: -1, message: "fail" }));
				}
			}
		} catch (e) {
			console.log(e)
			res.end(JSON.stringify({ status: -1, message: e }));
		}
	})
	app.get('/api/:gname/:qname/:method', function (req, res) {
		// var classPath = req.params["qname"];
		var methodName = req.params["method"];
		var gname = req.params["gname"];
		classPath = gname + "." + classPath;
		var tClientIp = System.get_client_ip(req);
		req.clientIp = tClientIp;
		req.uagent = req.headers["user-agent"];
		// req.classname=classPath;

		var params = [];
		params.push(gname);
		params.push(methodName);
		params.push(req.body);
		params.push(req.query);
		params.push(req);
		var p = null;
		var invokeObj = System.getObject("api." + classPath);
		if (invokeObj["doexec"]) {
			p = invokeObj["doexec"].apply(invokeObj, params);
		}
		p.then(r => {
			res.end(JSON.stringify(r));
		});
	});
	app.post('/api/:gname/:qname/:method', function (req, res) {
		var classPath = req.params["qname"];
		var methodName = req.params["method"];
		var gname = req.params["gname"];
		var params = [];
		classPath = gname + "." + classPath;
		console.log("====================");
		console.log(classPath);
		var tClientIp = System.get_client_ip(req);
		req.clientIp = tClientIp;
		req.uagent = req.headers["user-agent"];
		// req.classname=classPath;

		params.push(gname);
		params.push(methodName);
		params.push(req.body);
		params.push(req.query);
		params.push(req);
		var p = null;
		var invokeObj = System.getObject("api." + classPath);
		if (invokeObj["doexec"]) {
			p = invokeObj["doexec"].apply(invokeObj, params);
		}
		p.then(r => {
			res.end(JSON.stringify(r));
		});
	});
};
