const tavern = {
	socket: socket = new WebSocket('ws:/' + window.location.host + '/ws'),
	msgs: [],
	connected: false,
	loggedIn: false,
	call: (f, ...args) => {
		if (typeof (f) == "function") {
			f(...args);
		}
	}
};
tavern.add_msg_to_history = (m) => {
	let id = m.id - 1;
	if (id >= 0) {
		if (id < tavern.msgs.length) {
			if (tavern.msgs[id].id == id + 1) {
				tavern.msgs[id] = m;
			}
			else {
				for (let i = 0; i < tavern.msgs.length; i += 1) {
					if (tavern.msgs[i].id > id) {
						tavern.msgs.splice(i, 0, m);
						break;
					}
				}
			}
		}
		else {
			tavern.msgs.push(m)
		}
	}
}

tavern.socket.onopen = () => tavern.connected = true;
tavern.socket.onmessage = (m) => {
	m = JSON.parse(m.data);
	console.log(m);
	if (m.login) {
		tavern.socket.loggedIn = m.login.success;
		tavern.call(tavern.onlogin, tavern.socket.loggedIn);
	}
	if (m.message) {
		tavern.add_msg_to_history(m.message);
		tavern.call(tavern.onmessage, m.message);
	}
	if (m.get_chat_history) {
		m.get_chat_history.forEach(msg => {
			tavern.add_msg_to_history(msg);
			tavern.call(tavern.onmessage, msg);
		});
	}
	if (m.spawn_token) {
		tavern.call(tavern.onspawntoken, m.spawn_token);
	}
	if (m.move_token) {
		tavern.call(tavern.onmovetoken, m.move_token);
	}
	if (m.show_scene) {
		tavern.call(tavern.onshowscene, m.show_scene);
	}
	if (m.scene_list) {
		tavern.call(tavern.onscenelist, m.scene_list);
	}
}
tavern.login = (username, password) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ login: { username, password } }));
}
tavern.simple_msg = (msg, token) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ message: { text: msg, character: token ?? "" } }));
}
tavern.edit_msg = (new_text, id) => {
	if (id <= tavern.msgs.length && id > 0) {
		let msg = tavern.msgs[id - 1];
		msg.text = new_text;
		tavern.socket.send(JSON.stringify({ message: msg }));
	}
}
tavern.get_chat_history = (from, amount) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ get_chat_history: { from: from, amount: amount } }))
}
tavern.get_last_msgs = (amount) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ get_last_messages: { amount: amount } }))
}
tavern.get_tokens = (mapId) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ get_tokens: { scene: mapId } }));
}
tavern.move_token = (id, x, y) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ move_token: { token_id: id, x: x, y: y } }));
}
tavern.action_result = (name, source, targets, results) => {
	if (!tavern.connected || tavern.loggedIn) { return false; }
	tavern.socket.send(JSON.stringify({ action_result: { name: name, source: source ?? '', targets: targets ?? [], results: results } }));
}
tavern.get_scene = (id) => {
	if (!tavern.connected || tavern.loggedIn) { return; }
	tavern.socket.send(JSON.stringify({ get_scene: { id: id } }))
}
tavern.get_scene_list = () => {
	if (!tavern.connected || tavern.loggedIn) { return; }
	tavern.socket.send(JSON.stringify('get_scene_list'))
}
