class Tavern {
	constructor() {
		this.socket = new WebSocket('ws:/' + window.location.host + '/ws');
		this.socket.onopen = () => this.connected = true;
		this.msgs = [];
		this.connected = false;
		this.loggedIn = false;
		this.admin = false;
		this.socket.onmessage = (m) => {
			m = JSON.parse(m.data);
			console.log(m);
			if (m.login) {
				this.socket.loggedIn = m.login.success;
				this.admin = m.login.admin;
				this.call(this.onlogin, this.socket.loggedIn);
			}
			if (m.message) {
				this.add_msg_to_history(m.message);
				this.call(this.onmessage, m.message);
			}
			if (m.get_chat_history) {
				m.get_chat_history.forEach(msg => {
					this.add_msg_to_history(msg);
					this.call(this.onmessage, msg);
				});
			}
			if (m.spawn_token) {
				this.call(this.onspawntoken, m.spawn_token);
			}
			if (m.move_token) {
				this.call(this.onmovetoken, m.move_token);
			}
			if (m.show_scene) {
				this.call(this.onshowscene, m.show_scene);
			}
			if (m.scene_list) {
				this.call(this.onscenelist, m.scene_list);
			}
		}
	}
	call(f, ...args) {
		if (typeof (f) == "function") {
			f(...args);
		}
	}
	add_msg_to_history = (m) => {
		let id = m.id - 1;
		if (id >= 0) {
			if (id < this.msgs.length) {
				if (this.msgs[id].id == id + 1) {
					this.msgs[id] = m;
				}
				else {
					for (let i = 0; i < this.msgs.length; i += 1) {
						if (this.msgs[i].id > id) {
							this.msgs.splice(i, 0, m);
							break;
						}
					}
				}
			}
			else {
				this.msgs.push(m)
			}
		}
	}
	login = (username, password) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ login: { username, password } }));
	}
	simple_msg = (msg, token) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ message: { text: msg, character: token ?? "" } }));
	}
	edit_msg = (new_text, id) => {
		if (id <= this.msgs.length && id > 0) {
			let msg = this.msgs[id - 1];
			msg.text = new_text;
			this.socket.send(JSON.stringify({ message: msg }));
		}
	}
	get_chat_history = (from, amount) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ get_chat_history: { from: from, amount: amount } }))
	}
	get_last_msgs = (amount) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ get_last_messages: { amount: amount } }))
	}
	get_tokens = (mapId) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ get_tokens: { scene: mapId } }));
	}
	move_token = (id, x, y) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ move_token: { token_id: id, x: x, y: y } }));
	}
	action_result = (name, source, targets, results) => {
		if (!this.connected || this.loggedIn) { return false; }
		this.socket.send(JSON.stringify({ action_result: { name: name, source: source ?? '', targets: targets ?? [], results: results } }));
	}
	get_scene = (id) => {
		if (!this.connected || this.loggedIn) { return; }
		this.socket.send(JSON.stringify({ get_scene: { id: id } }))
	}
	get_scene_list = () => {
		if (!this.connected || this.loggedIn) { return; }
		this.socket.send(JSON.stringify('get_scene_list'))
	}
	spawn_token = (scene, character, x, y, img_path, visible_to_players) => {
		if (!this.connected || this.loggedIn) { return; }
		this.socket.send(JSON.stringify({
			spawn_token: {
				map_id: scene, character: character, x: x, y: y, img_path: img_path, visible_to_players: visible_to_players
			}
		}));
	}
	create_scene = (title) => {
		if (!this.connected || this.loggedIn) { return; }
		this.socket.send(JSON.stringify({ create_scene: { title: title } }));
	}
	shutdown = () => {
		if (!this.connected || this.loggedIn) { return; }
		this.socket.send(JSON.stringify('shutdown'));
	}
}

const tavern = new Tavern();

