import store from '@/store'

var _phone=null;
var server = 'call.yjf.runtzh.com'; // OPEN的IP地址和端口 
function webphoneHandle(type, data, options) {
	console.log("sip:"+type, JSON.stringify(data), options);
	if(type=="outgoing.connecting"){     //正在尝试呼出
		 
	}
	else if(type=="outgoing.progress"){  //开始振铃了
		 
		
	}
	else if(type=="outgoing.accecpted"){ //被接听
		store.dispatch('CallStatus',{
			title: "正在通话...",
		})
	}
	else if(type=="outgoing.ended"){   //呼出正常结束
		store.dispatch('CallOff')

	}else if(type=="outgoing.failed"){   //呼出失败
		store.dispatch('CallOff')
	}
	
}

export function callNumber(customerId,contactsId,orderId){
	
	const callUser = store.getters && store.getters.callUser
	const callRealm = store.getters && store.getters.callRealm
	const callHa1 = store.getters && store.getters.callHa1
    JsSIP.C.SESSION_EXPIRES=120,JsSIP.C.MIN_SESSION_EXPIRES=360;

	console.log(callUser)
	console.log(callRealm)
	console.log(callHa1)
	if(_phone==null){
		
		_phone = new WebPhoneSDK({
				lib: JsSIP,
				debug: true,
				debugLib: true,
				user: callUser,
				ha1: callHa1,
				realm: callRealm,
				server: server,
				audioElement: document.getElementById('remoteAudio'),
				httpsConfirmFrame: 'httpsConfirmFrame',
				pcConfig:{"iceServers":[{"urls":["stun:211.159.161.254"]}]},
				events: {
					'ua.connecting': (type, data) => webphoneHandle(type, data),
					'ua.connected': (type, data) => webphoneHandle(type, data), //WEBPHONE链接OK
					'ua.disconnected': (type, data) => webphoneHandle(type, data), //WEBPHONE链接断开
					'reg.registered': (type, data) => webphoneHandle(type, data),  //WEBPHONE 分机注册
					'reg.unregistered': (type, data) => webphoneHandle(type, data), //WEBPHONE 分机取消注册
					'reg.failed': (type, data) => webphoneHandle(type, data),  //WEBPHONE 注册失败
					'incoming.notify': (type, data, options) => webphoneHandle(type, data, options),  //有电话进来了
					'incoming.failed': (type, data, options) => webphoneHandle(type, data, options),//WEBPHONE 有电话进来，然后处理失败了（如，我们拒绝了，或者客户侧直接挂断了）
					'incoming.ended': (type, options) => webphoneHandle(type, options),// WEBPHONE 有电话进来了,呼叫正常结束了
					'incoming.accecpted': (type, options) => webphoneHandle(type, options),//有电话进来了,呼叫接听了额
					'outgoing.connecting': (type, options) => webphoneHandle(type, options),//WEBPHONE正在尝试呼出
					'outgoing.progress': (type, options) => webphoneHandle(type, options),//WEBPHONE开始振铃了
					'outgoing.failed': (type, data, options) => webphoneHandle(type, data, options),//WEBPHONE呼出失败了，如被叫拒绝了，线路异常呼出失败了
					'outgoing.ended': (type, options) => webphoneHandle(type, options),  //呼出正常结束
					'outgoing.accecpted': (type, data, options) => webphoneHandle(type, data, options),  //呼出被叫接听
					'call.hold': (type, data) => webphoneHandle(type, data), //呼叫保持
					'call.unhold': (type, data) => webphoneHandle(type, data),  //呼叫解除保持
				}
			})
			_phone.start({});
	}
	var data={customerId:customerId,contactsId:contactsId,orderId:orderId};
	this.callMobile(data).then(response => {
        if(response.code==200){
			_phone.call(response.mobile,{userdata:response.logs.sessionId});
			this.$store.dispatch('Call', {
				mobile: response.displayMobile,
				sessionId: response.logs.sessionId
			})
        }
        else{
			that.$notify.error({
				title: response.msg,
				showClose: false
			});
        }
    });
  }
  export function callOff(){
	if(_phone!=null){
		_phone.reject();
	}
  }
