package xml;

import java.io.PrintStream;
import java.util.LinkedList;

import structures.struct_label;

public class register {

	private String image_name;

	private int image_width;

	private int image_height;	

	private int nregions;

	LinkedList<fields> regions;

	public register (String image_name, int image_width, int image_height, LinkedList<fields> regions, int nregions ) {
		this.image_name = image_name;
		this.image_width = image_width;
		this.image_height = image_height;
		this.nregions = nregions;
		this.regions = regions;
	}

	public String get_image_name() {
		return image_name;
	}

	public int get_image_width() {
		return image_width;
	}

	public int get_image_height() {
		return image_height;
	}

	public int get_nregions () {
		return nregions;
	}

	public fields get_region (int index) {
		return regions.get(index);
	}

	public void write_to_file (LinkedList<fields> list, PrintStream file)
	{
		file.println("  <image>");

		file.println("  <imageName>" + image_name + "</imageName>");

		file.println("  <resolution x=\"" + image_width + "\" y=\"" + image_height + "\"/>");

		if (list.size() <= 0) {
			file.println("    <taggedRectangles/>");
		}
		else {
			file.println("    <taggedRectangles>");			
			for (int i = 0; i < list.size(); i++) {				
				fields f = list.get(i);
				file.println("      <taggedRectangle" + " x=\"" + f.get_x() + "\" y=\"" + f.get_y() + "\" width=\"" + f.get_w() + "\" height=\"" + f.get_h() + "\" text=\"\" score=\"" + f.get_score() + "\"/>");
			}
			file.println("    </taggedRectangles>");
		}
		file.println("  </image>");
	}
	
	public static void write_to_file (LinkedList<struct_label> list, PrintStream file, String image_name, int image_width, int image_height)
	{
		int margin = 2;
		
		file.println("  <image>");

		file.println("  <imageName>" + image_name + "</imageName>");

		file.println("  <resolution x=\"" + image_width + "\" y=\"" + image_height + "\"/>");

		if (list.size() <= 0) {
			file.println("    <taggedRectangles/>");
		}
		else {
			file.println("    <taggedRectangles>");			
			for (int i = 0; i < list.size(); i++) {				
				struct_label f = list.get(i);
				double x = f.xmin - margin;
				double y = f.ymin - margin;
				double h = (f.ymax-f.ymin+1) + 2*margin;
				double w = (f.xmax-f.xmin+1) + 2*margin;
				file.println("      <taggedRectangle" + " x=\"" + x + "\" y=\"" + y + "\" width=\"" + w + "\" height=\"" + h + "\" text=\"\" score=\"0.0\"/>");
			}
			file.println("    </taggedRectangles>");
		}
		file.println("  </image>");
	}	
}