class Api {
	getAuthors(params, cb) {
		const xhr = new XMLHttpRequest();
		params = Object.entries(params)
			.map(([key, value]) => `${key}=${value}`)
			.join("&");
		params = params !== "" ? `&${params}` : "";
		xhr.open(
			"GET",
			`/api/v/1.0/get-authors?response_format=json${params}`,
			true,
		);
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send();
	}

	getMuzic(params, cb) {
		const xhr = new XMLHttpRequest();
		params = Object.entries(params)
			.map(([key, value]) => `${key}=${value}`)
			.join("&");
		params = params !== "" ? `&${params}` : "";
		xhr.open("GET", `/api/v/1.0/get-muzic?response_format=json${params}`, true);
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send();
	}

	deleteMuzic(id, cb) {
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/remove-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			if (JSON.parse(this.responseText).response === undefined) {
				throw this.responseText;
			}
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "muzic",
				id,
			}),
		);
	}

	editMuzic(id, name, data, author_id, cb) {
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/edit-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			if (JSON.parse(this.responseText).response === undefined) {
				throw this.responseText;
			}
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "muzic",
				id,
				name,
				author_id,
				data,
			}),
		);
	}

	createMuzic(author_id, name, data = null, cb) {
		data = data === null ? undefined : data;
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/create-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "muzic",
				name,
				author_id,
				data,
			}),
		);
	}

	createAuthor(name, cb) {
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/create-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "author",
				name,
			}),
		);
	}

	removeAuthor(id, cb) {
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/remove-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "author",
				id,
			}),
		);
	}

	editAuthor(id, name, cb) {
		const xhr = new XMLHttpRequest();
		xhr.open("POST", `/api/v/1.0/edit-item?response_format=json`, true);
		xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
		xhr.onreadystatechange = function (event) {
			//console.log(event);
			if (this.readyState != 4) return;
			cb(JSON.parse(this.responseText).response);
		};
		xhr.send(
			JSON.stringify({
				type: "author",
				id,
				name,
			}),
		);
	}
}

const api = new Api();
