<?
class table_structure_update {
	public function __construct($init_command, $data_instance) {
		$create_commands = $object->strings->split($init_command, 'CREATE TABLE IF NOT EXISTS');
		foreach($create_commands as $item) {
			$table_parts = $object->strings->split($item, '(');
			if($table_parts->length > 1) {
				$nametrimmed = $object->strings->trim($table_parts[0]);
				$sub_items = $object->strings->split($table_parts[1], ')')[0];
				$sub_items = $object->strings->split($sub_items, ',');
				$index = 0;
				$columns = $data_instance->table_columns($nametrimmed);
				foreach($sub_items as $sub_item) {
					$sub_item_value = $object->strings->split($object->strings->trim($sub_item), ' ')[0];
					$flag = false;
					foreach($columns as $column) {
						if($column == $sub_item_value) {
							$flag = true;
						}
					}
					if(!$flag) {
						$data_instance->execute('ALTER TABLE '.$nametrimmed.' ADD COLUMN '.$sub_item, []);
					} else {
					}
				}
			}
		}
	}
}

class statement {
	public function generate($x, $table, $prevent_id_generating=false, $user_id=NULL, $set_type=(-1)) {
		$output = NULL;
		$keys = [];
		$type = 0;

		/*if($object->isset($x['set_uuid'])) {
			$x['id'] = $x['set_uuid'];
			delete $x['set_uuid'];
			$type = 1;
			$set_type = 1;
		}*/
		if($object->isset($x['modified']) && $x['modified'] == (-1)) {
			delete $x['modified'];
		}
		if($object->isset($v['id']) && $object->strings->strlen($v['id']) == 0) {
			delete $v['id'];
		}
		$table_columns = $data->main_instance->table_columns($table);
		/*$x_unaltered = $x;
		$x = [...$x];*/
		/*$x_unaltered = [...$x];*/
		if($object->index_of($table_columns, 'created') != (-1)) {
			/*if(!$object->isset($x_unaltered['created']) || $x_unaltered['created'] == NULL || $object->strings->strlen($x_unaltered['created']) == 0) {*/
				$x['created'] = 'current_timestamp';
			/*}*/
		}
		if($object->index_of($table_columns, 'modified') != (-1)) {
			/*if(!$object->isset($x_unaltered['modified']) || $x_unaltered['modified'] == NULL || $object->strings->strlen($x_unaltered['modified']) == 0) {*/
				$x['modified'] = 'current_timestamp';
			/*}*/
		}
		if($object->index_of($table_columns, 'user_id') != (-1) && $user_id != NULL) {
			$x['user_id'] = $user_id;
		}
		if($object->index_of($table_columns, 'id') == (-1)) {
			$prevent_id_generating = true;
		}
		/*if($object->index_of($table_columns, 'user_id') != (-1)) {

		}*/
		if($x->length > 0) {
			if($object->isset($x['id'])) {
				if($x['id'] == '-1') {
					$type = 1;	
				} else {
					$type = 0;	
				}
			} else {
				$type = 1;	
			}
			if($set_type != (-1)) {
				$type = $set_type;
			}
			if($type == 0) {
				$output = 'UPDATE '.$table.' SET ';
				if($object->isset($x['id'])) {
					$counter = 0;
					if($object->isset($x['created'])) {
						delete $x['created'];
					}
					foreach($x as $key => $x_value) {
						$keys[] = $key;
						if($key != 'id') {
							if($counter > 0) {
								$output = $output.', ';
							}
							if(($key == 'created' || $key == 'modified') && $object->strings->lower($x_value) == 'current_timestamp') {
								if($key != 'created') {
									$output = $output.$key.' = current_timestamp';
								}
							} else {
								$output = $output.$key.' = ?';
							}
							$counter = $counter+1;
						}
					}
					$output = $output.' WHERE id = ?';
				}
				/*($row, $table_name, $relations, $set_values=NULL, $create=false)*/
				/*$data->data_base->build_row(NULL, $table, NULL, $x, false);*/
			} else {
				if(!$prevent_id_generating) {
					$x['id'] = $data->main_instance->generate_uuid().'-'.$table;
				}
				$output = 'INSERT INTO '.$table.' (';
				$counter = 0;
				foreach($x as $key => $x_value) {
					$keys[] = $key;
					if($counter > 0) {
						$output = $output.', ';
					}
					$output = $output.$key;
					$counter = $counter+1;
				}
				$counter = 0;
				$output = $output.') VALUES (';
				foreach($x as $key => $x_value) {
					if($counter > 0) {
						$output = $output.', ';
					}
					if(($key == 'created' || $key == 'modified') && $object->strings->lower($x_value) == 'current_timestamp') {
						$output = $output.'current_timestamp ';
					} else {
						$output = $output.'? ';
					}
					$counter = $counter+1;
				}
				$output = $output.')';
				/*$data->data_base->build_row(NULL, $table, NULL, $x, true);*/
			}

			if($object->isset($x['modified'])) {
				delete $x['modified'];
			}
			if($object->isset($x['created'])) {
				delete $x['created'];
			}
			/*$x_unaltered['id'] = $x['id'];*/
			return $output;
		}
		return NULL;
	}
}


