package com.example.image07;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

	private Utils utils;
	private FullScreenImageAdapter adapter;
	private ViewPager viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		viewPager = (ViewPager) findViewById(R.id.pager);

		utils = new Utils(getApplicationContext());

		Intent i = getIntent();
		int position = i.getIntExtra("position", 0);

		adapter = new FullScreenImageAdapter(MainActivity.this,	utils.getFilePaths());

		viewPager.setAdapter(adapter);

		// displaying selected image first
		viewPager.setCurrentItem(position);
	}
}

class Utils {

	private Context _context;

	// constructor
	public Utils(Context context)
	{
		this._context = context;
	}

	/*
	 * Reading file paths from SDCard
	 */
	public ArrayList<String> getFilePaths() 
	{
			ArrayList<String> filePaths = new ArrayList<String>();

			File directory = new File(android.os.Environment.getExternalStorageDirectory() + "/mnt/sdcard/patoche");

			File[] listFiles = directory.listFiles();

			// Check for count
			if (listFiles.length > 0)
			{

					// loop through all files
					for (int i = 0; i < listFiles.length; i++)
					{
	
						// get file path
						String filePath = listFiles[i].getAbsolutePath();
	
						// check for supported file extension
						if (IsSupportedFile(filePath)) 
						{
							// Add image path to array list
							filePaths.add(filePath);
						}
					}
			} 
			
			else 
			
			{
				// image directory is empty
				Toast.makeText(	_context,	AppConstant.PHOTO_ALBUM	+ " is empty. Please load some images in it !",		Toast.LENGTH_LONG).show();
			}

			return filePaths;
	}

	/*
	 * Check supported file extensions
	 * 
	 * @returns boolean
	 */
	private boolean IsSupportedFile(String filePath)
	{
			String ext = filePath.substring((filePath.lastIndexOf(".") + 1), filePath.length());

			if (AppConstant.FILE_EXTN.contains(ext.toLowerCase(Locale.getDefault())))
				return true;
			else
				return false;
	}

	/*
	 * getting screen width
	 */
	public int getScreenWidth() 
	{
		int columnWidth;
		WindowManager wm = (WindowManager) _context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();

		final Point point = new Point();
		try {
			display.getSize(point);
		} catch (java.lang.NoSuchMethodError ignore) { // Older device
			point.x = display.getWidth();
			point.y = display.getHeight();
		}
		columnWidth = point.x;
		return columnWidth;
	}
}

class AppConstant {

	// Number of columns of Grid View
	public static final int NUM_OF_COLUMNS = 3;

	// Gridview image padding
	public static final int GRID_PADDING = 8; // in dp

	// SD card image directory
	public static final String PHOTO_ALBUM = "NAT";

	// supported file formats
	public static final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg",	"png");
}

class FullScreenImageAdapter extends PagerAdapter {

	private Activity _activity;
	private ArrayList<String> _imagePaths;
	private LayoutInflater inflater;

	// constructor
	public FullScreenImageAdapter(Activity activity, ArrayList<String> imagePaths) 
	{
		this._activity = activity;
		this._imagePaths = imagePaths;
		
		
	}

	@Override
	public int getCount() {
		return this._imagePaths.size();
	}

	@Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }
	
	@Override
    public Object instantiateItem(ViewGroup container, int position) {
        //TouchImageView imgDisplay;
        //Button btnClose;
 	
		   inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        View viewLayout = inflater.inflate(R.layout.activity_main, container,  false);

		
      //  TextView textView = new TextView(this._activity);
   //     textView.setTextColor(Color.WHITE);
	//    textView.setTextSize(30);
	//    textView.setTypeface(Typeface.DEFAULT_BOLD);
	//    textView.setText(String.valueOf(position));
	    
/*	    
	    LayoutParams imageParams = new LayoutParams(
	    		LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
	 
	    
	    LinearLayout layout = new LinearLayout(this._activity);
	    layout.setOrientation(LinearLayout.VERTICAL);
	    LayoutParams layoutParams = new LayoutParams(
	    		LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
	  //  layout.setBackgroundColor(backgroundcolor[position]);
	    layout.setLayoutParams(layoutParams);
	    layout.addView(textView);
*/	    
	  
	    
	  //  layout.setBackgroundColor(backgroundcolor[position]);
	   // layout.setLayoutParams(layoutParams);
        
 /*       
        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);
 
        //imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
       // btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
        
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
        imgDisplay.setImageBitmap(bitmap);
        
        // close button click event
        
*/
       
	    ((ViewPager) container).addView(viewLayout);
	    
        return viewLayout;

	    
      
	}
		
        

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

}

