/* Author: Amay Agrawal
 * Project: Bacteria Cloud
 * Mentor: Prof. Manish K Gupta
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class McsActionListener implements ActionListener {

	static String final1;
	static String final2;
	static String final3;
	
	public McsActionListener(){}
	
	public McsActionListener(String ab,String bc,String cd){
		final1 = ab;
		final2 = bc;
		final3 = cd;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		String line = null;
		String line1 = null;
		String [] parts;
		String check [] = new String[300];
		int counter = 0;
		int counter5 = 0;
		int counter10 = 0;
		int counter11 = 0;
		//System.out.println("final2 is "+final2);
		BufferedReader bufferedReader2 = null;
		try {
			bufferedReader2 = new BufferedReader(new FileReader(final2));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int k=0;
		String finalstring = null;
		String finalstring1=null;
		int plasmidlength=0;
		File file = new File("Plasmids/output8.txt");
		BufferedWriter bw = null;
		int counter2=0;
		int arr[] = new int[300];
		for(int m=0;m<199;m++){
			arr[m] = 100000;
		}
		// TODO Auto-generated method stub
		try {
			
			FileWriter fw = new FileWriter(file);
			  bw = new BufferedWriter(fw);
			BufferedReader bufferedReader = new BufferedReader(new FileReader(final1));
			
			BufferedReader bufferedReader1 = new BufferedReader(new FileReader(final3));
			while ((line = bufferedReader.readLine()) != null){
				plasmidlength = line.length();
				//System.out.println(line.substring(2109, 2117));
				while ((line1 = bufferedReader1.readLine()) != null){
						parts = line1.split(" ");
						int length = parts[1].length();
						//System.out.println("hi "+parts[2]);
						int abc = Integer.parseInt(parts[2]);
						for(int i=0;i<=(line.length() - length);i++){
							String ab = line.substring(i,i+length);
							for(int h=0;h<length;h++){
								if(parts[1].charAt(h) == 'N'){
									//System.out.println(parts[1]);
									counter++;
									continue;
								}
								else{
									if(ab.charAt(h) == parts[1].charAt(h)){
										counter++;
									}
									else{
										break;
									}
								}
							}
							if(counter == length){
								System.out.println("kilo "+k);
								arr[k] = i+abc;
								check[k++] = parts[0];
								bw.write((i+abc)+" "+parts[0]+" "+parts[2]+" "+parts[1]+"\n");
								//System.out.println("i = "+(i+abc)+" enzyme = "+parts[0]);
							}
							counter=0;
						}
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("jkkkj"+arr[0]);
		int arr2 []= new int[k];
		for(int m=0;m<k;m++){
			arr2[m]=arr[m];
		}
		Arrays.sort(arr2);
		for(int m=0;m<k;m++){
			if(!(counter10 == arr2[m])){
				counter11++;
			}
			counter10 = arr2[m];
		}
		System.out.println("jbjbjhhhhhh "+counter11);
		int arr1[] = new int[counter11];
		
		for(int m=0;m<k;m++){
			if(!(counter2 == arr2[m])){
			arr1[counter5]=arr2[m];
			System.out.println(arr1[counter5++]);
			}
			counter2 = arr2[m];
			
		}
		String check1[] = new String[counter5];
		System.out.println("kuhkujk "+arr1[1]);
		for(int g=0;g<counter11;g++){
			int f = arr1[g];
			for(int h=0;h<k;h++){
				if(arr[h] == f){
					check1[g] = check[h];
					break;
				}
			}
		}
		
		String finalstring5[][] = new String[counter5][2];
		for(int g=0;g<counter5;g++){
			//System.out.println(arr1[g]+" "+check1[g]);
			finalstring5[g][0] = Integer.toString(arr1[g]);
			finalstring5[g][1] = check1[g];
		}
		
		for(int h=0;h<counter5;h++){
			System.out.println(finalstring5[h][0]+" "+finalstring5[h][1]);
		}
		
		try {
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			while ((finalstring = bufferedReader2.readLine()) != null){
				finalstring1 = finalstring;
				System.out.println("finalstring is "+finalstring);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("tyuhj "+finalstring1);
		CloneActionListener c1 = new CloneActionListener(finalstring5,final1,final3,finalstring1);
		Circle c = new Circle(arr1,check1,k,plasmidlength,final1,finalstring1);
        c.amay();
		//Data_Insertion d = new Data_Insertion(arr);
		//d.insertsequence();
		
	}

}
