/* =========================
   Devices module
========================= */

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

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

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

	// GET /api/v1/devices/scanning/setup
	scanning_setup(cb) {
		return this.core.api_get("/api/v1/devices/scanning/setup", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.devices) {
				for(let i in resp.data.devices) {
					DataProvider.setRaw(`scanning.setup.devices.${i}`, resp.data.devices[i]);
				}
			}

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

	// GET /api/v1/devices/scanning/all
	scanning_all(cb) {
		return this.core.api_get("/api/v1/devices/scanning/all", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.devices) {
				for(let i in resp.data.devices) {
					DataProvider.setRaw(`scanning.devices.${i}`, resp.data.devices[i]);
				}
			}

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

	// POST /api/v1/devices/setup/new-device
	setup_new_device(payload, cb) {
		// payload: { device_ip, alias, name, description }
		return this.core.api_post("/api/v1/devices/setup/new-device", payload, cb);
	}

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

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

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

	// POST /api/v1/devices/action
	action(payload, cb) {
		// payload: { device_id, action, params }
		return this.core.api_post("/api/v1/devices/action", payload, cb);
	}

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

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

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

	// GET /api/v1/devices/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/devices/id/${safe}/unassign-from-area`, cb);
	}

	// POST /api/v1/devices/update-name
	update_name(payload, cb) {
		// payload: { device_id, name }
		return this.core.api_post("/api/v1/devices/update-name", payload, cb);
	}

	// POST /api/v1/devices/update-description
	update_description(payload, cb) {
		// payload: { device_id, description }
		return this.core.api_post("/api/v1/devices/update-description", payload, cb);
	}

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

	// POST /api/v1/devices/resetup
	resetup(payload, cb) {
		// payload: { device_id }
		return this.core.api_post("/api/v1/devices/resetup", payload, cb);
	}

	// POST /api/v1/devices/reset
	reset(payload, cb) {
		// payload: { device_id }
		return this.core.api_post("/api/v1/devices/reset", payload, cb);
	}
}
