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

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class DecloneActionListener implements ActionListener {
	static int startlength=0;
	static int endlength=0;
	static int length=0;
	static int length1=0;
	static int length2=0;
	static int counter=0;
	static int counter100=0;
	static String [] check1;
	static String check[][];
	static String allocation[];
	String line;
	String line2;
	static JFrame frame;
	static JPanel panel;
	
	public DecloneActionListener(JFrame ab,JPanel bc){
		frame = ab;
		panel = bc;
	}
	public DecloneActionListener(){}
	
	public DecloneActionListener(int a,int b,int c,int d,int e,int f,String h[][],String i[],int j){
		startlength = a;
		endlength = b;
		length = c;
		length1 = d;
		length2 = e;
		counter = f;
		counter100 = j;
		check = h;
		allocation = i;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		int length1000 = 0;
		String check5 []= new String[counter];
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader("Plasmids/final_output8.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			while ((line2 = bufferedReader.readLine()) != null){
				line = line2;
				
				String ab = line.substring(startlength, endlength);
				
				String bc = ab.substring(0, ab.length()-(length1+length2));
				String ad = bc.substring(length, bc.length());
				length1000 = ad.length();
				System.out.println(length+" "+ad);
				int i=0;
				for(int g=0;g<ad.length()-40;g+=40){
					//System.out.println("hi");
					//System.out.println(ad);
					check5[i++] = ad.substring(g, g+40);
			        //System.out.println(check[i++]);	
			        }
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String final1 = "";
		int counter5=1;
		int counter6=0;
		System.out.println("heya "+counter);
		String check2 [] = new String[500];
		int completed [] = new int[counter];
		int nice=0;
		for(int k=0;k<counter;k++){
				System.out.println(k);
				final1="";
			 String ab = check5[k];
			 System.out.println(ab);
			 if(ab==null){
				 continue;
			 }
			 //String cd="";
			 char bc = ab.charAt(0);
				if(bc=='G'){
					final1+='0';
				}
				else{
				    final1+='1';
				}
				//System.out.println(cd);
				//System.out.println(ab);
				for(int i=1;i<ab.length();i++){
					char ef = ab.charAt(i-1);
					char gh = ab.charAt(i);
					//System.out.println(ef+" "+gh);
					if(ef=='A'){
						if(gh=='G'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='T'){
						if(gh=='C'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='G'){
						if(gh=='T'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='C'){
						if(gh=='A'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
				}
				
			System.out.println("hey");
			System.out.println(final1);
			String seed = final1.substring(0, 8);
			int h=0;
			for(h=0;h<seed.length();h++){
				if(seed.charAt(h)=='0'){
					continue;
				}
				else{
					break;
				}
			}
			String seed1 = seed.substring(h, seed.length());
			int number = Integer.parseInt(seed1,2);
			System.out.println("seed "+number);
			
			String decode1 = allocation[number];
			String parts[] = decode1.split(",");
			int ap = Integer.parseInt(parts[0]);
			int aq = Integer.parseInt(parts[1]);
			int ar = Integer.parseInt(parts[2]);
			int as = Integer.parseInt(parts[3]);
			
			System.out.println("three numbers "+ap+" "+aq+" "+ar+" "+as);
			for(int gh=0;gh<counter;gh++){
				System.out.print(completed[gh]+" ");
			}
			String ac = final1.substring(8, 40);
			String ad="";
			String ae="";
			String af="";
			
			if(completed[ap]==0){
			
			//String ad = check1[counter5%counter][0];
			//String ae = check1[(counter5+1)%counter][0];
				counter6=ap;
				ad = check[aq][0];
				ae = check[ar][0];
				af = check[as][0];
				completed[ap]=1;
			
			}
			else if (completed[aq]==0){
				counter6=aq;
				ad = check[ap][0];
				ae = check[ar][0];
				af = check[as][0];
				completed[aq]=1;
			}
			
			else if (completed[ar]==0){
				counter6=ar;
				ad = check[ap][0];
				ae = check[aq][0];
				af = check[as][0];
				completed[ar]=1;
			}
			
			else if (completed[as]==0){
				counter6=as;
				ad = check[ap][0];
				ae = check[aq][0];
				af = check[ar][0];
				completed[as]=1;
			}
			else if (completed[as]==1 && completed[ap]==1 && completed[aq]==1 && completed[ar]==1){
				continue;
			}
			
			StringBuilder sb = new StringBuilder();

		    for (int i = 0; i < ac.length(); i++) {
		    	//System.out.println(ac.length()+" "+ad.length()+" "+ae.length());
		        sb.append(charOf(bitOf(ac.charAt(i)) ^ bitOf(af.charAt(i)) ^ bitOf(ad.charAt(i))
		                ^ bitOf(ae.charAt(i))));
		    }

		    String result = sb.toString();
			check2[counter6] = result;
			counter5++;
			final1="";
		}
		System.out.println("kaisa hai");
		String z="";
		for(int g=0;g<counter100;g++){
			z+=check2[g];
		}
		String output = "";
		for(int i = 0; i <= z.length() - 8; i+=8)
		{
		    int k = Integer.parseInt(z.substring(i, i+8), 2);
		    if(k==0)
		    	continue;
		    //System.out.println(k);
		    output += (char) k;
		} 
		BufferedWriter bw = null;
        FileWriter fw = null;
        try {
			fw = new FileWriter("Plasmids/Decloned_data.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        bw = new BufferedWriter(fw);
        try {
			bw.write(output);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frame.setLayout(new BorderLayout());
		JLabel label = new JLabel("Decloned data : ");
		label.setLocation(20,560);
		label.setSize(1000, 14);
		label.setVisible(true);
		JLabel label1 = new JLabel(output);
		label1.setFont(new Font("Serif", Font.BOLD, 12));
		//label1.setText(String.format("<html>%s<font color='red'>%s</font>%s</html>",aaf , bbf,ccf));
		//label1.setLocation(20,580);
		//label1.setSize(1350, 20);
		//label1.setVisible(true);
		JScrollPane scroller = new JScrollPane(label1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroller.setLocation(20,580);
		scroller.setSize(1300,50);
		scroller.setVisible(true);
		panel.add(label);
		panel.add(scroller);
		frame.getContentPane().add(panel);
		frame.validate();
        frame.repaint();
		//int charCode = Integer.parseInt(content, 2);
		//String str = new Character((char)charCode).toString();
		System.out.println("Final String : "+output+" "+length1000);
		Gel g = new Gel(length1000);
		g.amay();
		
	}
	
	private static boolean bitOf(char in) {
	    return (in == '1');
	}

	private static char charOf(boolean in) {
	    return (in) ? '1' : '0';
	}

}
