package ecies_java;

import java.awt.EventQueue;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;

public class client111 
{
	public static Socket s;
	
	static OutputStream os = null;
    static OutputStreamWriter osw = null;
    static BufferedWriter bw = null;
	
	private JFrame clientfrm;
	private static JTextArea client_screen;
	private JButton btnExit;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					client111 window = new client111();
					window.clientfrm.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
				 
		try {
			 s=new Socket("127.0.0.1",3333);
			 
			 String workingDirectory = System.getProperty("user.dir");
		     String absoluteFilePath = workingDirectory + File.separator +"file1.txt";
		        
		        //Variable to send file
		        OutputStream os = s.getOutputStream();
		        System.out.println("Sending Files...");
		        
		        send_bytes_to_server(os,absoluteFilePath);
		       
		       // socket.close();       
	        }
		catch (Exception e)
		    {System.err.println("Client: checks communication with the server");}
	}

	/**
	 * Create the application.
	 */
	public client111() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		clientfrm = new JFrame();
		clientfrm.getContentPane().setBackground(SystemColor.controlHighlight);
		clientfrm.setBackground(Color.GRAY);
		clientfrm.setTitle("frm1");
		clientfrm.setBounds(100, 100, 593, 450);
		clientfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		clientfrm.getContentPane().setLayout(null);
		
		client_screen = new JTextArea();
		client_screen.setBounds(14, 128, 552, 127);
		clientfrm.getContentPane().add(client_screen);
		
				
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setBounds(14, 387, 77, 25);
		clientfrm.getContentPane().add(btnExit);
		
		JLabel lblClientApplicationFor = new JLabel("User 1");
		lblClientApplicationFor.setFont(new Font("Dialog", Font.BOLD, 30));
		lblClientApplicationFor.setBounds(24, 41, 542, 67);
		clientfrm.getContentPane().add(lblClientApplicationFor);
		
		
		
		
		JButton btnNewButton = new JButton("send file");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
				String workingDirectory = System.getProperty("user.dir");
			        String absoluteFilePath = workingDirectory + File.separator +"ecies_files"+File.separator +"client_keys"+File.separator+"c_key"+File.separator+"cPub.key";
			        
			        //Variable to send file
			        OutputStream os = s.getOutputStream();
			        System.out.println("Sending Files...");
			        
			        send_bytes_to_server(os,absoluteFilePath);
			       
			       // socket.close();
			   
				}
				catch(Exception e)
				{client_screen.append("click");}
				
			}
		});
		btnNewButton.setBounds(24, 305, 117, 25);
		clientfrm.getContentPane().add(btnNewButton);
		
	}
	
		
		// This method to read file bytes and send bytes to server 
	    public static void send_bytes_to_server(OutputStream os,String absoluteFilePath)
	    {
	    	try
	    	{
	    	  File transferFile = new File(absoluteFilePath);
	          byte[] bytearray = new byte[(int) transferFile.length()];
	          FileInputStream fin = new FileInputStream(transferFile);
	          BufferedInputStream bin = new BufferedInputStream(fin);
	          bin.read(bytearray, 0, bytearray.length);
	          //Send file
	          os.write(bytearray, 0, bytearray.length);
	          System.out.println("File transfer complete");

	          BufferedReader r = new BufferedReader(new FileReader(absoluteFilePath));
	          String as = "", line = null;
	          while ((line = r.readLine()) != null) 
	             {as += line + "\n";}
	        
	          client_screen.append("The file was sent contains following data: " + as);
	          r.close();
	          os.flush();
	          fin.close();
	          bin.close();
	          os.close();
	    	 }
	        catch(Exception ex)
	        {System.out.println("Error with sent file bytes");}
	    }
}