class base {
	public $apps=NULL;
	private $dict = NULL;
	public function __construct($instance_input) {
		$object->has_set_event_tap = false;
		$data->statement = new statement();
		$object->deep_copy = function($input) {
			return $object->fromJSON($object->toJSON($input));
		};
		$object->index_of = function($input, $search_value) {
			foreach($input as $indexofkey => $value) {
				if($search_value == $value) {
					return $indexofkey;
				}
			}
			return 0-1;
		};
		$object->union = function($a, $b, $comparator) {
			foreach($b as $b_value) {
				$exists = false;
				foreach($a as $a_value) {
					$comparator_result = $comparator($a_value, $b_value);
					if($comparator_result) {
						$exists = true;
					}
				}
				if(!$exists) {
					$a[] = $b_value;
				}
			}
			return NULL;
		};
		$object->map = function($arr, $func) {
			$result = [];
			foreach($arr as $row) {
				$result[] = $func($row);
			}
			return $result;
		};
    	$files->append_path = function($path, $file) {
    		$path_strlen = $object->strings->strlen($path);
    		$strrev = $object->strings->strrev($path);
    		if($object->strings->strpos($strrev, '/') == 0) {
    			$path = $object->strings->substr($path, 0, $path_strlen-1);
    		}
    		if($object->strings->strpos($file, '/') != 0) {
    			$file = '/'.$file;
    		}
    		return $path.$file;
    	};
    	/*$object->create = function($input) {
			$created_object = ['test' => 'test'];
			delete $created_object['test'];
			return $created_object;
		};*/

		$object->count = function($arr) {
			return $arr->length;
		};

		$object->get_first_row = function($rows) {
			if($rows->length > 0) {
				return $rows[0];
			}
			return NULL;
		};

		$object->array_keys = function($value) {
			return $object->keys($value);
		};

    	$files->append_path_depr = function($path, $file) {
    		$path_strlen = $object->strings->strlen($path);
    		$strrev = $object->strings->strrev($path);
    		if($object->strings->strpos($strrev, '/') == 0) {
    			$path = $object->strings->substr($path, 0, $path_strlen-1);
    		}
    		if($object->strings->strpos($file, '/') != 0) {
    			$file = '/'.$file;
    		}
    		return $path.$file;
    	};

    	$object->strings->explode = function($delimiter, $value) {
    		return $object->strings->split($value, $delimiter);
    	};

    	$object->strings->implode = function($delimiter, $values) {
    		return $object->strings->join($values, $delimiter);
    	};

    	$object->regex->preg_split = function($regex_value, $text, $mark_delimiters) {
			$preg_split_instance = new preg_split($regex_value, $text, $mark_delimiters);
			return $preg_split_instance->get();
		};

		$object->regex->preg_replace = function($regex_value, $replace_value, $text) {
			$split = $object->regex->preg_split($regex_value, $text, true);
			return $object->strings->join($split, $replace_value);
		};

		$this->ws_dict = $object->create();

		$this->apps = [
			'main' => new main_app($this)
    	];

	}

	public $indexing_in_progress = false;

	public $ws_connections = [];

	public $ws_dict;

	public function reset_ws() {
		$object->send('app.clients.remove(data)', [
			'data' => -1
		]);
		$this->ws_connections = [];
		$this->ws_dict = $object->create();
		$this->last_client_id = 0;
	}

    public function __add_ws_connection($ws_item) {
		$this->ws_connections[] = $ws_item;

		$self = $this;

		$remove_callback = function($par) {
			$object->remove_item($self->ws_connections, $ws_item);
			delete $self->ws_dict[$ws_item->client_id];
			$client_id = $ws_item->client_id;
			$object->send('app.clients.remove(data)', [
				'data' => $client_id
			]);
			$object->dispose_parent($remove_callback);
		};

		$ws_item->add_remove_callback($remove_callback);

		$this->last_client_id = $this->last_client_id + 1;

		$ws_item->client_id = $this->last_client_id;

		$this->ws_dict[$ws_item->client_id] = $ws_item;

		$object->send('app.clients.add()', $object->create());

		$ws_item->add_response_callback(async function($message) {
			/*foreach($self->ws_connections as $connection) {
				if(!$object->equals($ws_item, $connection)) {
					$connection->send($message);
				}
			}*/
			if($self->set_ws_item_target == NULL && $this->ws_connections->length > 1) {
				$self->set_ws_item_target = $this->ws_connections[1];
			} else if($self->set_ws_item_target == NULL && $this->ws_connections->length <= 1) {
				$object->return_control();

				return false;
			}

			$connection = $self->set_ws_item_target;

			if(!$object->equals($ws_item, $connection)) {
				$connection->send($message);
			}
		});
	}

	public $last_client_id = 0;

	public function get_last_client_id() {
		return $this->last_client_id;
	}

	public $set_ws_item_target = NULL;

	public function set_client_target($target_id) {
		/*foreach($this->ws_connections as $connection) {
			$connection->is_active = false;
		}*/
		$set_ws_item = $this->ws_dict[$target_id];
		if($object->isset($set_ws_item)) {
			$this->set_ws_item_target = $set_ws_item;
		}
	}
}
$base_instance = new base(NULL);

?>
