package com.appli.gui;

import java.io.File;
import java.sql.Date;
import java.util.ArrayList; 
import java.util.Collections;
import java.util.List;
import java.text.DateFormat; 

import android.net.Uri;
import android.os.Bundle; 
import android.app.ListActivity;
import android.content.Intent; 
import android.view.View;
import android.widget.ListView; 
import android.widget.Toast;

import com.appli.patoche.R;

public class FileChooser extends ListActivity {

	private File currentDir;
    private FileArrayAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        currentDir = new File("/sdcard/");
        fill(currentDir); 
    }
    private void fill(File f)
    {
    	File[]dirs = f.listFiles(); 
		 this.setTitle("Current Dir: "+f.getName());
		 List<Item>dir = new ArrayList<Item>();
		 List<Item>fls = new ArrayList<Item>();
		 try{
			 for(File ff: dirs)
			 { 
				Date lastModDate = new Date(ff.lastModified()); 
				DateFormat formater = DateFormat.getDateTimeInstance();
				String date_modify = formater.format(lastModDate);
				if(ff.isDirectory()){
					
					
					File[] fbuf = ff.listFiles(); 
					int buf = 0;
					if(fbuf != null){ 
						buf = fbuf.length;
					} 
					else buf = 0; 
					String num_item = String.valueOf(buf);
					if(buf == 0) num_item = num_item + " item";
					else num_item = num_item + " items";
					
					//String formated = lastModDate.toString();
					dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon")); 
				}
				else
				{
					
					fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
				}
			 }
		 }catch(Exception e)
		 {    
			 
		 }
		 Collections.sort(dir);
		 Collections.sort(fls);
		 dir.addAll(fls);
		 if(!f.getName().equalsIgnoreCase("sdcard"))
			 dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
		 adapter = new FileArrayAdapter(FileChooser.this, R.layout.file_view_layout,dir);
		 this.setListAdapter(adapter); 
    }
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Item o = adapter.getItem(position);
		if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
				currentDir = new File(o.getPath());
				fill(currentDir);
		}
		else
		{
			onFileClick(o);
		}
	}
    private void onFileClick(Item o)
    {
    	Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
    	
    	String file = o.getName();
    	
    	String ext = file.substring(file.lastIndexOf(".") + 1);
    	
    	if (ext.equalsIgnoreCase("jpeg") || ext.equalsIgnoreCase("jpg") )
    	{	
    		
    		String selectType = "image/*";
    		
    		currentDir = new File(o.getPath());
    		
    		file = "/mnt/sdcard/patoche/21.jpg";   		
    		 Intent intent = new Intent();
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setDataAndType(Uri.fromFile(currentDir), selectType);
             startActivity(intent);
    		
             
//             Intent picIntent = new Intent();
//	    		picIntent.setAction(android.content.Intent.ACTION_VIEW);
//	    		picIntent.setDataAndType(Uri.fromFile(file), "image/*");
//	    		startActivity(picIntent);
//    			//http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
    		
    		//http://code.tutsplus.com/tutorials/image-display-and-interaction-with-android-webviews--mobile-11362
    	}
    	//Intent intent = new Intent();
        //intent.putExtra("GetPath",currentDir.toString());
        //intent.putExtra("GetFileName",o.getName());
        //setResult(RESULT_OK, intent);
        //finish();
    }
} 