import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Collect extends JFrame implements ActionListener {
        String studentDirName;
        File studentDir;
        String collectDirName;
        File collectDir;
        String hwName;
        String logFileName = "log.txt";
	File logFile;
	PrintStream logOut;
	String submitDirName = "submit";
	
	String[] students;
	
	JTextField studentDirField = new JTextField(15);
	JTextField collectDirField = new JTextField(15);
	JTextField hwField = new JTextField(15);
	JButton collectButton = new JButton("Collect");
	
	Container contentPane = getContentPane();
	
	public Collect() {

	     contentPane.setLayout(new GridLayout(0,1));
	     contentPane.add(new JLabel("The directory contains the students' working directories: "));
	     contentPane.add(studentDirField);
	     contentPane.add(new JLabel("The directory used to collect the homework: "));
	     contentPane.add(collectDirField);
	     contentPane.add(new JLabel("The file name you want to collect"));
	     contentPane.add(hwField);
	     contentPane.add(collectButton);
	     collectButton.addActionListener(this);
	}
	
	 public void actionPerformed(ActionEvent e) {
		     studentDirName = studentDirField.getText();
		     collectDirName = collectDirField.getText();
		     hwName = hwField.getText();
		     setUpDirs();
		     collectAllStudents();
		 }
	   
	public void setUpDirs() {
	    studentDir = new File(studentDirName);
	    collectDir = new File(collectDirName);
	    collectDir.mkdirs();
	    logFile = new File(collectDir, logFileName);
	    try {
	    logOut = new PrintStream(new FileOutputStream(logFile));
	   } catch (Exception ex) {
	      System.out.println("logOut file problem");
	      return;
	   }
	}
	
	
       public void collectAllStudents() {
	   String[] students = studentDir.list();
	   File copyFrom = null, copyTo = null;
	   for(int i = 0; i < students.length; i++) {
	       try {
	          copyFrom = new File(studentDir, students[i] + "/submit/" + hwName);
		  new File(collectDir, students[i]).mkdirs();
		  copyTo = new File(collectDir,  students[i] + "/" + hwName);
		  copyFile(copyFrom, copyTo);
		  logOut.println(copyFrom + " is copied to " + copyTo + " successfully");
	       } catch(Exception ex) {
		   logOut.println(copyFrom + " failed to be copied");
	       }
	   }
		   
       }
	   
	
	
      public  void copyFile(File copyFrom, File copyTo) throws Exception {
        //System.out.println("In copyFile : copying " + copyFrom + " to " + copyTo);
	    BufferedInputStream  in = new BufferedInputStream(new FileInputStream(copyFrom));
	    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(copyTo));
	
	    byte[] chs = new byte[64];
	    while(in.read(chs) != -1) {
	        out.write(chs);
	    }
	
	    in.close();
	    out.close();
    } 
    
    
        public static void main(String[] args) {
            JFrame f = new Collect();
	    f.pack();
	    f.setVisible(true);
	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
}
