var url = require("url");
var system = require("../../base/system");
var metaCtl = system.getObject("web.common.metaCtl");
var tmqueryCtl = system.getObject("web.trademark.tmqueryCtl");
var logClient = system.getObject("util.logClient");
module.exports = function (app) {
	app.get("/jdtm/getUser", async function (req, res) {
		try {
			var params = req.query;
			if (!params.state) {
				res.redirect("/#/jd/jdtrademark");
				return;
			}
			var getUserparams = {
				actionProcess: "jd",
				authUrl: "https://oauth2.jdcloud.com/userinfo",
				authToken: params.token_type + " " + params.access_token
			};
			var userItemResult = await tmqueryCtl.getUserInfo(getUserparams, req.query, req);
			if (userItemResult.status != 0) {
				res.redirect("/#/jd/jdtrademark");
				return;
			}
			if (!userItemResult.data.encryptChannelUserId || userItemResult.data.encryptChannelUserId == "undefined") {
				userItemResult = await tmqueryCtl.getUserIdEncryptStr(getUserparams, req.query, req);
				if (userItemResult.status != 0) {
					res.redirect("/#/jd/jdtrademark");
					return;
				}
			}
			var skipUrl = "/#/jd/" + params.state + "?channelUserId=" + encodeURIComponent(userItemResult.data.encryptChannelUserId);
			res.redirect(skipUrl);
		} catch (error) {
			logClient.error("jd", "req---->/jdtm/getUser---->error:" + error.stack);
		}
	});
	app.get("/jdtm/getUserTest", async function (req, res) {
		console.log(req.query, "/jdtm/getUser....................");
		var params = req.query;
		if (!params.state) {
			res.redirect("/#/jd/jdtrademark");
			return;
		}
		var getUserparams = {
			actionProcess: "jd",
			authUrl: "https://oauth2.jdcloud.com/userinfo",
			authToken: params.token_type + " " + params.access_token,
			tmpchannelUserId: params.tmpchannelUserId
		};
		var userItemResult = await tmqueryCtl.getUserInfoTest(getUserparams, req.query, req);
		if (userItemResult.status != 0) {
			res.redirect("/#/jd/jdtrademark");
			return;
		}
		var skipUrl = "/#/jd/" + params.state + "?channelUserId=" + encodeURIComponent(userItemResult.data.encryptChannelUserId);
		res.redirect(skipUrl);
	});
	app.get("/jd/orderNotify", async function (req, res) {
		// oplogCtl.info({
		// 	optitle: "########################################################.................orderNotify",
		// 	op: "/jd/orderNotify",
		// 	content: "req.query=" + JSON.stringify(req.query) + "，req.body=" + JSON.stringify(req.body),
		// 	clientIp: ""
		// });
		req.query.actionProcess = "jd";
		var result = await tmqueryCtl.pushPayOrder(req.query, req);
		// oplogCtl.info({
		// 	optitle: "############################################操作订单结果.................orderNotify",
		// 	op: "/jd/orderNotify",
		// 	content: "req.query=" + JSON.stringify(req.query) + "，req.body=" + JSON.stringify(req.body),
		// 	clientIp: ""
		// });
		if (result.status != 0) {
			res.end(JSON.stringify(result));
			return;
		}
		var params =
		{
			instanceId: req.query.orderBizId,
			appInfo: {
				authUrl: "http://oauth2.jdcloud.com/authorize?response_type=token&redirect_uri=http://tm.plus.jdcloud.com/jdtm/getUser&state=jdindentlist&client_id=9841572588670903"
			}
		};
		res.end(JSON.stringify(params));
		return;
	});
	app.get("/", async function (req, res) {
		try {
			var appinfo = await metaCtl.getAppInfo(req);
			if (!appinfo) {
				res.render("index", { appinfo: null, app: null });
			} else {
				res.render("index", appinfo);
			}
		} catch (error) {
			console.log(error.stack, "get/admin，error.............");
		}
	});
	app.get("/admin", async function (req, res) {
		try {
			var appinfo = await metaCtl.getAppInfo(req);
			res.render("admin", appinfo);
		} catch (error) {
			console.log(error.stack, "get/admin，error.............");
		}

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

			params.push(req.body);
			params.push(req.query);
			params.push(req);
			var p = null;
			var invokeObj = system.getObject("web." + classPath);
			if (invokeObj["doexec"]) {
				p = invokeObj["doexec"].apply(invokeObj, params);
			}
			p.then(r => {
				res.end(JSON.stringify(r));
			});
		} catch (error) {
			console.log(error.stack, "post/web/:gname/:qname/:method，error.............");
		}
	});
};
