package com.example.fichier02;

import java.io.File;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


//http://www.thaicreate.com/mobile/android.html

public class MainActivity extends Activity 
{
	  
    List <String> ImageList;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ImageList = getSD();
        
        // listView1
        final ListView LView1 = (ListView)findViewById(R.id.listView1); 
        	
        LView1.setAdapter(new ImageAdapter(this,ImageList));
        
        //final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        
        LView1.setOnItemClickListener(new OnItemClickListener() 
        {
			public void onItemClick(AdapterView<?> parent, View v,	int position, long id) 
			{

				String strPath = ImageList.get(position).toString();
                
                String name = new File(strPath).getName();
                
                Toast.makeText(MainActivity.this, "Plouf" , Toast.LENGTH_LONG).show();
                
                Intent intent = new Intent(MainActivity.this ,vision_image.class);
                intent.putExtra("selected-item", name);
                startActivity(intent);  
                
 		    	
			}
		});
    
      
    }

    private List <String> getSD()
    {
	    List <String> it = new ArrayList <String>();
	    File f = new File ("/mnt/sdcard");
	    File[] files = f.listFiles ();
	    
	    for (int i = 0; i <files.length; i++)
	    {
	    	File  file = files[i];
	    	String extension = file.getPath().substring(file.getPath().lastIndexOf('.') + 1);
	    	Log.d("Count",extension);
	    	 
	        if(extension.equals("jpg"))
	        {
	        	 it.add (file.getPath());
	        }	 
	    }

	    return it;
    }
    
    public class ImageAdapter extends BaseAdapter 
    {
        private Context context;
        private List <String> lis;
        
        public ImageAdapter(Context c, List <String> li) 
        {
        	// TODO Auto-generated method stub
            context = c;
            lis = li;
        }
 
        public int getCount() 
        {
        	// TODO Auto-generated method stub
            return lis.size();
        }
 
        public Object getItem(int position)
        {
        	// TODO Auto-generated method stub
            return position;
        }
 
        public long getItemId(int position)
        {
        	// TODO Auto-generated method stub
            return position;
        }
        
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 
			if (convertView == null) 
			{
				convertView = inflater.inflate(R.layout.activity_column, null); 
			}
					
			// ColImage
			String strPath = lis.get(position).toString();
			Bitmap bm = BitmapFactory.decodeFile(strPath);

			ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImage);
			imageView.getLayoutParams().height = 100;
			imageView.getLayoutParams().width = 100;
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setImageBitmap(bm);
			
			TextView txtPicName = (TextView) convertView.findViewById(R.id.ColPicname);
			txtPicName.setPadding(10, 0, 0, 0);
			String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length());
			
			File myFile = new File("/sdcard/" + fileName);
			SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy - hh:mm:ss");			
			String date = dateFormat.format(myFile.lastModified());	
			String taille = getFileSize(myFile);
			txtPicName.setText(fileName + "   " + taille + "   " + date );
		 
			return convertView;
				
		}
		
		
	    public String getFileSize(File file)
	    {
	        String modifiedFileSize = null;
	        double fileSize = 0.0;
	        
	        if(file.isFile())
	        {
	            fileSize = (double) file.length();  //in Bytes

	            if (fileSize < 1024) 
	            {
	                modifiedFileSize = String.valueOf(fileSize).concat("B");
	            } 
	            else if (fileSize > 1024 && fileSize < (1024 * 1024)) 
	            {
	                modifiedFileSize = String.valueOf(Math.round((fileSize / 1024 * 100.0)) / 100.0).concat("KB");
	            } 
	            else 
	            {
	                modifiedFileSize = String.valueOf(Math.round((fileSize / (1024 * 1204) * 100.0)) / 100.0).concat("MB");
	            }
	        }
	        
	        else 
	        {
	            modifiedFileSize = "Unknown";
	        }

	        return modifiedFileSize;
	    }
	    


    } 
    
    

};