var url = require("url");
var System = require("../../base/system");
const chnelapi = System.getObject("api.common.channels")
const userS = System.getObject("service.auth.userSve")
const settings = require("../settings.js")
let channelCache = {};
module.exports = function (app) {
	app.get("/autologin", async function (req, res, next) {
		console.log("/dddddddddddddddddddddddddddddddddddddddddd")
		let appkey = req.query.appkey
		let cooktoken = req.cookies["token"]
		let tv = cooktoken.split(' ')[1]
		let sourceUser = System.verify(tv, settings.publickey())
		let openid = sourceUser.employee
		let userName = sourceUser.name + "~" + openid
		let nickName = userName
		let unid = sourceUser.unionid

		//先按照openid去查看是否已经存在
		//let uf = await userS.findOne({ where: { openid: openid })
		// if (!uf) {
		// 	//就去注册一个用户

		// 	//按照公司companykey查询出要注册公司的所在公司，取公司id

		// 	//构造注册用户对象,需要设置默认角色，默认角色需要按照rolecode查询出角色

		// 	//调用pmregister
		// 	let uname = xxxx
		// }
		res.end("hello")
		//调用loginApp（appkey,uname）---登录APP
		// { code: xxx, homePage: xxxx }=loginApp（appkey, uname）
		// res.redirect(homePage ? code = xxx)

	})
	app.all("*", async function (req, res, next) {
		try {
			let channel;
			let sourceHost = req.headers["x-forwarded-host"];
			if (sourceHost in channelCache) {
				channel = channelCache[sourceHost];
			} else {
				channel = await chnelapi.getChannels(sourceHost);
				channelCache[sourceHost] = channel;
			}
			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));
		});
	});
};
