<?

class search {

	private $query_string;
	private $variables = [];
	public $search_results = [];
	private $locations = NULL;
	public $keys = NULL;

	public $sub_Searches = NULL;

	private $key = 0;

	public $extensions = ['m4v', 'mp4', 'mkv', 'avi', 'wmv', 'mov', 'flv', '3gp', 'mpg', 'ts', 'rm', 'swf'];

	/*public $callbacks = [];*/

	public function __construct($locations=NULL) {
		$this->key = $key;
		$this->sub_searches = $object->create();
		$this->variables = [];
		$this->keys = [];
		$this->query_string = '';
		$skip_first = false;
		if($key > 0) {
			$skip_first = true;
		}

		/*$this->callbacks[] = $callback;*/


		$content_type = 'public.movie';

		$query_string = 'kMDItemContentTypeTree CONTAINS[cd] "public.movie" OR kMDItemContentTypeTree CONTAINS[cd] "dyn.ah62d4rv4ge804450" OR kMDItemFSName LIKE[cd] "*.m4v" OR kMDItemFSName LIKE[cd] "*.mp4" OR kMDItemFSName LIKE[cd] "*.mkv" OR kMDItemFSName LIKE[cd] "*.avi" OR kMDItemFSName LIKE[cd] "*.wmv" OR kMDItemFSName LIKE[cd] "*.mov" OR kMDItemFSName LIKE[cd] "*.flv" OR kMDItemFSName LIKE[cd] "*.3gp" OR kMDItemFSName LIKE[cd] "*.mpg" OR kMDItemFSName LIKE[cd] "*.ts" OR kMDItemFSName LIKE[cd] "*.rm" OR kMDItemFSName LIKE[cd] "*.swf"';

		$this->query_string = $query_string;

		$patterns = [
			'(AND\s+)+',
			'(OR\s+)+'
		];

		$replace = [
			'AND',
			'OR'
		];


		/*foreach($patterns as $key => $pattern) {
			$duplicate_conditionals = $object->regex->reg_match($pattern, $this->query_string);
			$this->query_string = $this->reg_replace($duplicate_conditionals, $this->query_string, $replace[$key]);
		}*/


		/*$this->query_string = $object->regex->preg_replace('\(\s+AND', '(', $this->query_string, true);
		$this->query_string = $object->regex->preg_replace('\(\s+OR', '(', $this->query_string, true);
		$this->query_string = $object->strings->trim($this->query_string);*/
		/*$chars = $object->strings->str_split($this->query_string);*/
		/*if($chars[0] == '(' && $chars[$chars->length-1] == ')') {
			$object->splice($chars, 0, 1);
			$object->splice($chars, $chars->length-2, 1);
			$this->query_string = $object->strings->join($chars, '');
		}*/


		$this->locations = $locations;
	}

	public function reg_replace($matches, $query_string, $replace_value) {
		$offset = 0;
		$results = [];
		$last_pos = 0;
		foreach($matches as $index => $match) {
			$length_value = $match['length_value'];

			$results[] = $object->strings->substr($query_string, $last_pos, $index-$last_pos);
			$last_pos = $index+$length_value;
		}

		$results[] = $object->strings->substr($query_string, $last_pos);

		$query_string = $object->strings->join($results, ' '.$replace_value.' ');

		return $query_string;
	}

	public function initial_search($callback) {
		$self = $this;
		$files->search->new_search($this->query_string, $this->variables, $this->locations, function($results) {
			$self->search_results = $self->display_results($results);
			$object->log($self->search_results->length);
			$callback($self->search_results);
		}, function($results) {
			return true;
		});
	}

	public function sub_search($results) {
		$sub_results = [];
		$this->search_results = [];
		$keys = $this->keys;
		foreach($results as $file) {
			$file_value = $file->get_values($this->keys);
			$sub_results[] = $file_value;
		}
		$sub_results = $files->search->get_results($this->query_string, $this->variables, $sub_results); 
		$indicies = $sub_results['indicies'];

		foreach($indicies as $index) {
			$this->search_results[] = $results[$index];
		}

		$this->display_results($this->search_results);
	}

