package com.example.image08;
 
import java.io.File;
import java.util.ArrayList;
 
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.app.Activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity implements android.view.View.OnClickListener{
    private Button prevBtn, nextBtn;
    private ViewPager pager; 
    private ViewPagerAdapter adapter;
    ArrayList<String> list = new ArrayList<String>();
     
     
     public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
           // Raw height and width of image
           final int height = options.outHeight;
           final int width = options.outWidth;
           int inSampleSize = 1;
                    
           if (height > reqHeight || width > reqWidth) {
               if (width > height) {
                 inSampleSize = Math.round((float)height / (float)reqHeight);    
                } else {
                     inSampleSize = Math.round((float)width / (float)reqWidth);    
                }   
           }
                    
                   return inSampleSize;    
         }
      
    public Bitmap decodeSampleBitmapFromUri(String path, int reqWidth, int reqHeight){
        Bitmap bm = null;
        //First decode with inJustDecodeBounds = true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
         
        //calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        //Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options); 
                
           return bm;   
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        String targetPath = "/storage/usb0/img/";
         
        File targetDirector = new File(targetPath);
         
        File[] files = targetDirector.listFiles();
        for(File file : files){
            list.add(file.getAbsolutePath());
        }
         
         
         
        prevBtn = (Button)findViewById(R.id.prevBtn);
        prevBtn.setOnClickListener(this);
        nextBtn = (Button)findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(this);
         
        pager = (ViewPager)findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(this);
        pager.setAdapter(adapter);
        Toast.makeText(getApplicationContext(), "list.size():"+list.size(), 0).show();
    }
 
     
    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        int cur = pager.getCurrentItem();
        if(viewId == R.id.prevBtn){
            if(cur > 0){
                pager.setCurrentItem(cur -1);
            }else{
            //  Toast.makeText(getApplicationContext(), cur+"", 0).show();
                Toast.makeText(getApplicationContext(), "dfsfqsdgdsgqgqg", Toast.LENGTH_SHORT).show();
            }
        }
        else if(viewId == R.id.nextBtn){
            if(cur < adapter.getCount()-1){
                pager.setCurrentItem(cur +1);
            }else{
                Toast.makeText(getApplicationContext(), ""+adapter.getCount(), 0).show();
            //  Toast.makeText(getApplicationContext(), "ghqs", Toast.LENGTH_SHORT).show();
            }
        }
    }
     
    public class ViewPagerAdapter extends PagerAdapter {
         
        private Context mContext;
        public ViewPagerAdapter(Context context){
            super();
            mContext = context;
        }
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object instantiateItem(View pager, int position) {
            // create a instance of the page and set data
            Gpage page = new Gpage(mContext);
            Bitmap bm = decodeSampleBitmapFromUri(list.get(position), 220, 220);
            page.setImage(bm);
             
            ViewPager curPager = (ViewPager) pager;
            Log.d("DBMSG", "positionCreated:"+position);
            Log.d("DBMSG","paths:"+list.get(position));
            curPager.addView(page, position);
 
            return page;
        }
         
        @Override
        public void destroyItem(View pager, int position, Object view) {
            Log.d("DBMSG", "positionRemoved:"+position );
            ((ViewPager)pager).removeView((Gpage)view);
        }
         
        @Override
        public boolean isViewFromObject(View view, Object object) {
            //create a instance of the page and set data
            return view.equals(object);
        }
         
    }
 
 
}