package com.example.image10;

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

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;


import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;


public class MainActivity extends Activity {

	
	private ViewPager mMyPager;
    private MyPagerAdapter mMyPagerAdapter;
    private TextView positinTextView;
    private Button nextButton, previousButton;
	private int n;
	private File fichier[];
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    
	   n = new File("/mnt/sdcard/patoche").listFiles().length;
	   fichier = new File("/mnt/sdcard/patoche").listFiles(); 
	   
	   
	    positinTextView = (TextView)findViewById(R.id.positionTextView);

	    mMyPager = (ViewPager) findViewById(R.id.mypages);        
	    mMyPagerAdapter = new MyPagerAdapter(this);
	    mMyPager.setAdapter(mMyPagerAdapter);
	    
	    positinTextView.setText("1 offf "+n);
	    
	    previousButton = (Button)findViewById(R.id.previousButton);
        nextButton = (Button)findViewById(R.id.nextButton);
        
		Toast.makeText(getApplicationContext(), fichier[0].getName() , Toast.LENGTH_LONG).show();
     
        
        mMyPager.addOnPageChangeListener(new OnPageChangeListener() 
		{
			
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				positinTextView.setText((position+1)+" offf "+n);
				
				//String conv = Integer.toString(position);
				//Toast.makeText(getApplicationContext(), conv , Toast.LENGTH_LONG).show();
				
				
				Toast.makeText(getApplicationContext(), fichier[position].getName() , Toast.LENGTH_LONG).show();
				
				
				if (position== n-1) {
					nextButton.setEnabled(false);
				}
				else {
					nextButton.setEnabled(true);
				}
				if (position==0) {
					previousButton.setEnabled(false);
				}
				else {
					previousButton.setEnabled(true);
				}
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				// TODO Auto-generated method stub
				
				
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub
				
			}
		});
        
        
        /*
		 * Set OnClickListener on buttons to change the Image in ViewPager 
		 */
		nextButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMyPager.setCurrentItem(mMyPager.getCurrentItem()+1);
			}
		});
		
		previousButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMyPager.setCurrentItem(mMyPager.getCurrentItem()-1);
				
				
			}
		});
        
	    
	}
	
	
}

class MyPagerAdapter extends PagerAdapter {

	 private ArrayList<ImageView> mViewsList;
	 private Context mContext = null;
	 private ArrayList<String> list = new ArrayList<String>();
	 
	 int NumberOfPages = 5;
	 
	 int[] backgroundcolor = { 
			   0xFF101010,
			   0xFF202020,
			   0xFF303030,
			   0xFF404040,
			   0xFF505050};

	 public MyPagerAdapter(Context context) 
	 {
		   mContext = context;
		   mViewsList = new ArrayList<ImageView>();
		   
	
		  // Resources resource = mContext.getResources();
		   //Bitmap bMap1 = BitmapFactory.decodeResource(resource,   R.drawable.image1);
	/*	   Bitmap bMap1 = BitmapFactory.decodeFile("/mnt/sdcard/patoche/b0008.jpg");
		   ImageView image1 = new ImageView(mContext);
		   image1.setImageBitmap(bMap1); 
		   mViewsList.add(image1);
		   
		   
		   bMap1 = BitmapFactory.decodeFile("/mnt/sdcard/patoche/15-xl.jpg");
		   image1 = new ImageView(mContext);
		   image1.setImageBitmap(bMap1); 
		   mViewsList.add(image1);
	
		   bMap1 = BitmapFactory.decodeFile("/mnt/sdcard/patoche/33-xl.jpg");
		   image1 = new ImageView(mContext);
		   image1.setImageBitmap(bMap1); 
		   mViewsList.add(image1);
	*/
		   //Bitmap bMap2 = BitmapFactory.decodeResource(resource,  R.drawable.image2);
		   //ImageView image2 = new ImageView(mContext);
		   //image2.setImageBitmap(bMap2); 
		   //mViewsList.add(image2);
	
		   //BitmapDrawable bd = new BitmapDrawable(resource, bMap1);
	 
		   Bitmap bMap1;
		   ImageView image1 = new ImageView(mContext);
		   
		   //ArrayList<String> list = new ArrayList<String>();
		   String targetPath = "/mnt/sdcard/patoche/";
	       
	       File targetDirector = new File(targetPath);
	        
	       File[] files = targetDirector.listFiles();
	       for(File file : files){
	           list.add(file.getAbsolutePath());
	       }
       
	       for (int i = 0; i < list.size(); i++)
	       {
	   	         bMap1 = BitmapFactory.decodeFile(list.get(i));
	 	   		   image1 = new ImageView(mContext);
	   	   		   image1.setImageBitmap(bMap1); 
	   	   		   mViewsList.add(image1);
	    	}
       
	   
	 } 

	 @Override
	 public int getCount() {
	   return mViewsList.size();
	   //return NumberOfPages;
	 }

	 @Override
	 public Object instantiateItem(View view, int position)
	 {
		 
		 TextView textView = new TextView(mContext);
		   textView.setTextColor(Color.WHITE);
		   textView.setTextSize(30);
		   textView.setTypeface(Typeface.DEFAULT_BOLD);
		   textView.setText(String.valueOf(position));
		   
		   
		   LinearLayout layout = new LinearLayout(mContext);
		      layout.setOrientation(LinearLayout.VERTICAL);
		      LayoutParams layoutParams = new LayoutParams(
		        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		      layout.setBackgroundColor(backgroundcolor[position]);
		      layout.setLayoutParams(layoutParams);
		 
	   View myView = mViewsList.get(position);
	   
	 int pos = ((ViewPager) view).getCurrentItem();
	String conv = Integer.toString(pos);
	   
	   ((ViewPager) view).addView(myView);
	   
	  // list.get(position);
	   
	  // Toast.makeText(mContext, conv , Toast.LENGTH_LONG).show();
	   
	  //((ViewPager) view).addView(textView);
	  // TextView tv=(TextView)myPager.findViewById(R.id.page);
	   
	//   TextView tv=(TextView)view.findViewById(this);
	   
	  TextView tv = new TextView(mContext);
      tv.setText("Hello, world! myPostion :" + position);
      tv.setTextColor(Color.BLUE);
      tv.setTextSize(30);
      
      ((ViewPager) view).addView(tv);
      //tv.setBackgroundColor
	  
	  
//	  View view = inflater.inflate(R.layout.fragment_first, container, false);
//		TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
//		tvLabel.setText(page + " -- " + title);
	   
	   return myView;
	   
	   
	   /*
	   LayoutInflater inflater = getLayoutInflater();
       View view = inflater.inflate(R.layout.layout_page, null);
       TextView num = (TextView) view.findViewById(R.id.page_number);
       String pos = "This is page " + (position + 1);
       num.setText(pos);

       container.addView(view);

       return view;*/
	 }

	 @Override
	 public boolean isViewFromObject(View view, Object object) {
	  return view == object;
	 }

	 @Override
	 public void destroyItem(View view, int arg1, Object object) {
	  ((ViewPager) view).removeView((ImageView) object);
	 }
	}