export class AreasApi {
	constructor(core) {
		this.core = core;
	}

	// GET /api/v1/areas/list
	list(cb) {
		return this.core.api_get("/api/v1/areas/list", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.areas) {
				for(let item of resp.data.areas) {
					DataProvider.setRaw(`areas.${item.id}`, item);
				}
				DataProvider.setRaw("areas.list", resp.data);
			}

			return cb(err, resp, meta);
		});
	}

	// GET /api/v1/areas/id/{{area_id}}/list
	inner_list(area_id, cb) {
		const safe = encodeURIComponent(String(area_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/list`, cb);
	}

	// POST /api/v1/areas/new-area
	new_area(payload, cb) {
		// payload: { type, alias, display_name }
		return this.core.api_post("/api/v1/areas/new-area", payload, cb);
	}

	// GET /api/v1/areas/id/{{area_id}}/remove
	remove(area_id, cb) {
		const safe = encodeURIComponent(String(area_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/remove`, cb);
	}

	// POST /api/v1/areas/place-in-area
	place_in_area(payload, cb) {
		// payload: { target_id, place_in_area_id }
		return this.core.api_post("/api/v1/areas/place-in-area", payload, cb);
	}

	// POST /api/v1/areas/update-display-name
	update_display_name(payload, cb) {
		// payload: { area_id, display_name }
		return this.core.api_post("/api/v1/areas/update-display-name", payload, cb);
	}

	// POST /api/v1/areas/update-alias
	update_alias(payload, cb) {
		// payload: { area_id, new_alias }
		return this.core.api_post("/api/v1/areas/update-alias", payload, cb);
	}

	// GET /api/v1/areas/id/{{area_id}}/devices
	devices(area_id, cb) {
		const safe = encodeURIComponent(String(area_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/devices`, cb);
	}

	// GET /api/v1/areas/id/{{area_id}}/scripts
	scripts(area_id, cb) {
		const safe = encodeURIComponent(String(area_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/scripts`, cb);
	}

	// GET /api/v1/areas/id/{{target_id}}/unassign-from-area
	unassign_from_area(target_id, cb) {
		const safe = encodeURIComponent(String(target_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/unassign-from-area`, cb);
	}

	// GET /api/v1/areas/types/list
	types_list(cb) {
		return this.core.api_get("/api/v1/areas/types/list", cb);
	}

	// GET /api/v1/areas/reboot_devices
	// GET /api/v1/areas/id/{{area_id}}/reboot_devices
	reboot_devices(area_id, cb) {
		if (area_id === undefined || area_id === null) {
			return this.core.api_get("/api/v1/areas/reboot_devices", cb);
		}
		const safe = encodeURIComponent(String(area_id));
		return this.core.api_get(`/api/v1/areas/id/${safe}/reboot_devices`, cb);
	}
}