var Wx = require("./wx.event");
const system = require("../system");
var dicCache = {};
//创知厚的我想我要的配置
wxconfig = {
	AppID: "wx4c91e81bbb6039cd",
	Secret: "12048e66dba64f2581e02b306680b232",
	Token: "bosstoken",
	AccessTokenUrl: "https://api.weixin.qq.com/cgi-bin/token",
};

//智薪云的配置
wxconfig2 = {
	AppID: "wxdc08c441c9fdb7a7",
	Secret: "379e582e28645585a7def9c1c88de550",
	Token: "bosstoken",
	AccessTokenUrl: "https://api.weixin.qq.com/cgi-bin/token",
};
wxconfigDic = {
	"wx4c91e81bbb6039cd": wxconfig,
	"wxdc08c441c9fdb7a7": wxconfig2,
}
var getWeiXin = function (url) {
	var weixin = null;
	if (!dicCache[url]) {
		weixin = new Wx(url);
		dicCache[url] = weixin;
	} else {
		weixin = dicCache[url];
	}
	weixin.token = wxconfig.Token;
	weixin.wxconfig = wxconfig;
	weixin.getAccessConfig = function (appkey) {
		var cfg = wxconfigDic[appkey];
		return {
			grant_type: "client_credential",
			appid: cfg.AppID,
			secret: cfg.Secret,
		};
	}

	// 监听文本消息
	weixin.textMsg(function (msg) {
		console.log("textMsg received");
		console.log(JSON.stringify(msg));
		var resMsg = {};
		switch (msg.content) {
			case "文本":
				// 返回文本消息
				resMsg = {
					fromUserName: msg.toUserName,
					toUserName: msg.fromUserName,
					msgType: "text",
					content: "这是文本回复",
					funcFlag: 0
				};
				break;

			case "音乐":
				// 返回音乐消息
				resMsg = {
					fromUserName: msg.toUserName,
					toUserName: msg.fromUserName,
					msgType: "music",
					title: "音乐标题",
					description: "音乐描述",
					musicUrl: "音乐url",
					HQMusicUrl: "高质量音乐url",
					funcFlag: 0
				};
				break;

			case "图文":

				var articles = [];
				articles[0] = {
					title: "PHP依赖管理工具Composer入门",
					description: "PHP依赖管理工具Composer入门",
					picUrl: "http://weizhifeng.net/images/tech/composer.png",
					url: "http://weizhifeng.net/manage-php-dependency-with-composer.html"
				};

				articles[1] = {
					title: "八月西湖",
					description: "八月西湖",
					picUrl: "http://weizhifeng.net/images/poem/bayuexihu.jpg",
					url: "http://weizhifeng.net/bayuexihu.html"
				};

				articles[2] = {
					title: "「翻译」Redis协议",
					description: "「翻译」Redis协议",
					picUrl: "http://weizhifeng.net/images/tech/redis.png",
					url: "http://weizhifeng.net/redis-protocol.html"
				};

				// 返回图文消息
				resMsg = {
					fromUserName: msg.toUserName,
					toUserName: msg.fromUserName,
					msgType: "news",
					articles: articles,
					funcFlag: 0
				}
		}

		weixin.sendMsg(resMsg);
	});

	// 监听图片消息
	weixin.imageMsg(function (msg) {
		console.log("imageMsg received");
		console.log(JSON.stringify(msg));
	});

	// 监听位置消息
	weixin.locationMsg(function (msg) {
		console.log("locationMsg received");
		console.log(JSON.stringify(msg));
	});
	// 监听链接消息
	weixin.urlMsg(function (msg) {
		console.log("urlMsg received");
		console.log(JSON.stringify(msg));
	});
	// 监听事件消息
	weixin.eventMsg(function (msg) {
		if (msg.event == "subscribe" || msg.event == "SCAN") {
			var wxservice = system.getObject("service.wxSve");
			//to do msg.toUserName--按照微信号去取得应用，按照应用获取OAUTH
			// wxservice.checkAndLogin(msg.fromUserName);
			if (weixin.path == "ecwx") {
				var urlstr = "https://ec.gongsibao.com/h5";
				if (msg.eventKey && msg.eventKey != "") {
					if (msg.eventKey.indexOf("_") >= 0) {
						var strid = msg.eventKey.split('_')[1];
						urlstr = urlstr + "?ecid=" + strid;
					} else {
						var strid = msg.eventKey;
						urlstr = urlstr + "?ecid=" + strid;
					}

				}
				var articles = [];
				articles[0] = {
					title: "欢迎来到智薪云签约平台",
					description: "点击本图文消息，开始签约。",
					picUrl: "https://gsb-zc.oss-cn-beijing.aliyuncs.com//zc_476111544670096808201813111368084242271.jpg",
					url: urlstr,
				};
				var resMsg = {
					fromUserName: msg.toUserName,
					toUserName: msg.fromUserName,
					msgType: "news",
					articles: articles,
					funcFlag: 0
				};
				weixin.sendMsg(resMsg);
			} else {
				//是否启用派单
				var dispatchEnabled = true;
				// 通知信息
				var articles = [];
				// 参数
				var eventKey = msg.eventKey;
				var userId = 0;
				if (eventKey && eventKey.indexOf("_") > -1) {
					// 标眼查通知openId保存逻辑
					userId = Number(eventKey.split("_")[1] || 0);
					if (userId && userId > 0) {
						dispatchEnabled = false;
						// 标眼查动态通知
						articles[0] = {
							title: "欢迎使用标眼监测商标监控工具",
							description: "实时提醒商标动态，及时处理商标业务",
							picUrl: "",
							//picUrl: "https://search.gongsibao.com/imgs/biaoyan-logo-mini.png",
							url: ""
						};
					}
				}
				else {
					if (eventKey) {
						userId = Number(eventKey);
					}
				}
				if (dispatchEnabled) {
					// 派单通知
					articles[0] = {
						title: "欢迎来到创知厚德智能派单平台",
						description: "点击图文消息，开始接单",
						picUrl: "https://gsb-zc.oss-cn-beijing.aliyuncs.com//zc_305111544784291353201814184451353distributeneed.png",
						url: "https://boss.gongsibao.com/distributeneed",
					};
				}
				if (userId && userId > 0) {
					// wxservice.更新通知id
					wxservice.updateNotifyOpenId(userId, msg.fromUserName);
				}
				var resMsg = {
					fromUserName: msg.toUserName,
					toUserName: msg.fromUserName,
					msgType: "news",
					articles: articles,
					funcFlag: 0
				};
				weixin.sendMsg(resMsg);
			}
		} else {
			//发送空串回微信服务器
			weixin.res.send("");
		}
	});
	return weixin;
}
module.exports = getWeiXin;