import { msgs, user, layout, socket, current_room_name } from './Store_chat';

export async function get_room_messages()
{
	console.log("in get_room_messages");
	const response = await fetch('/api/v2/chat/messages');
	const data = await response.json();
	const messages = data.messages;

	messages.forEach(function(item) {
		if (item.name === user.username) {
			item.name = "me";
		}
	});

	msgs.set(messages);
}

export async function create_room(room_name, room_type)
{
	console.log("in create_room");

	let form_data = {
		room_name: room_name,
		room_type: room_type,
	};

	// send the new room
	const response = await fetch('/api/v2/chat/create', {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(form_data),
	});

	// get response status and message
	let response_status = response.status;
	let data = await response.json();
	let response_message = "";
	if (data.message)
		response_message = data.message;

	return {
		status: response_status,
		message: response_message
	};
}

export async function join_room(room_name)
{
	console.log("in join_room");
	console.log(room_name);

	let name = {
		room_name: room_name,
	}
	const response = await fetch('/api/v2/chat/join', {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(name),
	});
	let data = await response.json();
	console.log(data.message);

	socket.emit('join', room_name);
}

export async function change_room(room_name)
{
	console.log("in change_room");

	let room_name_request = {
		room_name: room_name,
	}
	const response = await fetch('/api/v2/chat/change', {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(room_name_request),
	});
	let data = await response.json();
	console.log(data.message);

	await get_room_messages();
	socket.emit('change', room_name);

	current_room_name.set(room_name);
	layout.set("room");
}

export async function get_my_rooms()
{
	console.log("in get_my_rooms");

	const response = await fetch('/api/v2/chat/myrooms');
	const data = await response.json();

	console.log("data.rooms:", data.rooms);
	for (let room of data.rooms)
		console.log(room.name);
	let rooms = data.rooms;

	return rooms;
}

export async function get_all_rooms()
{
	console.log("in get_all_rooms");

	const response = await fetch('/api/v2/chat/allrooms');
	const data = await response.json();

	console.log("data.rooms:", data.rooms);
	for (let room of data.rooms)
		console.log(room.name);
	let rooms = data.rooms;

	return rooms;
}

export async function get_room_users()
{
	console.log("in get_room_users");

	const response = await fetch('/api/v2/chat/roomusers');
	const data = await response.json();

	console.log("data.users:", data.users);
	for (let user of data.users)
		console.log(user.username);
	let users = data.users;

	return users;
}

export async function user_leave_room()
{
	console.log("in leave_room");

	const response = await fetch('/api/v2/chat/removeuser', {
		method: 'DELETE',
	});
	const data = await response.json();

	console.log("data", data);
}


