/* 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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class DecodeActionListener implements ActionListener {

	static JButton importbutton;
	static JButton encode;
	static JButton declone;
	static JFrame frame;
	static JPanel panel;
	static JLabel lable;
	static JLabel lbl;
	public DecodeActionListener(JButton ab,JButton bc,JButton cd,JFrame de,JPanel ef,JLabel l1,JLabel l2){
		importbutton = ab;
		encode = bc;
		declone = cd;
		frame = de;
		panel = ef;
		lable = l1;
		lbl = l2;
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//String encoded = JOptionPane.showInputDialog(null, "Enter Encoded data (Your own DNA Sequence)");
		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 e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            String line1="";
            String encoded = "";
            try {
				while ((line1 = bufferedReader.readLine()) != null){
					encoded+=line1;
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		BufferedWriter bw = null;
		FileWriter fw = null;
		try {
			fw = new FileWriter("Plasmids/encoded.txt");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		bw = new BufferedWriter(fw);
		try {
			bw.write(encoded);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			bw.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		importbutton.setEnabled(false);
		encode.setEnabled(false);
		declone.setEnabled(false);
		
		frame.setLayout(new BorderLayout());
		JLabel label = new JLabel("Encoded Data :");
		label.setLocation(20,200);
		label.setSize(1000, 14);
		label.setVisible(true);
		JLabel label1 = new JLabel(encoded);
		label1.setFont(new Font("Serif", Font.BOLD, 10));
        //label1.setLocation(20,170);
		//label1.setSize(1300,20);
		//label1.setVisible(true);
		JScrollPane scroller = new JScrollPane(label1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroller.setLocation(20,220);
		scroller.setSize(1300,50);
		scroller.setVisible(true);
		panel.add(label);
		panel.add(scroller);
		frame.remove(lable);
		frame.remove(lbl);
		frame.getContentPane().add(panel);
		frame.validate();
        frame.repaint();
		
		JOptionPane.showMessageDialog(null, "Your encoded data has been successfully uploaded");
		System.out.println(encoded);
		
	}
	
	}	

}
