<?

class training {

	public function __construct($set_id=NULL) {
		$this->start($set_id);
	}

	/*public function map($images) {


	}*/

	public function start_test($set_id) {
		$input_vector_test = [1, 0, 0, 1];
		$targets = [1, 0];

		$network = new network(1, $input_vector_test->length);

		$object->log('input_vector_test');
		$object->log($object->toJSON($input_vector_test));

		$counter = 0;

		while($counter < 10000) {

			$output_value = $network->compute_output($input_vector_test, $targets);
			$counter = $counter + 1;


			$print = [$targets, $output_value];
		
			$object->log($object->toJSON($print));
		}
	}

	public function start($set_id=NULL) {
		$images_location = '/Users/siggi/Movies/frames/';

		$images = $files->list_files($images_location);

		$images_paths = [];

		foreach($images as $image) {
			$extension = $files->get_extension($image);

			if($extension == 'png') {
				$images_paths[] = $files->append_path($images_location, $image);
			}
		}

		/*foreach($images_paths as $image_path) {
			$object->log($image_path);
		}*/




		/*$layer_generator = new layer_generator(NULL, 100, 3, 1, 1);


		$row = NULL;

		$adders = $layer_generator->adders;
		if($set_id != NULL) {
			$query = 'SELECT * FROM filters WHERE set_id = ?';
			$row = $data->main_instance->get_row($query, [$set_id]);

			if($row != NULL) {
				$json = $object->fromJSON($row['jsonvalue']);
				if($object->isset($json['parameters'])) {
					$adders[0]->set_parameters($json['parameters']);
				}
			}
		}

		$output_points = [];

		$points = NULL;

		$counter = 0;

		$markers = $layer_generator->markers;*/


	/*	$set_images_paths = $object->randomize_list($images_paths);

		$set_images_paths = $object->splice($set_images_paths, 0, 3);

		$total_error = 0;

		$img_points_set = []:*/

		/*$set_images_paths = $object->randomize_list($images_paths);*/

		$set_images_paths = $images_paths;

		$img_points_set = [];

		$set_images_paths = $object->splice($set_images_paths, 0, 1);

		foreach($set_images_paths as $set_image_path) {

			$from_img = new from_img($set_image_path);

			$img_points_set[] = $from_img->points;



		}


		$img_points_item = $img_points_set[0];

		/*$img_points_item_scaled = $this->scale($img_points_item);*/

		$object->log('set pixels');

		$object->log($object->toJSON($img_points_item[0][0]));

		/*$object->log($object->toJSON($img_points_item_scaled));

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

		/*$object->last_set_pixels = $img_points_item_scaled;*/

		$object->set_pixels($img_points_item);

		/*return false;*/


		$mapper = new mapper($img_points_set, $object->default_filter_size);

		$clusters = $mapper->get_clusters_alt();
		/*$cluster_items = $mapper->build_clusters($clusters);*/

		$filters = new filters($clusters);

		$set_counter = 0;

		while($set_counter < 1) {

			foreach($img_points_set as $img_points_item) {


				$filters->train(1, $img_points_item, $object->default_field_size);
			}

			$set_counter = $set_counter+1;
		}

		$img_point_test = $img_points_set[0];

		$object->log('print recall');

		$recall = $filters->perform($img_point_test);
		$object->log('print recall');

		/*$object->log($object->toJSON($recall));*/

		/*return false;*/

		/*$recall_scaled = $this->scale($recall);*/

		/*$object->set_pixels($recall);*/


		/*$recall = $filters->perform($img_point_test, NULL, $object->default_filter_size, false);*/

		$input_vector_test = $recall[0][0];

		$network = new network(3, $input_vector_test->length);

		$object->log('input_vector_test');
		$object->log($object->toJSON($input_vector_test));

		$output_test = $network->compute_output($input_vector_test);

		$object->log('output_test');

		$object->log($object->toJSON($output_test));

		$set_network_vectors = [];

		$iterator = new iterator();

		$iterator->iterate($img_point_test->length, function($x, $y) {
			if($recall->length > $y && $recall[$y]->length > $x && $recall[$y][$x]->length == $input_vector_test->length) {
				$set_item = [
					'input' => $recall[$y][$x],
					'targets' => [$img_point_test[$y][$x][1], $img_point_test[$y][$x][2]]
				];

				$object->log($object->toJSON($set_item));

				$set_network_vectors[] = $set_item;
			}
			return NULL;
		});


		$object->log('before compute output');

		$iteration_counter = 0;

		while($iteration_counter < 35) {

			$object->log('iteration');

			$outputs_print = [];

			$output_print_row = [];
			foreach($set_network_vectors as $item) {
				$output_value = $network->compute_output($item['input'], $item['targets']);

				$object->log($object->toJSON(['o' => $output_value, 't' => $item['targets']]));

				$print = [$item['input'][0], $output_value[0],  $output_value[1]];

				$object->log($object->toJSON($print));

				$output_print_row[] = $print;
				if($output_print_row->length == ($img_point_test->length - $object->default_filter_size)) {
					$outputs_print[] = $output_print_row;
					$output_print_row = [];
				}

			}
			$outputs_print[] = $output_print_row;

			$object->log($object->toJSON($outputs_print));

			$outputs_print_scaled = $this->scale($outputs_print);

			$object->log($object->toJSON($outputs_print_scaled));

			$object->log('set pixels');

			$set_pixels = [...$outputs_print_scaled];

			$object->set_pixels($set_pixels);

			$iteration_counter = $iteration_counter + 1;
		}


		$object->log('printed');
		return false;
	}

