package artistmetrics.actions;

// initial non-eclipse version

import java.io.*;
import java.util.ArrayList;
import java.util.Date;

public class Log{
	
	private String fileName = "";
	public Log() {}
	public Log(String fN) {
		fileName = fN;
	}
	
	/**
	 * Writes the given text to the log
	 * @param text the text to be written to the log
	 */
	public void write(String text) {
		Date theDate = new Date();
		  try {
		    String toWrite = theDate.toString() + " ~ " + text + "\n";
		    FileWriter fout = new FileWriter(fileName, true);
		    BufferedWriter out = new BufferedWriter(fout);
		    out.write(toWrite);
		    out.close();
		  }
		    catch (IOException e) {
		    System.err.println("error: " + e.getMessage());		
		  }
		}

	/**
	 * Gets the 10 most recent entries to the log file.
	 */
	public String getRecent() {
		String a = "";
		int Lines;
		ArrayList<String> RecentChanges = new ArrayList<String>();
		try {
	    FileReader fr = new FileReader(".artistmetricslog");
	    BufferedReader br = new BufferedReader(fr);	    
	    a = br.readLine();
	    while (a != null )
	    {
	    	RecentChanges.add(a);
	    	a = br.readLine();
	    }
	    Lines = RecentChanges.size();
	    int b = Lines - 20;
	    a = "";
	    for (int i = b ; i < Lines; i++)
	    {
	    	a += RecentChanges.get(i);
	    	a +="\n";  	
	    }
	    br.close();
	  }
	  catch (IOException e) {
	    System.err.println("Unable to read from file");		
	  }
	  
	return a;
	}

}