package tachyon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

import tachyon.thrift.ClientFileInfo;
import tachyon.thrift.FileDoesNotExistException;
import tachyon.thrift.InvalidPathException;

public class WebInterfaceBrowseServlet extends HttpServlet {
  private static final long serialVersionUID = 6121623049981468871L;

  private MasterInfo mMasterInfo;

  /**
   * Class to make referencing file objects more intuitive. Mainly to avoid implicit association
   * by array indexes.
   */
  public class UiFileInfo implements Comparable<UiFileInfo> {
    private final int ID;
    private final String NAME;
    private final String ABSOLUATE_PATH;
    private final long SIZE;
    private final boolean IN_MEMORY;
    private final boolean IS_DIRECTORY;

    private UiFileInfo(ClientFileInfo fileInfo) {
      ID = fileInfo.getId();
      NAME = fileInfo.getName();
      ABSOLUATE_PATH = fileInfo.getPath();
      SIZE = fileInfo.getSizeBytes();
      IN_MEMORY = fileInfo.isInMemory();
      IS_DIRECTORY = fileInfo.isFolder();
    }

    public int getId() {
      return ID;
    }

    public String getName() {
      if (ABSOLUATE_PATH.equals(Config.SEPARATOR)) {
        return "root";
      } else {
        return NAME;
      }
    }

    public String getAbsolutePath() {
      return ABSOLUATE_PATH;
    }

    public long getSize() {
      return SIZE;
    }

    public boolean getInMemory() {
      return IN_MEMORY;
    }

    public boolean getIsDirectory() {
      return IS_DIRECTORY;
    }

    @Override
    public int compareTo(UiFileInfo o) {
      return ABSOLUATE_PATH.compareTo(o.getAbsolutePath());
    }
  }

  public WebInterfaceBrowseServlet(MasterInfo masterInfo) {
    mMasterInfo = masterInfo;
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {
    request.setAttribute("debug", Config.DEBUG);

    request.setAttribute("masterNodeAddress", mMasterInfo.getMasterAddress().toString());
    request.setAttribute("invalidPathError", "");
    List<ClientFileInfo> filesInfo = null;
    String currentPath = request.getParameter("path");

    if (currentPath.isEmpty()) {
      currentPath = "/";
    }
    request.setAttribute("currentPath", currentPath);

    try {
      CommonUtils.validatePath(currentPath);
      setPathDirectories(currentPath, request);
      filesInfo = mMasterInfo.getFilesInfo(currentPath);
    } catch (FileDoesNotExistException fdne) {
      request.setAttribute("invalidPathError", "Error: Invalid Path " + fdne.getMessage());
      getServletContext().getRequestDispatcher("/browse.jsp").forward(request, response);
      return;
    } catch (InvalidPathException ipe) {
      request.setAttribute("invalidPathError", "Error: Invalid Path " + ipe.getLocalizedMessage());
      getServletContext().getRequestDispatcher("/browse.jsp").forward(request, response);
      return;
    }

    List<UiFileInfo> fileInfos = new ArrayList<UiFileInfo>(filesInfo.size());
    for (ClientFileInfo fileInfo : filesInfo) {
      fileInfos.add(new UiFileInfo(fileInfo));
    }
    Collections.sort(fileInfos);
    request.setAttribute("fileInfos", fileInfos);

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/browse.jsp");
    if (rd != null) {
      rd.forward(request, response);
    }
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    return;
  }

  /**
   * This function sets the fileinfos for folders that are in the path to the current directory.
   * 
   * @param path
   * @param request
   * @throws FileDoesNotExistException
   * @throws InvalidPathException
   */
  private void setPathDirectories(String path, HttpServletRequest request) 
      throws FileDoesNotExistException, InvalidPathException {
    request.setAttribute("currentDirectory", new UiFileInfo(mMasterInfo.getFileInfo(path)));
    if (path.equals(Config.SEPARATOR)) {
      request.setAttribute("pathInfos", new UiFileInfo[0]);
      return;
    }

    String[] splitPath = path.split(Config.SEPARATOR);
    UiFileInfo[] pathInfos = new UiFileInfo[splitPath.length-1];
    String currentPath = Config.SEPARATOR;
    pathInfos[0] = new UiFileInfo(mMasterInfo.getFileInfo(currentPath));
    for (int i = 1; i < splitPath.length - 1; i ++) {
      currentPath = currentPath + splitPath[i];
      pathInfos[i] = new UiFileInfo(mMasterInfo.getFileInfo(currentPath));
      currentPath = currentPath + Config.SEPARATOR;
    }
    request.setAttribute("pathInfos", pathInfos);

    for (UiFileInfo pathi : pathInfos) {
      System.out.println("=====================================================");
      System.out.println(pathi.getName());
    }

    return;
  }
}
@@ -294,48 +294,8 @@
 nse;
-%0Aimport javax.servlet.RequestDispatcher;
 %0A%0Aim
@@ -3437,31 +3437,8 @@
 %0A   
- RequestDispatcher rd =
  get
@@ -3493,40 +3493,8 @@
 sp%22)
-;%0A    if (rd != null) %7B%0A      rd
 .for
@@ -3518,22 +3518,16 @@
 ponse);%0A
-    %7D%0A
   %7D%0A%0A  @
@@ -4330,17 +4330,19 @@
 h.length
--
+ - 
 1%5D;%0A    
@@ -4742,183 +4742,8 @@
 s);%0A
-%0A    for (UiFileInfo pathi : pathInfos) %7B%0A      System.out.println(%22=====================================================%22);%0A      System.out.println(pathi.getName());%0A    %7D%0A%0A
     

clean up