package com.view;

import java.io.File;
import java.util.*;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import viewbg.*;

public class LocalBrowser extends ListActivity {

	private enum MODE{ABSOLUTE_DIR,RELATIVE_DIR};
	private List<BrowserItem> items=new ArrayList<BrowserItem>();
	private File current_dir=new File("\\");
	private MODE mode=MODE.RELATIVE_DIR;
	private TextView dir_title;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		dir_title=(TextView)findViewById(R.id.dir_in_browser);
	}
	
	private void listRoot()
	{
		browseTo(new File("/"));
	}
	
	private void browseTo(final File dir)
	{
		if(this.mode==MODE.RELATIVE_DIR)
		{
			dir_title.setText(dir.getAbsolutePath());
		}
		if(dir.isDirectory())
		{
			this.current_dir=dir;
			fill(dir.listFiles());
		}
		else
		{
			LocalBrowser.this.openFile(dir);
			
		}
	}
	
	protected void onListItemClick(android.widget.ListView lv,View view,int position,long id)
	{
		super.onListItemClick(lv, view, position, id);
		int rowId=(int)this.getSelectedItemId();
		String sFileId=this.items.get(rowId).getText();
		
		if(sFileId.equals(getString(R.string.current_dir)))
		{
			this.browseTo(this.current_dir);
		}
		else if(sFileId.equals(getString(R.string.up_one_level)))
		{
			this.upOneLevel();
		}
		else
		{
			File clickedFile=null;
			switch(this.mode)
			{
			case ABSOLUTE_DIR:
				clickedFile=new File(this.items.get(rowId).getText());
				break;
			case RELATIVE_DIR:
			clickedFile=new File(this.current_dir.getAbsoluteFile()+this.items.get(rowId).getText());
			break;
			default:break;
			}
			if(clickedFile!=null)
				this.browseTo(clickedFile);
		}
	}
	
	/*
	 * @param ext
	 * @param exts
	 */
	private boolean checkEndsWiStrings(String ext,String[] exts)
	{
		for(String e:exts)
		{
			if(ext.endsWith(e))
				return true;
		}
		return false;
	}
	
	private void fill(File[] files)
	{
		 this.items.clear();
	      // Add the "." == "current directory"
	      this.items.add(new BrowserItem(
	            getString(R.string.current_dir), getResources().getDrawable(
	                  R.drawable.logo)));
	      if (this.current_dir.getParent() != null) {
	         this.items.add(new BrowserItem(
	               getString(R.string.up_one_level), getResources()
	                     .getDrawable(R.drawable.logo)));
	      }// end if
	      Drawable currentIcon = null;
	      for (File currentFile : files) {
	         if (currentFile.isDirectory()) {
	            currentIcon = getResources().getDrawable(R.drawable.logo);
	         } else {
	            String fileName = currentFile.getName();
	            
	         }// end if
	         switch (this.mode) {
	         case ABSOLUTE_DIR:
	            /* on absolute mode, we show the full path */
	            this.items.add(new BrowserItem(currentFile
	                  .getPath(), currentIcon));
	            break;
	         case RELATIVE_DIR:
	            int currentPathStringLen = this.current_dir
	                  .getAbsolutePath().length();
	            this.items.add(new BrowserItem(currentFile
	                  .getAbsolutePath().substring(currentPathStringLen),
	                  currentIcon));
	            break;
	         }// end switch
	         // Collections.sort(this._directoryEntries);
	         BrowserItemAdapter itlAdapter = new BrowserItemAdapter(
	               this);
	         itlAdapter.setListItems(this.items);
	         this.setListAdapter(itlAdapter);
	      }// end for loop
	   
	}
	
	private void openFile(File file)
	{
		Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("file://"+file.getAbsolutePath()));
		startActivity(intent);
	}
	
	private void upOneLevel()
	{
		if(this.current_dir.getParent()!=null)
		{
			this.browseTo(this.current_dir.getParentFile());
		}
	}
}
