package com.appli.gui;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Toast;
import android.widget.TextView;

import com.appli.patoche.R;

//http://www.java2s.com/Code/Java/File-Input-Output/Getextensionpathandfilename.htm
//http://www.whycouch.com/2012/12/the-simplest-way-to-use-viewpager.html


public class gallery_activity extends FragmentActivity
{
 
    ViewPager pager;
    FragmentStatePagerAdapter adapter; 
    TextView txt_infos;
     
    ArrayList<Image> arraylist = new ArrayList<Image>();
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.gallery);
	        pager=(ViewPager)findViewById(R.id.my_pager);
	        txt_infos =(TextView)findViewById(R.id.textView1); 
	        
	        /// listing des images dans un repertoire
	        String parentDirectory = "/mnt/sdcard";
	        File dirFileObj = new File(parentDirectory);
	        File[] files = dirFileObj.listFiles();
	        
	        for (File file : files)
	        {
	            if (file.isFile() )
	            {
	            	  if(file.getName().toUpperCase().endsWith(("JPG")) )
	            	  {
	            		  String fullpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + file.getName();
	            		  SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	            		  String date_creation =  sdf.format(file.lastModified());
	            		  Bitmap bitmap = BitmapFactory.decodeFile(fullpath);
	            		  int width = bitmap.getWidth();
	            		  int height = bitmap.getHeight();
	            		  arraylist.add(new Image(file.getName(), getFileSize(file), fullpath, date_creation, width, height));
	            	  }
	            } 
	            
	        }
	
	        // init
	        show_info(0);
	       
	        adapter=new FragmentStatePagerAdapter(getSupportFragmentManager() )
	        {
    
	            @Override
	            public int getCount() 
	            {
	               return arraylist.size();
	            }
	    
	            @Override
	            public Fragment getItem(int position)
	            {
	            	
	            	String url = "file://mnt/sdcard/" + arraylist.get(position).Getname();
	                return fragment_webview.newInstance(url);
	            }
	        };
   
	        //Let the pager know which adapter it is supposed to use
	        pager.setAdapter(adapter);  
        
	        pager.addOnPageChangeListener(new OnPageChangeListener() 
	        {
		        	// This method will be invoked when a new page becomes selected.
		        	@Override
		        	public void onPageSelected(int position)
		        	{
		        		//Toast.makeText(MainActivity.this, "Selected page position: " + position, Toast.LENGTH_SHORT).show();
		        		//String pos = Integer.toString(position+1) +   "/"  + Integer.toString(images.length) + "    " + images[position];
		        		//txt_infos.setText(pos);
		        		
		    	        //String name = arraylist.get(position).Getname();
		    	        //int w = arraylist.get(position).Getwidth();
		    	        //int h = arraylist.get(position).Getheight();
		    	        //String infos = "1" + "/" + Integer.toString(images.length) + "    " +  name + "  " + w + "  " + h ; //images[0];
		    	        //txt_infos.setText(infos);
		    	        show_info(position);

		        	}
		        	
		        	// This method will be invoked when the current page is scrolled
		        	@Override
		        	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
		        	{
		        		// Code goes here
		        	}
		        	
		        	// Called when the scroll state changes: 
		        	// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
		        	@Override
		        	public void onPageScrollStateChanged(int state) 
		        	{
		        		// Code goes here
		        	}
	        });
        
    }  
    
    public void show_info(int position)
    {
        String name = arraylist.get(position).Getname();
        String size = arraylist.get(position).Getsize();
        String date = arraylist.get(position).Getdate();
        String fullpath = arraylist.get(position).Getfullpath();
        int w = arraylist.get(position).Getwidth();
        int h = arraylist.get(position).Getheight();
        String tmp = " " +  name + "  " + size + "  " + date + " " +  "  " + w + "  " + h  ;
        //String infos = "1" + "/" + Integer.toString(images.length) + tmp ; //images[0];
        int index = position + 1;
        String infos = index + "/" +  arraylist.size() + tmp ; //images[0];
        txt_infos.setText(infos);

    }
    
    public static 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;
    }
}

// classe qui d�finit les objets images
class Image
{
		   String name;
		   String size;
		   String date;
		   String fullpath;
		   int width;
		   int height;
			
		   public Image(String name, String size, String fullpath, String date,  int width, int height)
		   {
		      this.name = name;
		      this.size = size;
		      this.date = date;
		      this.fullpath = fullpath;
		      this.width = width;
		      this.height = height;
		   }
		   /****/
		   public void Setname(String name)
		   {
			   this.name = name;
		   }
		   
		   public String Getname()
		   {
			   return name;
		   }
		   /****/
		   public void Setsize(String size)
		   {
			   this.size = size;
		   }
		   public String Getsize()
		   {
			   return size;
		   }
		   
		   /****/
		   public void Setdate(String date)
		   {
			   this.date = date;
		   }
		   public String Getdate()
		   {
			   return date;
		   }
		   /****/
		   public void Setfullpath(String fullpath)
		   {
			   this.fullpath = fullpath;
		   }
		   public String Getfullpath()
		   {
			  return fullpath;
		   }
		   /****/
		   public void Setwidth(int width)
		   {
			   this.width = width;
		   }
		   public int Getwidth()
		   {
			   return width;
		   }
		   /****/
		   public void Setheight(int height)
		   {
			   this.height = height;
		   }
		   public int Getheight()
		   {
			   return height;
		   }
		   
}