package com.example.articleandroidpager;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

	private int[] images = {R.drawable.android1, R.drawable.android2, R.drawable.android3, R.drawable.android4, R.drawable.android5};
	private ViewPager mViewPager;
	private MyPagerAdapter myPagerAdapter;
	private Button nextButton, previousButton;
	private TextView positinTextView;
	private int imageCount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ArrayList<ImageView> List_images = null;
		
		List_images = toto();
		
		imageCount = List_images.size();
		
		/*
		 * Initialize every Object
		 */
		myPagerAdapter = new MyPagerAdapter(List_images);
		
		previousButton = (Button)findViewById(R.id.previousButton);
		nextButton = (Button)findViewById(R.id.nextButton);
		
		positinTextView = (TextView)findViewById(R.id.positionTextView);
		
		mViewPager = (ViewPager)findViewById(R.id.viewPager);
		mViewPager.setAdapter(myPagerAdapter );
		
		/*
		 * ViewPager's setCurrentItem(int) method is used here to set the first image although not required   
		 */
		
		
		
		mViewPager.setCurrentItem(0);
		positinTextView.setText("1 of "+imageCount);
		
		/*
		 * Set OnPageChangeListener
		 * In onPageSelected enable/disable next/previous buttons and set the value in poistionTextView
		 */
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() 
		{
			
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				positinTextView.setText((position+1)+" of "+imageCount);
				/*
				if (position==imageCount-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
				mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
			}
		});
		
		previousButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1);
			}
		});
	}
	
	
	protected  ArrayList<ImageView> toto()
	{
		 ArrayList<String> list = new ArrayList<String>();
		// ArrayList<ImageView> mViewsList = null;
		 Context mContext = null;
		
		 ArrayList<ImageView> mViewsList = new ArrayList<ImageView>();
		 
		 Bitmap bMap1;
		 ImageView image1 = new ImageView(this);
		   
		 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(this);
	   	   		   image1.setImageBitmap(bMap1); 
	   	   		   mViewsList.add(image1);
	    	}
		
	       
	       return(mViewsList);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/*
	 * Pager Adapter for ViewPager
	 * We have to override 4 methods as done below
	 */
	private class MyPagerAdapter extends PagerAdapter {
		
	    private ArrayList<ImageView> datas = null;
		
		public MyPagerAdapter( ArrayList<ImageView> List_images)
		{
	        this.datas = List_images;
		}
		
		/*
		 * Returns the 
		 */
		@Override
		public int getCount() {
			return imageCount;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
		
			
			View myView = datas.get(position);
		   
			container.addView(myView); // , LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			
			
			return myView;
			   
			

//			return mImageView;
			
			
	
			   
			   //http://www.c-sharpcorner.com/uploadfile/2fd686/android-viewpager/
			   
			   
			 
			   
			
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

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

	}
	
}

