package com.cwbuyer.lib;

import com.pwbuyer.main.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

public class PhotoFull  extends Activity {
	  private ImageView image1;
	  private String mSerch="";
	  private Bitmap bm;
	  private int bmWidth=0;
	  private int bmHeight=0;
	  private int width=0;
	  private int height=0;
	  private int pointX=0;
	  private int pointY=0;
	  private GestureDetector detector;
	  private myGestureListener gListener;
	  
	  /** Called when the activity is first created. */
		@SuppressWarnings("deprecation")
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			setContentView(R.layout.photofull);
	    /* 取得螢幕寬高 */    
	    width=getWindowManager().getDefaultDisplay().getWidth();
	    height=getWindowManager().getDefaultDisplay().getHeight();   
	    /* Bitmap設定 */
		mSerch=getIntent().getStringExtra("PHOTO");

	    bm=Utilis.getLimitBitmap(mSerch, 210, 280);
	    bmWidth=bm.getWidth();
	    bmHeight=bm.getHeight();
	    /* ImageView初始化 */
	    image1=(ImageView)findViewById(R.id.image1);
	    Bitmap newB=Bitmap.createBitmap(bm,pointX,pointY, width, height);
	    image1.setImageBitmap(newB);
	    /* GestureDetector設定 */
	    gListener = new myGestureListener();
	    detector = new GestureDetector(PhotoFull.this,gListener);
	  }
	  /* 當Activity的onTouchEvent()被觸發時，
	   * 觸發GestureDetector的onTouchEvent() */
	  @Override
	  public boolean onTouchEvent(MotionEvent event)
	  {
	    if (detector.onTouchEvent(event))
	    {
	      return detector.onTouchEvent(event);
	    }  
	    else
	    {
	      return super.onTouchEvent(event);
	    }
	  }
	  
	  /* 自定義GestureListener */
	  public class myGestureListener implements GestureDetector.OnGestureListener
	  {
	    /* 手指在螢幕上拖拉時觸發此method */
	    @Override
	    public boolean onScroll(MotionEvent e1, MotionEvent e2,
	                            float distanceX,  float distanceY)
	    {
	      /* 計算X軸基準點移動後的位置 */
	      if(pointX+distanceX>=0){
	        if((pointX+distanceX)>(bmWidth-width)){  
	          pointX=bmWidth-width;
	        }else{
	          pointX+=distanceX;
	        }
	      }else{
	        pointX=0;
	      }
	      /* 計算Y軸基準點移動後的位置 */
	      if(pointY+distanceY>=0){  
	        if((pointY+distanceY)>(bmHeight-height)){  
	          pointY=bmHeight-height;
	        }else{
	          pointY+=distanceY;
	        }
	      }else{
	        pointY=0;
	      }
	      /* 如果有移動，則更新Bitmap設定 */
	      if(distanceX!=0&&distanceY!=0)
	      {
	        Bitmap newB=Bitmap.createBitmap(bm,pointX,pointY,width,height);
	        image1.setImageBitmap(newB);
	      }
	      return false;
	    }

	    @Override
	    public boolean onDown(MotionEvent arg0)
	    {
	      System.out.println("DOWN");
	      return false;
	    }

	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2,
	        float velocityX, float velocityY)
	    {
	      return false;
	    }

	    @Override
	    public void onLongPress(MotionEvent e)
	    {
	    }

	    @Override
	    public void onShowPress(MotionEvent e)
	    {
	    }

	    @Override
	    public boolean onSingleTapUp(MotionEvent e)
	    {
	      return false;
	    }
	  }
	}