	/*public function display_results_alt($results) {
		foreach($results as $file_values) {
			$object->send('app.searches.results.display_result(data);', [
				'data' => $file_values
			]);
		}
	}*/

	public function display_results($results) {
		$file_value_items = [];


		$remove_string = '/System/Volumes/Data';
		$remove_string_length = $object->strings->strlen($remove_string);

		foreach($results as $file) {
			$file_values = $file->get_values(['FSName', 'Path', 'ContentType', 'ContentTypeTree']);

			if($object->strings->strpos($file_values['kMDItemPath'], $remove_string) === 0) {
				$file_values['kMDItemPath'] = $object->strings->substr($file_values['kMDItemPath'], $remove_string_length); 
			}
			
			$file_value_items[] = $file_values;
		}


		/*$object->send('app.searches.results.display_result_items(data);', [
			'data' => $file_value_items
		]);*/
		return $file_value_items;
	}

	public $numeric_types = [
		'==',
		'!=',
		'>=',
		'<=',
		'<',
		'>'
	];

	public $string_types = [
		'LIKE[cd]',
		'BEGINSWITH[cd]',
		'CONTAINS[cd]',
		'ENDSWITH[cd]'
	];

	public function interpret($search_term, $initial=false, $skip_first=false, $enclose=true, $skip_logical_operator=false) {
		$query_string = '';
		if(!$skip_logical_operator && $object->isset($search_term['logical_operator']) && $search_term['logical_operator'] != NULL) {
			$query_string = $query_string.' '.$search_term['logical_operator'].' ';
		}
		$query_string_value = $query_string;
		$query_string = '';
		$enclose = false;
		if($search_term['type'] == 'group') {
			if(!$skip_first) {
				if(!$initial) {
					$enclose = true;
					$query_string = $query_string.'(';
				} else {
					if($object->strings->strlen($search_term['input_value']) > 0) {
						$query_string = $query_string.'kMDItemTextContent LIKE %@';
						$this->variables[] = $search_term['input_value'];
					}
				}
			}
			foreach($search_term['children'] as $child) {
				if($object->strings->strlen($object->strings->trim($query_string)) > 0) {
					$query_string = $query_string.$this->interpret($child, false, false, $enclose);
				} else {
					$query_string = $this->interpret($child, false, false, $enclose, true);
				}
			}
			if(!$skip_first) {
				if(!$initial && $enclose) {
					$query_string = $query_string.')';
				}
			}
		} else {
			if($object->index_of($this->keys, $search_term['type_value']) == (-1)) {
				$this->keys[] = $search_term['type_value'];
			}

			$convert_to_number = false;
			if($search_term['type_datatype'] != 'date' && $object->index_of($this->numeric_types, $search_term['condition_value']) != (-1)) {
				$convert_to_number = true;
			}
			if($convert_to_number) {
				$to_number = $object->to_number($search_term['input_value']);
				if($to_number == $search_term['input_value']) {
					$search_term['input_value'] = $to_number;
				}
			}

			$query_string = $query_string.$search_term['type_value'].' '.$search_term['condition_value'].' %@';
			if($object->isset($search_term['secondary_input_value'])) {
				if($convert_to_number) {
					$to_number = $object->to_number($search_term['secondary_input_value']);
					if($to_number == $search_term['secondary_input_value']) {
						$search_term['secondary_input_value'] = $to_number;
					}
				}
				$this->variables[] = [$search_term['input_value'], $search_term['secondary_input_value']];
			} else {
				if($search_term['type_datatype'] == 'date') {
					$this->variables[] = ['date_value' => $search_term['input_value']];
				} else {
					$this->variables[] = $search_term['input_value'];
				}
			}
		}
		if($object->strings->strlen($object->strings->trim($query_string)) == 0) {
			return $query_string_value;
		}
		return $query_string_value.$query_string;
	}

}

?>