<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.idca.fsim.searchers.files.models.Host" %>
<%@ page import="com.idca.fsim.searchers.files.models.File" %>
<%@ page import="com.idca.fsim.searchers.files.models.Path" %>
<%@ page import="com.idca.fsim.searchers.files.FilesListener" %>
<%
	Object model = session.getAttribute("model");
	Path path = null;
	Host host = null;
	
	Collection paths = null;
	List files = null;
	
	if(model instanceof File)
	{
		File file = (File)model;
		path = file.getPath();
		paths = path.getChilderen();
		files = FilesListener.getPathManager().getFiles(path);
	}
	
	if(model instanceof Path)
	{
		path = (Path)model;
		paths = path.getChilderen();
		files = FilesListener.getPathManager().getFiles(path);
	}
	if(model instanceof Host)
	{
		host = (Host)model;
		if (host.getRootPaths().iterator().hasNext()) {
			paths = ((Path)(host.getRootPaths().iterator().next())).getChilderen();
			files = FilesListener.getPathManager().getFiles(((Path)(host.getRootPaths().iterator().next())));
		} else {
			paths = new ArrayList();
			files = new ArrayList();
		}
	}
	
	request.setAttribute("paths",paths);
	request.setAttribute("files",files);
%>
<table width="100%">
	<thead>
		<tr>
			<td width="45%"></td>
			<td width="25%"></td>
			<td width="25%"></td>
		</tr>
	</thead>
	<tbody>
		<%  if (path!=null && path.getParent()!=null) {  %>
				<tr>
				<td><span class="resultDetails"><a href="<%
						out.write("ppage?ss="+request.getParameter("ss")+"&rid="+path.getParent().getID()+"&up=path_browse");			
					%>">..</a></span></td>
				<td><span class="resultDetails"></span></td>
				<td><span class="resultDetails"></span></td>
			</tr>
		<%  }  %>
	
	
	
	
			 <% if (host!=null) {
				%>
				<tr>
				<td><span class="resultDetails">/</span></td>
				<td><span class="resultDetails">(root)</span></td>
				<td><span class="resultDetails"></span></td>
			</tr>
		<% }
	
		//<c:forEach var="path" items="${paths}">
		Iterator p = paths.iterator();
		while(p.hasNext())
		{
			Path pp = (Path)p.next();
			request.setAttribute("path",pp);
	%>
			<tr>
				<td>&gt; <a href="<c:out value="${path.path}"/>"><c:out value="${path.path}"/></a></td>
				<td>
					<span class="resultDetails">
					<%
						Path path2 = (Path)request.getAttribute("path");
						//if(path2!=null) {
							out.println(path2.getChilderen().size());
						//} else {
						// out.println("path2 null");
						 //}
						
						
					%>
					</span>
				</td>
				<td>
					<span class="resultDetails">
						<c:out value="${path.host.lastScanned}"/>
					</span>
				</td>
			</tr>
<%			}
		
		//</c:forEach
		//<c:forEach var="file" items="${files}">
		Iterator f = files.iterator();
		while(f.hasNext())
		{
			request.setAttribute("file",f.next());
			File file = (File)request.getAttribute("file");
			
			
			//redirect?ss="+hit.source.getName()+"&rid="+hit.id+"&up=path_browse"+"\">browse");
			
			String url = "redirect/"+file.getPath()+file.getName();
			
			url = file.getPath().getHost().getProtocol()+file.getPath().getHost().getHostName()+URLEncoder.encode(url, "UTF-8");
			
		%>
			<tr>
				<td><span class="resultDetails"><a href="<%=url%>"><c:out value="${file.name}"/></a></span></td>
				<td><span class="resultDetails">
					<%
						//File file = (File)request.getAttribute("file");
						//if(file!=null) {
							out.println(File.printSize(file));
						//} else {
						//	out.println("file is null");
						//}
					%>
					</span>
				</td>
				<td>
					<span class="resultDetails">
					</span>
				</td>
			</tr>
		<%
		}
			//</c:forEach>
		%>
	</tbody>
</table>