<?

class main_app {

	private $sql;
	
	private $statement;

	private $dates;

	public $base_instance = NULL;

	public function __construct($base_instance) {
		$this->sql = $data->fetch('main');
		$this->statement = $data->statement;
		$this->base_instance = $base_instance;

		$self_sql = $this->sql;

		$init_string = 'CREATE TABLE IF NOT EXISTS settings (
			id INTEGER primary key autoincrement,
			property_name TEXT,
			property_value TEXT );';


		if(false) {

			$init_tables = $object->strings->split($init_string, ';');

			foreach($init_tables as $init_table) {
				if($init_table != NULL && $object->strings->strlen($init_table) > 0) {
					$this->sql->execute($init_table, []);
				}
			}
			$table_update = new table_structure_update($init_string, $this->sql);
		}


		$this->run_main();


		$object->assign_global_callback('event', function($input) {
			$object->send('app.send_event(e)', [
				'e' => $input
			]);
		});
	}

	public function assign_client_target($v) {
		return $this->base_instance->get_last_client_id();
	}

	public function set_client_target($v) {
		$target_id = $v['target_id'];


		$this->base_instance->set_client_target($target_id);

		return true;
	}

	public function apply_event($v) {
		$object->apply_event($v['data']);

		return true;
	}

	public function return_control($v) {
		$object->return_control();

		return true;
	}

	public function start_server($v) {
		$this->base_instance->reset_ws();
		$object->log($object->toJSON($v));
		$object->set_ws_server($v['ip_address'], $object->to_number($v['port']), function($res) {
			$object->log($res);
			$object->send('app.ws.init(true)', $object->create());
			if(!$object->has_set_event_tap) {
				$object->has_set_event_tap = true;
				$object->set_event_tap();
			}
		});
	}

	public function run_main() {
		/*$preg_split = new preg_split('(<|>|\")', '<value prop="test"></value>');
		$res = $preg_split->get();
		$object->log($object->toJSON($res));*/

		$source = $files->read_text('streamline2/main', 'html');
		/*
				<div class="c_container">
					<video class="preview"></video>
				</div>*/
		/*
				<div class="web_container">
					<web></web>
				</div>*/


		$html_parser_instance = new html_parser($source);
		$res = $html_parser_instance->parse();

		$object->log('run main1');
		$object->log($res);

		$web_main_item = $res->get_window()->get_document()->query_selector('web.mainweb')[0];


		$object->log('run main1');

		$object->web_main_item = $web_main_item->get_webview();

		$object->send = function($eval_string, $arguments) {
			$object->web_main_item->evaluate($eval_string, NULL, $arguments);
		};


		$object->log('run main1');

		$self = $this;

		$object->web_main_item->init_messages(function($message_value) {
			/*$data_res = $self->base_instance->receive_messages($message_value);*/
			$message_counter = $message_value['message_counter'];
			delete $message_value['message_counter'];
			$action = $message_value['action'];
			delete $message_value['action'];
			$data_res = $self[$action]($message_value);
			$data_res = [
				'message_counter' => $message_counter,
				'message' => $data_res
			];
			if($object->isset($data_res) && $data_res != NULL && $data_res['message_counter'] != (-1)) {
	    		$object->send('app.receive_messages(data)', ['data' => $data_res]);
	    	}
		});

		$object->assign_global_callback('fullscreen', function() {
			$outer = $res->get_window()->get_document()->query_selector('.outer')[0];

			$set_box_value = [
	            'set_rectangle' => [0, 0, 0, 0]
			];


			$outer->get_layout()->set_box($set_box_value);
			$outer->get_layout()->animate((350/1000), [(59/100), (17/100), (5/10), (91/100)], [], async function() {
				$object->log('noobscript callback');
			});
		});

		$object->assign_global_callback('exit_fullscreen', function() {
			$outer = $res->get_window()->get_document()->query_selector('.outer')[0];

			$set_box_value = [
	            'set_rectangle' => [0, 30, 0, 0]
			];


			$outer->get_layout()->set_box($set_box_value);
			$outer->get_layout()->animate((350/1000), [(59/100), (17/100), (5/10), (91/100)], [], async function() {
				$object->log('noobscript callback');
			});
		});
		$object->log('run main completed');

		$object->make_accessible = function($path, $callback) {
			$self->make_accessible($path, $callback);
		};
	}

	

}

?>
