package IAMdatabase;

import ij.ImagePlus;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.FileWriter;

import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.raster.JimiRasterImage;


public class FindAuthor {
	
	ArrayList formId = new ArrayList();
	ArrayList writerId = new ArrayList();
	
	public FindAuthor() {
	}

	public void run(){
		System.out.println("start");
		//formID - nazvy vsetkym obrazkov ktore su momentalne v adresari
		try{
			//FileReader fr = new FileReader("pics\\osekane\\fungujeLineSegm\\formID.txt");
			FileReader fr = new FileReader("pics\\testing\\formID.txt");
			//FileReader fr = new FileReader("pics\\osekane\\fungujeLineSegm\\new\\formID.txt");
			//FileReader fr = new FileReader("pics\\osekane\\formID.txt");
			//FileReader fr = new FileReader("pics\\formID.txt");
			BufferedReader br = new BufferedReader(fr);
			String line = "";
			//int u = 0;
			while ( (line = br.readLine()) != null ) {
			//while (u <= 100) {
				//u++;
				formId.add(line);
			//	System.out.println(line);
			}
			fr.close();
		} catch (Exception e) {
			System.err.println("subor sa nenasiel!");
		}	
		
		IAM.run();
		for (int i = 0; i < formId.size(); i++) {
			for (int k = 0; k < IAM.getFormId().size(); k++) {
				
				if (((String)(formId.get(i))).compareTo( ((String)(IAM.getFormId().get(k))).concat(".png") ) == 0) {
					System.out.println(((String)(formId.get(i)))+" "+(String)(IAM.getWriterId().get(k)));
					writerId.add((String)(IAM.getWriterId().get(k)));
					
					  File f = new File("pics\\"+((String)(formId.get(i))));
					  
					  try{
			            Image image = Jimi.getImage(f.toString());
			            String newName = ((String)(IAM.getWriterId().get(k))).concat("-").concat((String)(formId.get(i)));
			            f = new File("pics\\"+newName);
				 		Jimi.putImage(image, f.toString());
			 			   
					  } catch (Exception e) {
						System.out.println("zle "+e.getMessage());
					}
					  
					//selectWriterID[0] = (String)(IAM.getWriterId().get(k));
				}
			}

		}	
				
		System.out.println("Mam "+formId.size()+" obrazkov.");
		
		ArrayList writerIdnew = new ArrayList();
		for (int i = 0; i < writerId.size(); i++) {
			boolean isThere = false;
			for (int j = 0; j < writerIdnew.size(); j++) {
				if ( ((String)(writerIdnew.get(j))).compareTo((String)(writerId.get(i)))  == 0)
					isThere = true;
			}
			if (!isThere)
				writerIdnew.add((String)(writerId.get(i)));
		}
		
/*			for (int i = 0; i < writerIdnew.size(); i++) {
			System.out.println(writerIdnew.get(i));
		}*/
		
		System.out.println("Mam "+writerIdnew.size()+" autorov.");		
		
	}
	
	
	public static void main(String[] args) {
		FindAuthor f = new FindAuthor();
		f.run();
	}
	
}
