package com.jesuspinar;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Color;
import javax.swing.JEditorPane;
import java.awt.BorderLayout;
import javax.swing.JToggleButton;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JProgressBar;
import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.GridLayout;
import javax.swing.JTextPane;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.FormSpecs;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

public class Windowbuild implements ActionListener{

	private JFrame frame;
	private JTextField tfInputFile;
	private JTextField tfNewName;
	private static JButton btnCopy ;
	private JProgressBar progressBar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Windowbuild window = new Windowbuild();
					window.frame.setVisible(true);
					//Creating event on button click-tap
					btnCopy.addActionListener(window);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

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

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("Enter a file to copy");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane();
		frame.setResizable(false);
		frame.getContentPane().setLayout(null);
		
		JLabel lbInput = new JLabel("File path");
		lbInput.setBounds(116, 30, 130, 16);
		frame.getContentPane().add(lbInput);
		
		tfInputFile = new JTextField();
		tfInputFile.setBounds(116, 47, 232, 26);
		frame.getContentPane().add(tfInputFile);
		tfInputFile.setColumns(10);
		
		JLabel lbNewName = new JLabel("New name");
		lbNewName.setBounds(119, 90, 127, 16);
		frame.getContentPane().add(lbNewName);
		
		tfNewName = new JTextField();
		tfNewName.setColumns(10);
		tfNewName.setBounds(116, 105, 232, 26);
		frame.getContentPane().add(tfNewName);
		
		btnCopy = new JButton("Copy");
		btnCopy.setBounds(116, 168, 232, 47);
		frame.getContentPane().add(btnCopy);
		
		progressBar = new JProgressBar(0,100);
		progressBar.setBounds(116, 143, 232, 20);
		frame.getContentPane().add(progressBar);
		
		
	}

	/**
	 * This method copies the input file in the same directory and adds the name
	 * @return true copied
	 */
	private boolean copyFile() {
		String inputPath = tfInputFile.getText().toString();
		String newName = tfNewName.getText().toString();
		
		if ( inputPath != "" && newName != "") {
			
			File inputFile = new File(inputPath);		
			File outputFile = new File(inputFile.getParent(),newName + getExtension(inputFile));
			
			
			try (FileInputStream fis = new FileInputStream(inputFile);
		             FileOutputStream fos =
		                 new FileOutputStream(outputFile)
		    ){  
		        byte[] content = fis.readAllBytes(); fis.close();
		        
		        double pctLoad =  0.01; // 1 = 100%
		        double loadFraction = (pctLoad * content.length);

		        int loadIndex = 1;
		        int i = 0;

		        for (int j = 0 ; j <= content.length - 1; j++){
		            if (loadIndex >= (int) loadFraction){
		                loadIndex = 0;
		                if (i < progressBar.getMaximum()){
		                	progressBar.setValue(++i);
		                	progressBar.update(progressBar.getGraphics());
		                }
		            }
		            //Action of copy
		            loadIndex++;
		            fos.write(content[j]);
		        }
		    } catch (FileNotFoundException e) {
		        throw new RuntimeException(e);
		    } 
			catch (IOException e) {
		        throw new RuntimeException(e);
		    } 
			//Action complete 
			progressBar.setValue(progressBar.getMaximum());
        	progressBar.update(progressBar.getGraphics());
        	return true;
		}	
		return false;
	}
	
	private static String getExtension(File file){
        String fileName = file.getName();
        return "." + fileName.substring(fileName.lastIndexOf('.') + 1);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		boolean isCopied = copyFile();
		if (isCopied){
			//TODO : dialog - Done!
		}
		else {
			//TODO : dialog - one fields are empty !
		}
	}
	
	 
}

