(function() {
	
	window.callTest = function(params){
		console.log(JSON.stringify(params));
		console.log(JSON.stringify(window.$join));
		console.log(JSON.stringify(window.$registerParams));
	}
	
	// 注册签入
	window.registerFun = function(params){
		const { pwd , Agent } = params;
		const data = {
		   register: { //重新登录参数
		    Agent: Agent,
		    pwd: pwd,
		   },
		   ip:"8.134.50.217",
		   signIn: "signIn"
		}
		window.postMessage(data, "*");
	}
	
	// 拨打电话
	window.callInFun = function(params){
		const { phone } = params;
		const data = {
			phone : phone,
		};
		window.postMessage(data, "*");
	}
	
	// 挂机
	window.handUpFun = function(){
		window.$join = null;
		const data = {
		    hangUp: "hangUp"
		}
		window.postMessage(data, "*");
	}
	
	window.postSignIn = function(){
		const data = {
		    signIn: "signIn"
		}
		window.postMessage(data, "*");
	}
	
	window.postSignOut = function(){
		const data = {
			signOut: "signOut"
		}
		window.postMessage(data,"*");
	}
	
	window.postSignState = function(){
		const data = {
			signInState: "signInState"
		}
		window.postMessage(data,"*");
	}
	
	// 外呼来电接听
	window.incomeCallAnswer = function(){
		const data = {
			answerCall: "answerCall"
		}
		window.postMessage(data,"*");
	}
	
	// 拒绝来电
	window.rejectCall = function(){
		const data = {
			rejectCall: "rejectCall"
		}
		window.postMessage(data,"*");
	}
	
	document.addEventListener('UniAppJSBridgeReady', function() {
		
		window.addEventListener('message', function(e) {
			if (e.data.type == "passDataBack") {
				uni.postMessage({
					data: {
						data : e.data.data.call_id,
						action : "passDataBack",
						type : e.data.data.callType,
						join : window.$join
					}
				});
			}
			if (e.data.type == "message") {
				if (e.data.data.code == 500) {
					uni.postMessage({
						data: {
							data : e.data.data.msg,
							action: "message",
							type : "error"
						}
					});
				} else if (e.data.data.code == 200) {
					uni.postMessage({
						data: {
							data : e.data.data.msg,
							action: "message",
							type : "success"
						}
					});
				} else if(e.data.data.code == 1001){
					uni.postMessage({
						data: {
							data : e.data.data.msg.substring(6,e.data.data.msg.indexOf("@")),
							action: "incoming"
						}
					});
				}
			}
			if(e.data.type == "singInState"){
				uni.postMessage({
					data: {
						data : e.data.data.signState,
						action : "singInState",
						type : "status"
					}
				});
			}
		});
		
		setTimeout(()=>{
			registerFun($registerParams);
		},3000);
		
		
		setInterval(()=>{
			postSignState();
		},5000)
	})
}())
