/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */

package com.ovhoo.android.file;

import com.ovhoo.android.fiveexplore.ContentAdapter;

import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Class used in this porject to manage the curent directory explored by the user
 * @author mzalim
 *
 */
public class CurentDirManager {
	private FileInterface fileRoot;
	private ArrayList<FileInterface> filesList;
	
	private ContentAdapter controler;

	
	public CurentDirManager(ContentAdapter controler,String root){
		this.controler = controler;
		
		this.setRoot(root);
		
		this.filesList = new ArrayList<FileInterface>();
		this.ls();
	}
	
	/**
	 * Sed curent dir == cd 
	 * @param path : the path of the directory to go
	 * @return : returns tre if the given path points to a directory
	 */
	public boolean setRoot(String path){
		FileInterface _file = new FileInterface(path);
		if (_file.isDirectory()){
			this.fileRoot = _file;
			this.contentUpdated();
			
			return true;
		}
		else{
			return false;
		}
	}
	
	// Usual unix fs commands
	
	/**
	 * list the Directory
	 * @return
	 */
	public ArrayList<FileInterface> ls(){
			this.filesList = this.fileRoot.ls();
			return this.filesList;
	}
	
	public ArrayList<FileInterface> list(FilenameFilter filter){
		return this.fileRoot.ls(filter);
}
	
	/**
	 * return current directory
	 * @return path to the current directory
	 */
	public String pwd(){
		return this.fileRoot.getAbsolutePath();
	}
	
	public void cd(String dir){
		if(dir.startsWith("/")){
			this.setRoot(dir);
		}
		else {
			this.setRoot(this.pwd() + "/" + dir);
		}
	}
	
	/**
	 * Create a directory in the curent dir
	 * @param dirname
	 */
	public boolean mkdir(String dirname){
		//Create Directory
		FileInterface _file = new FileInterface(this.pwd() +  "/" + dirname);
		if (_file.mkdirs()){
			this.contentUpdated();
			return true;
		}
		else{
			return false;
		}
		
		
	}
	
	public void contentUpdated(){
		this.ls();
		this.controler.notifyDataSetChanged();
	}
	
	public  ArrayList<FileInterface> getFilesList(){
		return this.filesList;
	}
	
	public void go_up(){
		String _path = this.fileRoot.getParentDir();
		if (_path != null){
			this.setRoot(_path);
		}
	}
	
	//Rename the file
	public boolean rename(FileInterface file, String name) {
		if (file.renameTo(name)){
			this.contentUpdated();
			return true;
		}
		else{
			return false;
		}
		
	}

	public boolean rm(FileInterface file) {
		if (file.delete()){
			this.contentUpdated();
			return true;
		}
		else{
			return false;
		}
		
	}	
}
