<?

class main {
	private $data = NULL;

	public $base_instance = NULL;

	public function __construct($base_instance) {
		$this->data = $data->main_instance;
		$this->base_instance = $base_instance;

		$self_sql = $this->data;

		$init_string = '
CREATE TABLE IF NOT EXISTS app_users (
	id text primary key,
	email text,
	password text
);
';


		$init_tables = $object->strings->split($init_string, ';');
		foreach($init_tables as $init_table) {
			if($object->strings->strlen($object->strings->trim($init_table)) > 0) {
				$this->data->execute($init_table, []);
			}
		}
		$table_update = new table_structure_update($init_string, $this->data);

		$object->main_instance = $this;

		$this->test_preg();
	}

	public function test_preg2() {
		$test = [3, 4, 5];
		$res = [
			1,
			2,
			$test,
			[5, 6, 7]
		];

		$object->log($object->toJSON($res));
	}

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

$source = '
	<html>
		<head>
			<style>
				body {
					width:100%;
					height:100%;
					display:1;
				}
				

				div.outer {
					top:30px;
					left:0px;
					right:0px;
					bottom:0px;
				}

				.wrap {
					orientation:1;
					height:50%;
					width:100%;
				}

				.a_container {
					width:50%;
					height:50%;
					top:0px;
					left:0px;
					right:50%;

				}
				.b_container {
					width:50%;
					height:50%;
					top:0px;
					right:0px;
					left:50%;


				}
				.c_container {
					width:50%;
					height:50%;
					bottom:0px;
					left:0px;
				}
				.d_container {
					width:100%;
					height:50%;
					bottom:0px;
					right:0px;
					top:50%;
					left:0px;
				}

				video {
					width:100%;
					height:100%;
				}

				.wrap video {
					width:50%;
					height:100%;
				}

				.mainweb {
					height:100%;
					width:100%;
				}

				web.upcoming {
					top:0px;
					left:0px;
					bottom:0px;
					right:0px;
				}

			</style>
		</head>
		<body id="body" class="body">
			<div class="outer">
				<div class="a_container">
					<video class="main"></video>
				</div>
				<div class="b_container">
					<div class="wrap">
						<video class="preview"></video>
						<video class="preview"></video>
					</div>
					<div class="wrap">
						<video class="preview"></video>
						<video class="preview"></video>
					</div>
					<web class="upcoming" id="upcoming"></web>
				</div>
				<div class="d_container">
					<web class="mainweb"></web>
				</div>
			</div>
		</body>
	</html>
		';
		/*
				<div class="c_container">
					<video class="preview"></video>
				</div>*/
		/*
				<div class="web_container">
					<web></web>
				</div>*/

		$object->log('start parse');

		$html_parser_instance = new html_parser($source);
		$object->log('start 2');
		$res = $html_parser_instance->parse();
		$object->log('completed 2');

		$object->log($res);

		$video_main_item = $res->get_window()->get_document()->query_selector('video.main')[0];

		$object->root = $res;

		$object->video_main_item = $video_main_item->get_parent();


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

		$object->web_main_item = $web_main_item;

		$media->video_main = $video_main_item->get_media_tab();


		$media->video_main->register_callback(function() {
			$object->log('register 1');
		});

		$media->video_main->register_callback(function() {
			$object->log('register 2');
		});

		$media->video_main->register_callback(function($par) {
			$object->main_webview_web->evaluate('app.controls.play_next(data)', function($data) {
				$object->log($data);
			}, ['data' => (-1)]);
		});

		$web_results_info = $res->get_window()->get_document()->query_selector('video.preview');

		$object->log('video length: '.$web_results_info->length);

		$preview_video = [];

		$index = 0;
		foreach($web_results_info as $video) {
			/*$object->log($video);
			$object->log($video->get_media_tab());
			$video->get_media_tab()->play('/Users/siggi/test.mkv');
			$video->get_media_tab()->toggle_audio();*/
			$preview_video[] = $video->get_media_tab();
			$video->get_media_tab()->set_volume(0);

			$video_media = $video->get_media_tab();

			$register = function($index, $video_media) {

				$video_media->register_callback(function() {
					$object->log('register 1');
					$object->main_webview_web->evaluate('app.controls.play_next(data)', function($data) {
						$object->log($data);
					}, ['data' => $index]);
				});

				$video_media->register_callback(function() {
					$object->log('register 2');
				});

				$video_media->register_callback(function($par) {
					$video_media->set_time(0);
					$video_media->pause();
				});
			};

			/*$video_media->fill_view();*/

			$register($index, $video_media);

			$index++;
		}

