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: "wxf616c0a459d66081",
	Secret: "35275146272c7c862ff4b1ba11282d02",
	Token: "bosstoken",
	AccessTokenUrl: "https://api.weixin.qq.com/cgi-bin/token",
};
wxconfigDic = {
	"wx4c91e81bbb6039cd": wxconfig,
	"wxf616c0a459d66081": 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://bpohhr.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 articles = [];
				articles[0] = {
					title: "欢迎来到创知厚德智能派单平台.",
					description: "点击图文消息，开始接单。",
					picUrl: "https://gsb-zc.oss-cn-beijing.aliyuncs.com//zc_305111544784291353201814184451353distributeneed.png",
					url: "https://boss.gongsibao.com/distributeneed",
				};
				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;