/* 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.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;

public class CloneActionListener implements ActionListener {
	
	static int counter100;
	static int counter1001;
	static String [] check1;
	static String check[][];
	static String allocation[];
	static String finalstring[][];
	File file = new File("Plasmids/final_output8.txt");
	File file10 = new File("Plasmids/Cloned_data.txt");
	BufferedWriter bw = null;
	BufferedWriter bw10 = null;
	static String final1;
	static String final3;
	static JFrame frame;
	static JPanel panel;
	static String finalstring1;
	public CloneActionListener(JFrame ab,JPanel bc){
		frame = ab;
		panel = bc;
	}
	
	public CloneActionListener(){}
	public CloneActionListener(String ab[][],String bc,String cd,String ef){
		finalstring = ab;
		final1 = bc;
		final3 = cd;
		finalstring1 = ef;
	}
	public CloneActionListener(int a,String b[],String c[][],String d[],int e){
		counter100 = a;
		check1 = b;
		check = c;
		allocation = d;
		counter1001 = e;
	}
	@Override
	public void actionPerformed  (ActionEvent arg0)   {
		// TODO Auto-generated method stub
		System.out.println("hi how r u??");
		FileWriter fw = null;
		FileWriter fw10 = null;
		try {
			fw = new FileWriter(file);
			fw10 = new FileWriter(file10);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		  bw = new BufferedWriter(fw);
		  bw10 = new BufferedWriter(fw10);
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader("Plasmids/encoded.txt"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedReader bufferedReader1 = null;
		try {
			bufferedReader1 = new BufferedReader(new FileReader("Plasmids/output8.txt"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedReader bufferedReader2 = null;
		try {
			bufferedReader2 = new BufferedReader(new FileReader(final1));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedReader bufferedReader3 = null;
		try {
			bufferedReader3 = new BufferedReader(new FileReader("Plasmids/output8.txt"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String line = null;
		String pbr322 = null;
		String line1 = null;
		String encoded = null;
		String line2 = null;
		int counter = 0;
		int length1=0;
		int length2=0;
		int length=0;
		int endlength=0;
		String line3 = null;
		String final2 = "";
		System.out.println("or bhai");
		try {
			while ((line = bufferedReader2.readLine()) != null){
				pbr322 = line;
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(pbr322);
		try {
			while ((line1 = bufferedReader.readLine()) != null){
				encoded = line1;
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(encoded);
		int search = Integer.parseInt(finalstring[0][0]);
		System.out.println(search);
		final2+=pbr322.substring(0, search);
		try {
			while ((line2 = bufferedReader1.readLine()) != null){
				
				String parts [] = line2.split(" ");
				if(search==Integer.parseInt(parts[0])){
					int ab = Integer.parseInt(parts[2]);
					String bc = parts[3];
					String sub = bc.substring(ab, bc.length());
					String subco = complement(sub);
					length=subco.length();
					String final1 = subco+encoded;
					final2+=final1;
					int total = search+final1.length();
					for(int j=0;j<finalstring.length;j++){
						if(Integer.parseInt(finalstring[j][0])>total){
							counter = Integer.parseInt(finalstring[j][0]);
							endlength = counter;
							break;
						}
					}
					
					while ((line3 = bufferedReader3.readLine()) != null){
						String parts1[] = line3.split(" ");
						int gh = Integer.parseInt(parts1[0]);
						if(gh==counter){
							int hh = Integer.parseInt(parts1[2]);
							String hc = parts1[3];
							String sub1 = hc.substring(hh, hc.length());
							String sub1co = complement(sub1);
							length1=counter-total-sub1.length();
							length2=sub1co.length();
							for(int y=0;y<(counter-total-sub1.length());y++){
								final2+='A';
							}
							final2+=sub1co;
							final2+=pbr322.substring(counter);
							try {
								System.out.println(final2);
								bw.write(final2);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}

			}
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JTextArea textArea = new JTextArea(5, 30);
		textArea.setText(final2);
		Highlighter highlighter = textArea.getHighlighter();
	      HighlightPainter painter = 
	             new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
	      
	      try {
			highlighter.addHighlight(search, counter, painter );
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      frame.setLayout(new BorderLayout());
	      JLabel label = new JLabel("Cloned data : ");
			label.setLocation(20,440);
			label.setSize(1000, 14);
			label.setVisible(true);
			String aaf = final2.substring(0, search);
			String bbf = final2.substring(search, counter);
			String ccf = final2.substring(counter, final2.length());
			String gf = String.format("<html>%s<font color='red'>%s</font>%s</html>",aaf , bbf,ccf);
			String gf1 = "<html>"+aaf+"<font color='red'>"+bbf+"</font>"+ccf+"</html>";
			JLabel label1 = new JLabel(gf1);
			label1.setFont(new Font("Serif", Font.BOLD, 10));
			//label1.setText(String.format("<html>%s<font color='red'>%s</font>%s</html>",aaf , bbf,ccf));
			//label1.setLocation(20,270);
			//label1.setSize(1350, 50);
			//label1.setVisible(true);
			JScrollPane scroller = new JScrollPane(label1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			scroller.setLocation(20,460);
			scroller.setSize(1300,50);
			scroller.setVisible(true);
			panel.add(label);
			panel.add(scroller);
			frame.getContentPane().add(panel);
			frame.validate();
	        frame.repaint();
	        try {
				bw10.write(gf1);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        try {
				bw10.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	      DecloneActionListener de = new DecloneActionListener(search,endlength,length,length1,length2,counter100,check,allocation,counter1001);
	      JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
	      
	      Circle3 cr = new Circle3(search,counter,final2.length(),final3,final1,finalstring1);
	      cr.amay();
		
		
	}
	
	public String complement(String ab){
		String bc="";
		for(int i=0;i<ab.length();i++){
			if(ab.charAt(i)=='A'){
				bc+='T';
			}
			else if(ab.charAt(i)=='T'){
				bc+='A';
			}
			else if(ab.charAt(i)=='g'){
				bc+='C';
			}
			else if(ab.charAt(i)=='C'){
				bc+='G';
			}
		}
		return bc;
	}

}
