import { dutyOff } from '@/api/public/wex';
/**
 * @description 呼叫中心对接接口
 */
import { ElMessage } from 'element-plus';

export const ola: any = {
	version: '2.0.1',
	ws: undefined,
	uuid: '73836387-0000-0000-0000-0000-0000000000',
	connected: false,
	socket_connected: false,
	onConnect: undefined,
	onMessage: undefined,
	onClose: undefined,
	_connectSuccess: undefined,
	_connectError: undefined,
	_username: undefined,
	_password: undefined,
	_extn: undefined,

	connect: function (url: string | URL, username: any, password: any, success?: any, error?: any) {
		if (!url) url = 'ws://' + document.location.hostname + ':' + document.location.port + '/ola_socket';
		if ('WebSocket' in window) {
			// browser supports websockets
			this.ws = new WebSocket(url);
			if (this.ws) {
				this.ws.onclose = ola.onClose;
				this.ws.onopen = ola._onOpen;
				this.ws.onmessage = ola._onMessage;
				this._connectSuccess = success;
				this._connectError = error;
				this._username = username;
				this._password = password;
			}
		} else {
			// browser does not support websockets
			ElMessage.error('您的浏览器不支持websocket!');
			if ('console' in window) {
				console.log("you don't have websocket");
			}
			return false;
		}
		return this;
	},

	close: function () {
		ola.ws.close();
	},

	_onOpen: function () {
		ola.auth(ola._username, ola._password);
	},

	_onMessage: function (evt: any) {
		//获取消息
		const msg = JSON.parse(evt.data);
		if (msg.event_type == 'command/reply' && msg.code !== 200) {
			//授权失败 调用签出接口
			ElMessage.error(msg.text);
			dutyOff();
		}
		if (msg.event_name == 'command/reply' && msg.code == 200) {
			ola.ws.onmessage = this.onMessage;
		}
		ola.ws.onmessage = ola.onMessage;
		ola.onConnect();
	},

	auth: function (username: any, password: any) {
		return ola.send({ cmd: 'auth', args: { username, password, accept: 'application/json' } });
	},

	get_agent_state: function (extn: any) {
		return this.send({ action: 'api', cmd: 'get_agent_state', args: { extn } });
	},

	get_trunk_state: function () {
		return this.send({ action: 'api', cmd: 'get_trunk_state' });
	},

	login: function (queue: any, extn: any, params: any) {
		const args = { queue, extn };
		this.merge(args, params);
		this._extn = extn;
		return ola.send({ action: 'api', cmd: 'login', args: args });
	},

	logout: function (extn?: string) {
		const extns = extn || this._extn;
		return this.send({ action: 'api', cmd: 'logout', args: { extn: extns } });
	},

	collect_dtmf: function (extn: any, soundfile: any) {
		return this.send({ action: 'api', cmd: 'collect_dtmf', args: { extn, soundfile } });
	},
	collect_judge: function (extn: any) {
		return this.send({ action: 'api', cmd: 'collect_judge', args: { extn: extn } });
	},

	ping: function () {
		return ola.send({ cmd: 'ping' });
	},

	subscribe: function (k: any) {
		return ola.send({ cmd: 'subscribe', args: { key: k } });
	},

	unsubscribe: function (k: any) {
		return ola.send({ cmd: 'unsubscribe', args: { key: k } });
	},

	go_ready: function () {
		return this.send({ action: 'api', cmd: 'go_ready', args: { extn: this._extn } });
	},

	go_ready2: function (extn: any) {
		return this.send({ action: 'api', cmd: 'go_ready', args: { extn: extn } });
	},

	go_break: function (reason: any) {
		return this.send({ action: 'api', cmd: 'go_break', args: { extn: this._extn, reason: reason } });
	},

	go_break2: function (extn: any) {
		return this.send({ action: 'api', cmd: 'go_break', args: { extn: extn, reason: '' } });
	},

	toggle_ready: function () {
		return this.send({ action: 'api', cmd: 'toggle_ready', args: { extn: this._extn } });
	},

	answer: function () {
		return this.send({ action: 'api', cmd: 'answer', args: { extn: this._extn } });
	},

	hangup: function () {
		return this.send({ action: 'api', cmd: 'hangup_other', args: { extn: this._extn } });
	},

	dial: function (dst: any, otherStr?: any, gateway?: any) {
		return this.send({ action: 'api', cmd: 'dial', args: { extn: this._extn, dest: dst, gateway: gateway, otherStr: otherStr } });
	},

	transfer: function (dst: any, src?: any) {
		let extn = src;

		if (extn == null || typeof extn == 'undefined') {
			extn = this._extn;
		}

		return this.send({ action: 'api', cmd: 'transfer', args: { extn: extn, dest: dst } });
	},

	transfer_uuid: function (uuid: any, dst: any) {
		return this.send({ action: 'api', cmd: 'transfer', args: { channel_uuid: uuid, dest: dst } });
	},

	monitor: function (dst: any, src: any) {
		let extn = src;
		if (extn == null || typeof extn == 'undefined') {
			extn = this._extn;
		}

		return this.send({ action: 'api', cmd: 'monitor', args: { extn: extn, dest: dst } });
	},

	monitor_uuid: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'monitor', args: { extn: this._extn, channel_uuid: uuid } });
	},

	intercept: function (dst: any, src: any, gateway: any) {
		let extn = src;

		if (extn == null || typeof extn == 'undefined') {
			extn = this._extn;
		}

		return this.send({ action: 'api', cmd: 'intercept', args: { extn: extn, dest: dst } });
	},

	intercept_uuid: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'intercept', args: { extn: this._extn, channel_uuid: uuid } });
	},

	three_way: function (dst: any, src: any, gateway?: any) {
		let extn = src;
		if (extn == null || typeof extn == 'undefined') {
			extn = this._extn;
		}
		return this.send({ action: 'api', cmd: 'monitor', args: { extn: extn, dest: dst, three_way: 'true', goip_gateway: gateway } });
	},
	/* hangup the thrid party */
	exit_three_way: function (ext: any) {
		return this.send({ action: 'api', cmd: 'unmonitor', args: { extn: ext, three_way: 'true' } });
	},
	three_way_uuid: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'monitor', args: { extn: this._extn, channel_uuid: uuid, three_way: 'true' } });
	},

	unmonitor: function (ext: any) {
		return this.send({ action: 'api', cmd: 'unmonitor', args: { extn: ext } });
	},

	whisper: function (w: any) {
		// who = "agent" / "caller" / "both" / "none"
		return this.send({ action: 'api', cmd: 'whisper', args: { extn: this._extn, who: w } });
	},

	consult: function (dst: any) {
		return this.send({ action: 'api', cmd: 'consult', args: { extn: this._extn, dest: dst } });
	},
	unconsult: function (dst: any) {
		return this.send({ action: 'api', cmd: 'unconsult', args: { extn: this._extn, dest: dst } });
	},
	consult_to_three_way: function (dst: any) {
		return this.send({ action: 'api', cmd: 'consult_to_three_way', args: { extn: this._extn, dest: dst } });
	},

	hold: function () {
		return this.send({ action: 'api', cmd: 'hold', args: { extn: this._extn } });
	},

	unhold: function () {
		return this.send({ action: 'api', cmd: 'unhold', args: { extn: this._extn } });
	},

	toggle_hold: function () {
		return this.send({ action: 'api', cmd: 'toggle_hold', args: { extn: this._extn } });
	},

	take_call: function (channel_uuid: any) {
		return this.send({ action: 'api', cmd: 'take_call', args: { extn: this._extn, channel_uuid: channel_uuid } });
	},
	next_queue: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'next_queue', args: { extn: this._extn, channel_uuid: uuid } });
	},

	conference: function (dst: any) {
		return this.send({ action: 'api', cmd: 'conference', args: { extn: this._extn, dest: dst } });
	},

	conference_uuid: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'conference', args: { extn: this._extn, channel_uuid: uuid } });
	},

	broadcast: function (numbers: any, mute: any) {
		return this.send({ action: 'api', cmd: 'broadcast', args: { extn: this._extn, numbers, mute } });
	},

	/* send chat to a queue or an agent
      to = queue             send to queue
      to = queue.agent       send to agent
    */
	chat: function (to: any, message: any, content_type: any) {
		return this.send({ action: 'api', cmd: 'chat', args: { to: to, message: message, content_type: content_type } });
	},

	message: function (from: any, to: any, message: any, content_type: any) {
		return this.send({ action: 'api', cmd: 'message', args: { from: from, to: to, message: message, content_type: content_type } });
	},

	alarm: function (queue: any, state: any) {
		return this.send({ action: 'api', cmd: 'alarm', args: { queue, state } });
	},

	/* dispatching apis */

	dlogin: function (ext: any) {
		return this.send({ action: 'api', cmd: 'dlogin', args: { extn: ext } });
	},

	dlogout: function (ext: any) {
		return this.send({ action: 'api', cmd: 'dlogout', args: { extn: ext } });
	},

	inject: function (ext: any, uuid: any) {
		return this.send({ action: 'api', cmd: 'inject', args: { extn: ext, channel_uuid: uuid } });
	},

	kill: function (uuid: any, extn: any) {
		return this.send({ action: 'api', cmd: 'kill', args: { channel_uuid: uuid, extn: extn } });
	},

	eavesdrop: function (uuid: any) {
		return this.send({ action: 'api', cmd: 'eavesdrop', args: { channel_uuid: uuid } });
	},

	conf: function (name: any, action: any, member: any) {
		return this.send({ action: 'api', cmd: 'conf', args: { name: name, action: action, member: member } });
	},

	answer_all: function (ext: any, queue: any) {
		return this.send({ action: 'api', cmd: 'answer_all', args: { extn: ext, queue: queue } });
	},

	group_call: function (ext: any, queue: any, numbers: any, batch_accept: any) {
		return this.send({ action: 'api', cmd: 'group_call', args: { extn: ext, queue: queue, numbers: numbers, batch_accept: batch_accept } });
	},

	sip_gateway: function (profile: any, gateway: any, op: any) {
		return this.send({ action: 'api', cmd: 'sip_gateway', args: { profile: profile, gateway: gateway, op: op } });
	},

	playback: function (filename: any) {
		return this.send({ action: 'api', cmd: 'playback', args: { extn: this._extn, soundfile: filename } });
	},

	stop_playback: function () {
		return this.send({ action: 'api', cmd: 'stop_playback', args: { extn: this._extn } });
	},

	merge_call: function (ext1: any, ext2: any) {
		return this.send({ action: 'api', cmd: 'merge_call', args: { extn1: ext1, extn2: ext2 } });
	},

	/* common apis*/

	/*phone control api, only yealink support for now*/
	api_handfree: function (ext: any) {
		return this.send({ action: 'api', cmd: 'api_handfree', args: { extn: ext } });
	},
	status: function () {
		return ola.ws.readyState;
	},

	send: function (msg: any) {
		//发送消息
		if (!ola.ws) {
			ElMessage.warning('请先签入');
			return;
		}
		msg.uuid = ola.next_uuid();
		ola.ws.send(JSON.stringify(msg));
		return msg.uuid;
	},

	merge: function (target: { [x: string]: any }, additional: { [x: string]: any; hasOwnProperty: (arg0: string) => any }) {
		for (const i in additional) {
			if (additional.hasOwnProperty(i)) {
				target[i] = additional[i];
			}
		}
	},

	next_uuid: function () {
		let u = (parseFloat(ola.uuid.substring(29)) + 1).toString();
		u = u == '2147483647' ? '0' : u;
		while (u.length < 12) {
			u = '0' + u;
		}
		ola.uuid = '73836387-0000-0000-0000-0000-' + u;
		return ola.uuid;
	},
};
