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

import javax.swing.*;
public class ImportActionListener extends JPanel implements ActionListener {

	static JFrame frame;
	static JPanel panel;
	static JLabel lable;
	static JLabel lable2;
	static JButton decode;
	static JLabel lblFileLink;
	static JLabel lblFileLink1;
	static JLabel lblFileLink2;
	static JLabel lblFileLink3;
	
	public ImportActionListener(){
		
	}
	
	public ImportActionListener(JFrame ab,JButton gh,JPanel p,JLabel l1,JLabel l2){
		frame = ab;
		decode = gh;
		panel = p;
		lable = l1;
		lable2 = l2;		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		JFileChooser fc=new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc = new JFileChooser();
        int value = fc.showOpenDialog(null);
        if(value == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            BufferedReader bufferedReader = null;
            try {
    			bufferedReader = new BufferedReader(new FileReader(f));
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            String line1="";
            String line2 = "";
            try {
				while ((line2 = bufferedReader.readLine()) != null){
					line1+=line2;
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            decode.setEnabled(false);
            frame.setLayout(new BorderLayout());
    		JLabel label = new JLabel("Imported Data from file :");
    		label.setLocation(20,100);
    		label.setSize(1000, 14);
    		//label.setOpaque( true );
    		//label.setForeground(Color.red);
    		label.setVisible(true);
    		JLabel label1 = new JLabel(line1);
    		label1.setFont(new Font("Serif", Font.BOLD, 12));
    		//label1.setBorder(BorderFactory.createLineBorder(Color.black));
    		//label1.setLocation(20,120);
    		//label1.setSize(1000, 14);
    		//label1.setVisible(true);
    		JScrollPane scroller = new JScrollPane(label1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		scroller.setLocation(20,120);
    		scroller.setSize(1300,50);
    		scroller.setVisible(true);
    		panel.add(label);
    		panel.add(scroller);
    		frame.remove(lable);
    		frame.remove(lable2);
    		frame.getContentPane().add(panel);
    		
    		//frame.setUndecorated(true); //transparent 
    		frame.validate();
            frame.repaint();
    		//frame.pack();
    		//frame.setTitle("abcd");
    		//frame.getContentPane().add(label);
            //frame.revalidate();
            //JPanel pnlButton = new JPanel();
            //JButton importbutton = new JButton("Import File");//frame.setVisible(true);
            //importbutton.setBounds(100, 100, 100, 300);
            //pnlButton.add(importbutton);
            //frame.removeAll();
            //frame.getContentPane().add(pnlButton);
            
            //frame.setSize(200, 200);
            EncodeActionListener e = new EncodeActionListener(f);
            JOptionPane.showMessageDialog(null, "Your file has been successfully imported. Please press \"Encode\" to encode the imported file." );
        }
        

	}
	
	private static boolean bitOf(char in) {
	    return (in == '1');
	}

	private static char charOf(boolean in) {
	    return (in) ? '1' : '0';
	}
	
	public static String convert(String ab){
		String cd="";
		char bc = ab.charAt(0);
		if(bc=='0'){
			cd+='A';
		}
		else{
			cd+='T';
		}
		
		for(int i=1;i<ab.length();i++){
			char ef = cd.charAt(i-1);
			char gh = ab.charAt(i);
			if(ef=='A'){
				if(gh=='1'){
					cd+='G';
				}
				else{
					cd+='T';
				}
			}
			else if(ef=='T'){
				if(gh=='1'){
					cd+='C';
				}
				else{
					cd+='A';
				}
			}
			else if(ef=='G'){
				if(gh=='1'){
					cd+='T';
				}
				else{
					cd+='C';
				}
			}
			else if(ef=='C'){
				if(gh=='1'){
					cd+='A';
				}
				else{
					cd+='G';
				}
			}
			
		}
		return cd;
		
	}
	
	public static void decode (int counter,String [] check,String check1[][],String allocation[]){
		System.out.println("In decoder");
		for(int yu=0;yu<25;yu++){
			System.out.println(check[yu]);
		}
		String final1 = "";
		int counter5=1;
		int counter6=0;
		System.out.println("heya "+counter);
		String check2 [] = new String[100];
		int completed [] = new int[counter];
		int nice=0;
		for(int k=0;k<25;k++){
				System.out.println(k);
				final1="";
			 String ab = check[k];
			 if(ab==null){
				 continue;
			 }
			 //String cd="";
			 char bc = ab.charAt(0);
				if(bc=='A'){
					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 = check1[aq][0];
				ae = check1[ar][0];
				af = check1[as][0];
				completed[ap]=1;
			
			}
			else if (completed[aq]==0){
				counter6=aq;
				ad = check1[ap][0];
				ae = check1[ar][0];
				af = check1[as][0];
				completed[aq]=1;
			}
			
			else if (completed[ar]==0){
				counter6=ar;
				ad = check1[ap][0];
				ae = check1[aq][0];
				af = check1[as][0];
				completed[ar]=1;
			}
			
			else if (completed[as]==0){
				counter6=as;
				ad = check1[ap][0];
				ae = check1[aq][0];
				af = check1[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<18;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;
		}  
		//int charCode = Integer.parseInt(content, 2);
		//String str = new Character((char)charCode).toString();
		System.out.println("Final String : "+output);
	}


}