		$media->preview_video = $preview_video;

		$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');
			});
		});
		/*$web_results_info = $res->get_window()->get_document()->query_selector('.info_container web');

		$web_results_info[0]->get_webview()->load_request('https://wikipedia.org/');

		$object->web->main_info_web = $web_results_info[0]->get_webview();*/

		/*$web_results_video = $res->get_window()->get_document()->query_selector('.video_container web');

		$web_results_video[0]->get_webview()->load_request('https://noob.software/videogalaxy/noobapps/apps/videogalaxy/index.php?path=MDppbmRleDo7MTptYWlucGFnZTo7MjptYWlubWVudTo7MzpyZWNlbnRseV9hZGRlZDo7');
*/	

		/*$web_results = $res->get_window()->get_document()->query_selector('.web_container');
		$object->log($web_results->length);*/


		/*$timeout_callback = async function() {
			$set_box_value = [
	            'set_rectangle' => [-1, -1, -1, -1],
	            'set_width' => 100,
	            'set_height' => 50,
	            'width_type' => 1,
	            'height_type' => 1
			];


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

		$object->set_timeout($timeout_callback, 5);*/

		/*$video_web = $web_results_video[0]->get_webview();*/

		/*$object->set_timeout(async function() {
			$object->log($files);
			$evaluation_script = $files->read_text('Streamline2/mainscript');
			$object->log($evaluation_script);
			$object->log($video_web);
			$video_web->evaluate($evaluation_script, function($res) {
				$object->log($res);
			});
		}, 5);*/


		/*$object->log($res[0]);

		$window_instance = new window();*/

		/*$res[0]->set_window*/
	}

	public function set_upcoming_state($state) {
		$object->log('set upcoming state');
		$object->log($state);
		if($state === (-1)) {
			$wraps = $object->root->get_window()->get_document()->query_selector('.b_container .wrap');
			foreach($wraps as $wrap) {
				$wrap->get_layout()->raise_child();
			}
		} else if($state === (-2)) {
			$object->log('raise webview');
			$wraps = $object->root->get_window()->get_document()->query_selector('.b_container web');
			foreach($wraps as $wrap) {
				$wrap->get_layout()->raise_child();
			}
		}
	}

	public function receive_request($v=NULL, $callback=NULL) {
		$action = NULL;
		if($object->isset($v['action'])) {
			$action = $v['action'];
			delete $v['action'];
		}

		if($action != NULL) {
			$func = $this[$action];
			/*$func($v, function($res) {
				$callback($res, $v['messaage_counter']);
			});*/
			$func($v, $callback);
		}
	}

	public $live_indexing = NULL;

	public function live_index($v=NULL, $callback=NULL) {

		$object->log($object->toJSON([$v]));

		$locations = [];
		$files_value = [];
		$paths = $v['paths'];
		foreach($paths as $path) {
			if($files->is_dir($path)) {
				$locations[] = $path;
			} else {
				$files_value[] = $path;
			}
		}

		$send_callback = function($results) {
			$files_value = $object->concat($files_value, $results);
			/*$object->log($object->toJSON($files_value));*/
			$callback($files_value);
		};

		$callbacks = [
			'send_callback' => $send_callback
		];
		if($locations->length > 0) {
			$this->live_indexing = new live_indexing();
			$this->live_indexing->init($locations, $callbacks);
		} else {
			$callback($files_value);
		}

		/*$callback($v['paths']);*/

	}

	public function toggle_audio($v=NULL, $callback=NULL) {
		$media->video_main->toggle_audio();
		$callback(true);
	}

	public function play($v=NULL, $callback=NULL) {
		$media->video_main->play($v['path']);
		$object->log('play: '.$v['path']);
		$callback(true);
	}

	public function upnext($v=NULL, $callback=NULL) {

		/*
			0	3
			1	2
		*/

		$object->upcoming_webview_web->evaluate('app.set_titles(data)', function($res) {
			return false;
		}, [
			'data' => $v['paths']
		]);
		/*$paths = $v['paths'];*/



		$paths = [$v['paths'][0],$v['paths'][3],$v['paths'][1],$v['paths'][2]];
		/* else if($v['paths']->length == 3) {
			$paths = [$v['paths'][0],$v['paths'][1],NULL,$v['paths'][2]];
		} else if($v['paths']->length == 2) {
			$paths = [$v['paths'][0],$v['paths'][1],NULL];
		}*/
			/*var values = [values[0],values[1],values[2],values[3]];*/

		/*$object->log('paths: '.$object->toJSON($paths));*/

		$index = 0;
		foreach($paths as $path) {
			$object->log('index: '.$index.' - ' .$path);
			if($media->preview_video->length > $index) {
				if($path != NULL) {
					$object->log($media->preview_video[$index]);
					$media->preview_video[$index]->play($path);
					$random = $math->random_int(75)/100;
					$media->preview_video[$index]->set_time($random);
				}
			}
			$index++;
		}
		$callback(true);
		$object->upcoming_webview_web->evaluate('app.set()', function($res) {
			return false;
		}, $object->create());
	}
	

	public function get_current_tab_time($v=NULL, $callback=NULL) {
		$current_time = $media->video_main->time();
		$total_time = $media->video_main->total_time();

		$callback([
			'total_time' => $total_time,
			'current_time' => $current_time
		]);
	}

	public function set_player_time($v=NULL, $callback=NULL) {
		if($media->video_main != NULL) {
			$media->video_main->set_time($v['time']);
		}
		$callback(['result' => 1]);
	}

	public function pause($v=NULL, $callback=NULL) {
		$media->video_main->pause();
		$callback(true);
	}


	public function set_volume($v=NULL, $callback=NULL) {
		$media->video_main->set_volume($v['volume']);
		$callback(true);
	}

	public function get_is_toggled($v=NULL, $callback=NULL) {
		if(!$object->isset($object->video_main_item->is_toggled)) {
			$object->video_main_item->is_toggled = false;
		}
		$callback($object->video_main_item->is_toggled);
	}

	public function toggle_up_next($v, $callback) {

		if(!$object->isset($object->video_main_item->is_toggled_upnext)) {
			$object->video_main_item->is_toggled_upnext = false;
		}

		if($object->video_main_item->is_toggled) {
			$this->toggle_menu($v, $callback);
		} else {

			if(!$object->video_main_item->is_toggled_upnext) {
				$b_container = $object->root->get_window()->get_document()->query_selector('.b_container')[0];
				/*$c_container = $object->root->get_window()->get_document()->query_selector('.c_container')[0];*/

				$b_container->get_layout()->set_box([
					'set_width' => 0
				]);


				$set_box_value = [
		            'set_rectangle' => [0, 0, 0, 50],
					'rectangle_type' => [1, 1, 1, 1],
		            'set_width' => 100,
		            'set_height' => 50,
		            'width_type' => 1,
		            'height_type' => 1
				];


				$object->video_main_item->get_layout()->set_box($set_box_value);
			} else {
				$b_container = $object->root->get_window()->get_document()->query_selector('.b_container')[0];
				/*$c_container = $object->root->get_window()->get_document()->query_selector('.c_container')[0];*/

				$b_container->get_layout()->set_box([
					'set_rectangle' => [50, 0, 0, -1],
					'rectangle_type' => [1, 1, 1, 1],
					'set_width' => 50,
					'set_height' => 50,
		            'width_type' => 1,
		            'height_type' => 1
				]);

				$set_box_value = [
		            'set_rectangle' => [0, 0, 50, 50],
					'rectangle_type' => [1, 1, 1, 1],
		            'set_width' => 50,
		            'set_height' => 50,
		            'width_type' => 1,
		            'height_type' => 1
				];


				$object->video_main_item->get_layout()->set_box($set_box_value);
			}

			/*cubic-bezier(.98,.47,0,1)*/
			/*cubic-bezier(.47,.72,.08,1.01)*/
			$object->video_main_item->get_layout()->animate((35/100), [(47/100), (72/100), (8/100), (1)], [], async function() {
				$object->log('noobscript callback');
				if(!$object->video_main_item->is_toggled_upnext) {
					$object->video_main_item->is_toggled_upnext = true;
				} else {
					$object->video_main_item->is_toggled_upnext = false;
				}

				$callback(true);
			});
		}
	}

	public function call_randomize($v, $callback) {
		$object->main_webview_web->evaluate('app.controls.randomize(data)', function($data) {
			$object->log($data);
		}, ['data' => (-1)]);
		$callback(true);
	}

	public function toggle_menu($v=NULL, $callback=NULL) {

		if(!$object->isset($object->video_main_item->is_toggled)) {
			$object->video_main_item->is_toggled = false;
		}

		if(!$object->video_main_item->is_toggled) {
			$set_box_value = [
	            'set_rectangle' => [0, 0, 0, 0],
	            'set_width' => 100,
	            'set_height' => 100,
	            'width_type' => 1,
	            'height_type' => 1
			];


			$object->video_main_item->get_layout()->set_box($set_box_value);

			$web_box_values = [
				/*'set_rectangle' => [-1, -1, 0, 0],
				'rectangle_type' => [1, 1, 1, 1],*/
				'vertical_alignment' => 1,
				'horizontal_alignment' => 1,
	            'set_width' => 50,
	            'set_height' => 100,
	            'width_type' => 1,
	            'height_type' => 0
			];


			$object->web_main_item->get_layout()->set_box($web_box_values);

			$object->log($object->web_main_item);

			$web_layout = $object->web_main_item->get_layout();

			$b_container = $object->root->get_window()->get_document()->query_selector('.b_container')[0];
			/*$c_container = $object->root->get_window()->get_document()->query_selector('.c_container')[0];*/

			$b_container->get_layout()->set_box([
				'set_width' => 0
			]);
			/*$c_container->get_layout()->set_box([
				'set_width' => 0
			]);*/

			$object->log($web_layout);
		} else {

			$set_box_value = [
	            'set_rectangle' => [0, 0, 50, 50],
				'rectangle_type' => [1, 1, 1, 1],
	            'set_width' => 50,
	            'set_height' => 50,
	            'width_type' => 1,
	            'height_type' => 1
			];


			$object->video_main_item->get_layout()->set_box($set_box_value);

			$web_box_values = [
				/*'set_rectangle' => [0, 0, 0, 0],
				'rectangle_type' => [1, 1, 1, 1],*/
	            'set_width' => 100,
	            'set_height' => 100,
	            'width_type' => 1,
	            'height_type' => 1
			];


			$object->web_main_item->get_layout()->set_box($web_box_values);

			$object->log($object->web_main_item);

			$web_layout = $object->web_main_item->get_layout();

			$b_container = $object->root->get_window()->get_document()->query_selector('.b_container')[0];
			/*$c_container = $object->root->get_window()->get_document()->query_selector('.c_container')[0];*/

			$b_container->get_layout()->set_box([
				'set_rectangle' => [50, 0, 0, -1],
				'rectangle_type' => [1, 1, 1, 1],
				'set_width' => 50,
				'set_height' => 50,
	            'width_type' => 1,
	            'height_type' => 1
			]);
			/*$c_container->get_layout()->set_box([
				'set_rectangle' => [0, -1, -1, 0],
				'rectangle_type' => [1, 1, 1, 1],
				'set_width' => 50,
				'set_height' => 50,
	            'width_type' => 1,
	            'height_type' => 1
			]);*/

			/*
				.b_container {
					width:50%;
					height:50%;
					top:0px;
					right:0px;
					left:50%;


				}
				.c_container {
					width:50%;
					height:50%;
					bottom:0px;
					left:0px;
				}*/

			$object->log($web_layout);
		}
		/*$object->root->get_window()->get_document()->*/
		$object->video_main_item->get_layout()->animate((350/1000), [(59/100), (17/100), (5/10), (91/100)], [], async function() {
			$object->log('noobscript callback');
			if(!$object->video_main_item->is_toggled) {
				$object->video_main_item->is_toggled = true;
			} else {
				$object->video_main_item->is_toggled = false;
				$object->video_main_item->is_toggled_upnext = false;
			}

			$callback(true);
		});
	}

	public function pause_previews() {
		foreach($media->preview_video as $preview) {
			if($preview->is_playing()) {
				$preview->pause();
			}
		}
	}

	public function play_previews() {
		foreach($media->preview_video as $preview) {
			if(!$preview->is_playing()) {
				$preview->pause();
			}
		}
	}

}

?>