package athary.audio.ency.Helpers.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import athary.audio.ency.R;

public class FileChooser extends ListActivity {
	
    private File currentDir;
    private FileArrayAdapter adapter;
	private String FLAG;
	private String PATH;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        FLAG = getIntent().getStringExtra("FLAG");
        
        currentDir = new File("/mnt/");
        fill(currentDir);
    }
    private void fill(File f)
    {
    	File[]dirs = f.listFiles();
		 this.setTitle("Current Dir: "+f.getName());
		 List<Option>dir = new ArrayList<Option>();
		 List<Option>fls = new ArrayList<Option>();
		 try{
			 for(File ff: dirs)
			 {
				if(ff.isDirectory())
					dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
				else
				{
					fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
				}
			 }
		 }catch(Exception e)
		 {
			 
		 }
		 Collections.sort(dir);
		 Collections.sort(fls);
		 if (FLAG.equals("DB")){dir.addAll(fls);}

		 if(!f.getName().equalsIgnoreCase(""))
			 dir.add(0,new Option("..","الذهاب إلى الأعلى^^",f.getParent()));
		 adapter = new FileArrayAdapter(FileChooser.this,R.layout.txt_file_view,dir);
		 this.setListAdapter(adapter);
    }
    
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Option o = adapter.getItem(position);
if (FLAG.equals("DB")){
	
	
	if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("الذهاب إلى الأعلى^^")){
		currentDir = new File(o.getPath());
		fill(currentDir);
}
else
{
	onFileClick(o);
}
	
}
else if (FLAG.equals("DOWNLOAD")){
	
	if(o.getData().equalsIgnoreCase("folder")){
		currentDir = new File(o.getPath());
		fill(currentDir);
		PATH = o.getPath() + "/";
        Toast.makeText(getApplicationContext(),"تم اختيار المسار" + "\n" +  o.getPath() + "/", Toast.LENGTH_LONG).show();

}
	else if (o.getData().equalsIgnoreCase("الذهاب إلى الأعلى^^")){
		currentDir = new File(o.getPath());
		fill(currentDir);
	}
	}

	}
    private void onFileClick(Option o)
    {

    	   SharedPreferences athary = getSharedPreferences("athary", 0);
  		   SharedPreferences.Editor editor = athary.edit();
  		   editor.putString("path", o.getPath());

  		   //Commit the edits!
  		   editor.commit();
//  	    	Toast.makeText(this, o.getPath(), Toast.LENGTH_SHORT).show();
  		 //onDestroy();
         setResult(2);
  		 finish(); 
    	}

    @Override
    public void onBackPressed()
    {
    	 if (FLAG.equals("DOWNLOAD")){
    		 Intent newIntent = new Intent();
    		 newIntent.putExtra("ComingFrom", PATH);
    		 setResult(5, newIntent);
    	 }
 		 finish(); 
 		 
    }

}