	public function scale($original) {
		return $original;

		$scale_to = 8;
		$set_scale = $original->length * $scale_to;



		$generator = new generator();

		$outputs = $generator->random_points($set_scale, 1, 0);

		$iterator = new iterator();

		$iterator->iterate($original->length, function($x, $y) {
			$offset_y = 0;
			$set_x = $x * $scale_to;
			$set_y = $y * $scale_to;
			while($offset_y < $scale_to) {
				$offset_x = 0;
				while($offset_x < $scale_to) {
					/*$object->log($object->toJSON(['original' => $original[$y][$x]]));*/
					$outputs[$set_y + $offset_y][$set_x + $offset_x] = [$original[$y][$x][0], $original[$y][$x][1], $original[$y][$x][2]];
					$offset_x = $offset_x + 1;
				}
				$offset_y = $offset_y + 1;
			}
			return NULL;
		});

		$object->log('after iterator');

		/*$iterator = new iterator();

		$iterator->iterate($set_scale, function($x, $y) {
			$object->log($object->toJSON(['outputs' => $outputs[$y][$x]]));
		});*/

		return $outputs;
	}

	public function depr() {

		while($counter < 1) {
			
			/*$set_images_paths = $object->randomize_list($images_paths);

			$set_images_paths = $object->splice($set_images_paths, 0, 3);*/

			$set_images_paths = [$images_paths[0]];

			$total_error = 0;

			foreach($set_images_paths as $set_image_path) {

				$from_img = new from_img($set_image_path);

				$mapper = new mapper($from_img->points, 3);

				$clusters = $mapper->get_clusters();

				/*$object->set_pixels($from_img->points);*/
				/*$object->log($set_image_path);

				$object->log($from_img->points);*/

				$object->log('set field');

				$layer_generator->input_field->set_points($from_img->points);

				$object->log('call continue');

				$layer_generator->continue();
				$object->log('call continue');

				/*$object->log('map length');

				$object->log($layer_generator->map->length);*/

				$points = [$layer_generator->res_field->get_field()];


				$point_count = 0;

				foreach($adders as $adder) {
					foreach($points as $y => $rows) {
						foreach($rows as $x => $point) {
							/*if(!$markers[$y][$x]['marked']) {*/
								$total_error = $total_error + $adder->get_error($point);
								$point_count = $point_count + 1;
							/*}*/
						}
					}
				}



				if($points != NULL) {
					$iterator = new iterator();

					$output_points = [];

					$object->log('points');
					/*$object->log($object->toJSON($points));*/ 

					$object->log('points len: '.$points[0]->length);

					$output_points = $iterator->iterate($points[0]->length, function($x, $y) {
						$object->log('x: '.$x.' y: '.$y);
						$item_values = $points[0][$y][$x]; /*[0]*/

						if($object->isset($item_values) && $item_values != NULL) {

							$object->log('item values');
							$object->log($object->toJSON($item_values));

							$res = [$item_values['filtered'], $item_values['original_values'][0][0][1], $item_values['original_values'][0][0][2]]; 


							$error = $item_values['original_values'][0][0][0] - $res[0];

							/*if($error < 0.025) {
								$markers[$y][$x]['marked'] = true;
								$markers[$y][$x]['found_value'] = $item_values['filtered'];
							}*/

							$object->log('item res');
							$object->log($object->toJSON($res));

							return $res;
						}
						return (-1);
					});

					$object->log('output poins len: '.$output_points->length);

					$object->log($object->toJSON($output_points));

					$object->set_pixels($output_points);

					/*return false;*/
				}
			}

			$object->log('total error: '.($total_error / $point_count));
			$counter = $counter + 1;
		}

		if($set_id != NULL) {
			$json = [
				'parameters' => $adders[0]->get_parameters()
			];

			$json = $object->toJSON($json);

			$v = [
				'set_id' => $set_id,
				'jsonvalue' => $json
			];

			if($row != NULL) {
				$object->log('row is not null');
				$v['id'] = $row['id'];
				$object->log($row['id']);
				$object->log($v['id']);
			}

			$insert = $data->statement->generate($v, 'filters');
			$object->log($insert);
			$object->log($object->toJSON($v));
			$data->main_instance->_($insert, $v);
			$object->log('after update insert');
			/*$query = 'UPDATE filters SET json = ? WHERE set_id = ?';
			$data->main_instance->execute($query, [$json, $set_id]);*/
		}



	}


}

class from_img {

	public function __construct($img_path) {
		$img = $object->video->get_img_arr($img_path, -1, -1);
		$object->log('started');

		$random_points = [];


		$object->log($object->toJSON($img));

		foreach($img as $row) {
			$set_row = [];
			foreach($row as $values) {
				$set_values = [];
				$set_values[] = $values['l'];
				$set_values[] = $values['h'];
				$set_values[] = $values['s'];
				$set_row[] = $set_values;
			}
			$random_points[] = $set_row;
		}

		$this->points = $random_points;
	}

	public $points;
}

?>