<?php

namespace SHServ\Controllers;

use \SHServ\Models\Devices;
use \SHServ\Tools\DeviceScanner;

class DevicesRESTAPIController extends \SHServ\Middleware\Controller {
	public function scanning__ready_to_setup() {
		$device_model = new Devices();
		$devices = $device_model -> get_unregistered_devices();

		return $this -> utils() -> response_success([
			"devices" => $devices
		]);
	}

	public function scanning__all() {
		$device_model = new Devices();
		$devices = $device_model -> scanning_localnet(FCONF["device_ip_range"][0], FCONF["device_ip_range"][1]);

		return $this -> utils() -> response_success([
			"devices" => $devices
		]);
	}

	public function setup_new_device($device_ip, $alias, $description) {
		$devices_model = new \SHServ\Models\Devices();
		$result = $devices_model -> connect_new_device($device_ip, $alias, $description);

		return $this -> utils() -> response_success([
			"result" => $result ? true : false
		]);
	}

	public function remove_device($device_id) {
		$devices_model  = new Devices();
		$result = $devices_model -> remove_device(intval($device_id));

		if(!$result["result"]) {
			return $this -> utils() -> response_error(
				$result["err_alias"], 
				[], 
				[ "device_msg" => $result["device_msg"] ]
			);
		}

		return $this -> utils() -> response_success([
			"result" => $result["result"] ? true : false,
			"device_msg" => $result["device_msg"]
		]);
	}

	public function reboot_device($device_id) {
		$devices_model = new Devices();
		$result = $devices_model -> reboot_device(intval($device_id));

		if(!$result["result"]) {
			return $this -> utils() -> response_error(
				$result["err_alias"], 
				[], 
				[ "device_msg" => $result["device_msg"] ]
			);
		}

		return $this -> utils() -> response_success([
			"result" => $result["result"] ? true : false,
			"device_msg" => $result["device_msg"]
		]);
	}
}