package tcra.logger;

import java.io.*;
import java.util.Calendar;
import tcra.gui.MyTableModel;

public class LogWriter
{
   private static LogWriter instance = null;
   private FileWriter fw;
   private String logPath;
   
   private LogWriter()
   {
      logPath = "tcra_log"; // default to working directory
      fw = null;
   } // end ctor
   
   public Object clone() { return instance; }
   
   public static LogWriter getInstance()
   {
      if(instance != null) return instance;
         instance = new LogWriter();
      return instance;
   } // end getInstance()
   
   public void logFileOpened(String name)
   {
      try
      {
         String temp = "001: " +
                       Calendar.getInstance().getTime().toString() +
                       ": User opened file " + name + "\n";
         fw = new FileWriter(logPath, true);         
         fw.write(temp, 0, temp.length());
         fw.flush();
         fw.close();
      } // end try
      catch(IOException ioe)
      {
         System.out.println("LogFileOpened: Could not open "+
                            "tcra_log for writing");
         System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logFileOpened(String)
   
   public void logMethodSelect(String name)
   {
      try
      {
         String temp = "010: " +
                       Calendar.getInstance().getTime().toString() +
                       ": User selected method " + name + "\n";
         fw = new FileWriter(logPath, true);         
         fw.write(temp, 0, temp.length());
         fw.flush();
         fw.close();
      } // end try
      catch(IOException ioe)
      {
         System.out.println("logMethodSelect: Could not open "+
                            "tcra_log for writing");
         System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logMethodSelect(String)
   
   public void logInput(MyTableModel mtm_input, boolean correct)
   {
      try
      {
         fw = new FileWriter(logPath, true);
         
         String temp;
         temp = "1" +
                String.format("%1$02d",mtm_input.getRowCount()) + ": "
                + Calendar.getInstance().getTime().toString() + ":\n";
         fw.write(temp, 0, temp.length());
         for(int i = 0; i < mtm_input.getRowCount(); ++i)
         {
            temp = mtm_input.getValueAt(i,0) + " = " +
                   mtm_input.getValueAt(i,1) + "\n";
            fw.write(temp, 0, temp.length());
         } // end for
         temp = "Test case was deemed " + correct + "\n";
         fw.write(temp, 0, temp.length());
         fw.flush();
         fw.close();
      } // end try
      catch(IOException ioe)
      {
         System.out.println("logInput: Could not open tcra_log "+
                            "for writing");
         System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logInput(MyTableModel)
   
   public void logTimestamp()
   {
      try
      {
         fw = new FileWriter(logPath, true);
         
         String temp = "000: " +
                       Calendar.getInstance().getTime().toString() +
                       ": Program executed\n";
         fw.write(temp, 0, temp.length());
         fw.flush();
         fw.close();
      } // end try
      catch(IOException ioe)
      {
         System.out.println("logTimestamp: Could not open tcra_log "+
                            "for writing");
         System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logTimestamp()
   
   public boolean logHasID()
   {
      try
      {
         BufferedReader fr = new BufferedReader(new FileReader("tcra_log"));
         String idLine = fr.readLine();
         fr.close();
         
         if(idLine == null) return false;
         
         if(idLine.substring(0, 3).equals("id:"))
            return true;
         else return false;
      } // end try
      catch(IOException ioe)
      {
         return false; // if this occurs, then tcra_log does not exist
         //System.out.println("logHasID: Could not open tcra_log "+
         //                   "for reading");
         //System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logHasID()
   
   public void logID(String id)
   {
      try
      {
         // this MUST start a file,
         // so I option to not append to the file
         fw = new FileWriter(logPath);
         
         String temp = "id: "+id+"\n";
         fw.write(temp);
         fw.flush();
         fw.close();
      } // end try
      catch(IOException ioe)
      {
         System.out.println("logID: Could not open tcra_log "+
                            "for writing");
         System.out.println(ioe.getMessage());
      } // end catch(IOException)
   } // end logID()
   
   /**
    * This is for the SFTP function so it can prepend the ID to the filename on submission.
    * @return ID the user entered on first run
    */
   public String getID()
   {
      try
      {
         BufferedReader fr = new BufferedReader(new FileReader("tcra_log"));
         String idLine = fr.readLine();
         fr.close();
         
         if(idLine.substring(0, 3).equals("id:"))
            return idLine.substring(4);
         else return "BAD_LOG";
      } // end try
      catch(IOException ioe)
      {
         return "BAD_LOG"; // if this occurs, then tcra_log does not exist
         //System.out.println("logHasID: Could not open tcra_log "+
         //                   "for reading");
         //System.out.println(ioe.getMessage());
      } // end catch(IOException)
   }
} // end class
