package com.appli.image;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.appli.patoche.R;

import static com.appli.gui.gallery_activity.getFileSize;

public class FullScreenImageAdapter extends PagerAdapter
{
	private Activity _activity;
	private ArrayList<String> _imagePaths;
	private LayoutInflater inflater;

    int orientation = 0;

	// 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)
	{
        final TouchImageView imgDisplay;
        Button btnClose, btnRotate;
		TextView infos;
 
        inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//      View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false);
		View viewLayout = inflater.inflate(R.layout.test02, container, false);

		imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
        btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
		btnRotate = (Button) viewLayout.findViewById(R.id.btnRotate);
		infos = (TextView) viewLayout.findViewById(R.id.txt_infos);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        final Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options); // final rajout
		imgDisplay.setImageBitmap(bitmap);

		String path = _imagePaths.get(position);
		File file = new File(path);
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		String date_creation =  sdf.format(file.lastModified());
		String filesize = getFileSize(file);
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		String filename = file.getName();
		String str_infos = path + "    " + filesize + "\n" + date_creation + "   " + width + "x" + height;
		infos.setText(str_infos);

		// effetcs

		//http://www.c-sharpcorner.com/UploadFile/e14021/adding-effects-to-image-in-android-using-android-studio/
		// https://www.tutorialspoint.com/android/android_image_effects.htm

        // close button click event
        btnClose.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				_activity.finish();
			}
		});

		btnRotate.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Matrix matrix = new Matrix();

                if(orientation == 0)
                {
                    orientation = -90;
                }
                else if(orientation == -90)
                    orientation = -180;
                else if(orientation == -180)
                    orientation = -270;
                else if(orientation == -270)
                    orientation = 0;

                matrix.postRotate(orientation);

                Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap .getWidth(), bitmap .getHeight(), matrix, true);
				imgDisplay.setImageBitmap(rotatedBitmap);
				Toast.makeText( _activity, " Rotate !", Toast.LENGTH_LONG).show();

			}
		});


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

}