<?php

namespace SHServ\Models;

use \SHServ\Middleware\ControlScripts;
use \SHServ\Entities\Script;

class Scripts extends \SHServ\Middleware\Model {

	public function get_scopes_list(): Array {
		$instances = app() -> control_scripts_instances;
		$scopes = [];

		foreach($instances as $name => $scope) {
			$ref = new \ReflectionClass($scope);
			$path_info = pathinfo($ref -> getFileName());

			$scopes[] = [
				"name" => $name,
				"filename" => $path_info["basename"],
				"path" => $path_info["dirname"],
				"state" => $this -> script_state("scope", $name) ? "enabled" : "disabled"
			];
		}

		return $scopes;
	}

	public function remove_scope(String $name): Bool {
		$scopes = app() -> control_scripts_instances;

		if(!isset($scopes[$name])) {
			return false;
		}

		$ref = new \ReflectionClass($scopes[$name]);
		$filepath = $ref -> getFileName();

		if(!file_exists($filepath)) {
			return false;
		}

		if(!@unlink($filepath)) {
			return false;
		}

		$script = new Script($name);
		$script -> remove();

		return true;
	}

	public function script_state(String $type, String $uniq_name): Bool {
		$result = $this -> thin_builder() -> select(
			Script::$table_name,
			[ "state" ],
			[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
		);

		return !$result ? false : ($result[0]["state"] == "enabled");
	}

	public function set_script_state(String $type, String $uniq_name, Bool $state): Bool {
		$result = $this -> thin_builder() -> select(
			Script::$table_name,
			Script::get_fields(),
			[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
		);

		if(!$result) {
			return $this -> thin_builder() -> insert(
				Script::$table_name,
				[
					"uniq_name" => $uniq_name,
					"type" => $type,
					"state" => $state ? "enabled" : "disabled",
					"create_at" => date("Y-m-d H:i:s")
				]
			) ? true : false;
		}

		// update
		$script = new Script($result[0]["id"], $result[0]);
		$script -> state = $state ? "enabled" : "disabled";
		return $script -> update() ? true : false;
	}

	public function enable_script(String $type, String $uniq_name): Bool {
		return $this -> set_script_state($type, $uniq_name, true);
	}

	public function disable_script(String $type, String $uniq_name): Bool {
		return $this -> set_script_state($type, $uniq_name, false);
	}

	public function regular_scripts_list(): Array {
		return $this -> get_scripts_list( "regular", ControlScripts::get_regular_scripts() );
	}

	public function actions_scripts_list(): Array {
		return $this -> get_scripts_list( "action", ControlScripts::get_actions_scripts() );
	}

	public function get_scripts_list(String $type, Array $scripts): Array {
		$data = [];

		foreach($scripts as $alias => $script_data) {
			$data[] = [
				"alias" => $alias,
				"type" => $type,
				"name" => $script_data["attributes"]["name"],
				"state" => $this -> script_state($type, $alias) ? "enabled" : "disabled",
				"description" => $script_data["attributes"]["description"],
				"filename" => $script_data["attributes"]["filename"],
				"path" => $script_data["attributes"]["path"],
				"created_by" => $script_data["attributes"]["author"],
			];
		}

		return $data;
	}

	public function scope_is_exists(String $name): Bool {
		$instances = app() -> control_scripts_instances;

		return isset($instances[$name]);
	}
}