// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codeu.chat.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public final class ServerLog { 
  private HashMap<Integer,String> lines;
  private BufferedReader in;

  public ServerLog(File log) {
    try {
      in = new BufferedReader(new FileReader(log.getAbsolutePath()));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    lines = readFile(in);
  }
  
  
  /**
   * gets the location of the log
   * @return a String that represents the location of the ServerLog
  */
  public static String createFilePath() {
    String workingDirectory = System.getProperty("user.dir");
    return workingDirectory + File.separator + "serverLog.txt";
  }
  
  /**
   * Gets the length of logs
   * @return the number of lines in the ServerLog
  */
  public int getLength() { 
    return lines.size();
  }
  
  /**
   * Reads log line and create the object based on logs
   * @param index of the line in the log
   */
  public void readLine(int index) {
    String toParse = lines.get(index);

    // turns the string from log into an array
    String[] ParArr = toParse.split("_");
    char commandType = toParse.charAt(0);

    if(commandType == 'M') {
      // parse a message
      controller.newMessage(Uuid.parse(ParArr[2]), Uuid.parse(ParArr[1]), 
		            Uuid.parse(ParArr[3]), ParArr[5], ParArr[4] );

      } else if (commandType == 'U') {
      // parse a user
      controller.newUser(Uuid.parse(ParArr[2]), ParArr[1], ParArr[3] );

      } else if (commandType == 'C') {
      // parse a conversation
      controller.newConversation(Uuid.parse(ParArr[2]), ParArr[1], 
		                 Uuid.parse(ParArr[3]), parArr[4] );
    }
  }

  private HashMap<Integer,String> readFile(BufferedReader br) {
    String line;
    HashMap<Integer,String> lines = new HashMap<Integer,String>();
    try {
      int index = 0;
      while ((line = br.readLine()) != null) {
        lines.put(index, line);
        index++;
    }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return lines;
  }
}

@@ -837,17 +837,37 @@
 verLog %7B
- 
+%0A  private File log;%09
 %0A  priva
@@ -930,16 +930,59 @@
 er in;%0A%0A
+  /**%0A   * Constructor for ServerLog%0A   */%0A
   public
@@ -1006,32 +1006,54 @@
 og) %7B%0A    try %7B%0A
+      this.log = log;%0A
       in = new B
@@ -1645,24 +1645,124 @@
 .size();%0A  %7D
+%0A%0A  /**%0A   * getter for Log%0A   * @return File log%0A   */%0A  public File getLog() %7B%0A    return log;%0A  %7D
 %0A  %0A  /**%0A  

check files