var shares = null;
var Intent = null,
	File = null,
	Uri = null,
	main = null;
// H5 plus事件处理
function plusReady() {
	updateSerivces();
	if(plus.os.name == "Android") {
		main = plus.android.runtimeMainActivity();
		Intent = plus.android.importClass("android.content.Intent");
		File = plus.android.importClass("java.io.File");
		Uri = plus.android.importClass("android.net.Uri");
	}
}
if(window.plus) {
	plusReady();
} else {
	document.addEventListener("plusready", plusReady, false);
}

/**
 * 
 * 更新分享服务
 */
function updateSerivces() {
	plus.share.getServices(function(s) {
		shares = {};
		for(var i in s) {
			var t = s[i];
			shares[t.id] = t;
		}
	}, function(e) {
		plus.nativeUI.toast("获取分享服务列表失败:" + e.message);
	});
}

/**
 * 分享操作
 * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
 * @param {Boolean} bh 是否分享链接
 */
function shareAction(sb, bh) {
	if(!sb || !sb.s) {
		plus.nativeUI.toast("无效的分享服务!");
		return;
	}

	var msg = {
		content: sharehrefDes.value,
		extra: {
			scene: sb.x
		},
		type:"web"
	};
	if(bh) {
		msg.href = sharehref.value;
		if(sharehrefTitle && sharehrefTitle.value != "") {
			msg.title = sharehrefTitle.value;
		}
		if(sharehrefDes && sharehrefDes.value != "") {
			msg.content = sharehrefDes.value;
		}
		msg.thumbs = ["_www/logo.png"];
		msg.pictures = ["_www/logo.png"];
	} else {
		if(pic && pic.realUrl) {
			msg.pictures = [pic.realUrl];
		}
	}
	// 发送分享
	if(sb.s.authenticated) {
		//		plus.nativeUI.toast("---已授权---");
		shareMessage(msg, sb.s);
	} else {
//		plus.nativeUI.toast("---未授权---");
		sb.s.authorize(function() {
			shareMessage(msg, sb.s);
		}, function(e) {
			plus.nativeUI.toast("认证授权失败:" + e.code + " - " + e.message);
//			alert("认证授权失败:"+e.code+" - "+e.message );
		});
	}
}
/**
 * 发送分享消息
 * @param {JSON} msg
 * @param {plus.share.ShareService} s
 */
function shareMessage(msg, s) {

//		plus.nativeUI.toast(JSON.stringify(msg));
	s.send(msg, function() {
		plus.nativeUI.toast("分享到\"" + s.description + "\"成功! ");

	}, function(e) {
		plus.nativeUI.toast("分享到\"" + s.description + "\"失败 ");
//		alert( "分享到\""+s.description+"\"失败: "+JSON.stringify(e) );
	});
}
// 分析链接
function shareHref() {
	var shareBts = [];
	// 更新分享列表
	var ss = shares['weixin'];
	ss && ss.nativeClient && (shareBts.push({
			title: '微信朋友圈',
			s: ss,
			x: 'WXSceneTimeline'
		}),
		shareBts.push({
			title: '微信好友',
			s: ss,
			x: 'WXSceneSession'
		}));

	// 弹出分享列表
	shareBts.length > 0 ? plus.nativeUI.actionSheet({
		title: '分享注册链接',
		cancel: '取消',
		buttons: shareBts
	}, function(e) {
		(e.index > 0) && shareAction(shareBts[e.index - 1], true);
	}) : plus.nativeUI.plus.nativeUI.toast('当前环境无法支持分享链接操作!');
}

mui.plusReady(function() {
	var token = localStorage.getItem('token');
	mui.ajax(hyhUrl('app/Users/get_share'), { 
		
		headers: { 
			"HYH-Token": token
		},
		dataType: 'json', //服务器返回json格式数据  
		type: 'post', //HTTP请求类型  
		timeout: 10000, //超时时间设置为10秒;  
		success: function(data) {           //服务器返回响应,根据响应结果,分析是否登录成功;  
			//					console.log(data.data.goodsFavoritesNum)
			//					console.log(data.data.Rows)
			var data = toJson(data);
			if(data.status == 1) {
				//读取成功后
				var qrcode = new QRCode(document.getElementById("qrcode"), {
					width: 96, //设置宽高
					height: 96
				});
				qrcode.makeCode(data.data.url);
//				console.log(hyhImgUrl(data.data.bg_share))
				$('.bg').attr('src',hyhImgUrl(data.data.bg_share))
//				$('.bg').attr('src','../img/fenx111.png')
				$('#sharehref').val(data.data.url)
				$('#sharehrefTitle').val(data.data.title)
				$('#sharehrefDes').val(data.data.desc)
			} else {
				//console.log(data.status)
			}

		},
		error: function(xhr, type, errorThrown) {           //异常处理;  
			//					plus.nativeUI.toast(type);    
		}  
	}); 
	$('.header').on('tap','.share',function(){
		shareHref();
	})
});