package de.uwesoftware;

import android.view.View;
import android.content.*;
import android.util.*;
import android.graphics.*;
import android.view.*;
import de.uwesoftware.mobilRaw.*;

public final class ImageView extends View {

	int moveX = 0, moveY = 0;
	int counter  = 0;
	
	//int image[];
	Bitmap image;
	int imagesizeX, imagesizeY;
	
	int src_imagesizeX, src_imagesizeY;
	int posX, posY;
	int image_posX = 0; 
	int image_posY = 0;
	
	boolean firstload = true;
	
	MobilRawWrapper mobilraw = null;
	
	String path = "/sdcard/IMGP2040.DNG";
	
	public ImageView(Context context){
		super(context);
		android.util.Log.v(FormularActivity.TAG, "Constructor 1");
		
		setup(this.path);
	}
	
	public ImageView(Context context, AttributeSet attrs){
		super(context, attrs);
		this.setVisibility(View.VISIBLE);
		android.util.Log.v(FormularActivity.TAG, "Constructor 2");
		
		setup(this.path);
	}
	
	public ImageView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		android.util.Log.v(FormularActivity.TAG, "Constructor 3");
		
		setup(this.path);
	}
	
	private void setup(String path){
		mobilraw = new MobilRawWrapper(path);
		src_imagesizeX = mobilraw.get_ZoomsizeX();
		src_imagesizeY = mobilraw.get_ZoomsizeY();
	}
	
	@Override
	protected void  onMeasure  (int widthMeasureSpec, int heightMeasureSpec){
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
		int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
		this.setMeasuredDimension(parentWidth, parentHeight);
		
		imagesizeX = getMeasuredWidth();
		imagesizeY = getMeasuredHeight();
		
		image = Bitmap.createBitmap(imagesizeX, imagesizeY, Bitmap.Config.RGB_565);
		drawImage();
		
		
		 android.util.Log.v(FormularActivity.TAG,"widthMeasureSpec = " + widthMeasureSpec + "  heightMeasureSpec = " + heightMeasureSpec);
		 android.util.Log.v(FormularActivity.TAG, "parentWidth = " + parentWidth + " parentHeight = " + parentHeight);
	}
	
	@Override
	protected void  onDraw  (Canvas canvas){
		if(moveX == 0 && moveY == 0){
			android.util.Log.v(FormularActivity.TAG, "height = " + this.getHeight() + " width = " + this.getWidth());
			
			canvas.drawBitmap(image, 0, 0, null);
		}else{
			//canvas.clipRect(0, 0, imagesizeX, imagesizeY);
			//canvas.translate(moveX, moveY);
			
			canvas.drawBitmap(image, 0, 0, null);
			//canvas.drawBitmap(image,0, imagesizeX,0,0,imagesizeX, imagesizeY, false, null);
			moveX = 0;
			moveY = 0;
		}
	}
	
	@Override
	public boolean  onTouchEvent  (MotionEvent ev){
		int tempX, tempY;
		
		final int pointerCount = ev.getPointerCount();
		if(pointerCount > 0){
			switch(ev.getAction()){
			case MotionEvent.ACTION_DOWN:
				posX = (int)ev.getX(0);
				posY = (int)ev.getY(0);
				android.util.Log.v(FormularActivity.TAG,"Down" );
				break;
			case MotionEvent.ACTION_MOVE:
				tempX = posX;
				tempY = posY;
				posX = (int)ev.getX(0);
				posY = (int)ev.getY(0);
				move(posX - tempX, posY - tempY);
				android.util.Log.v(FormularActivity.TAG,"Move" );
				break;
			case MotionEvent.ACTION_UP:
				android.util.Log.v(FormularActivity.TAG,"Up" );
				break;
			}
		}
		//printSamples(ev);
		return true;
	}
	
	/*
	 void printSamples(MotionEvent ev) {
		 int curX, curY;
		
	     final int historySize = ev.getHistorySize();
	     final int pointerCount = ev.getPointerCount();
	     for (int h = 0; h < historySize; h++) {
	    	 android.util.Log.v(FormularActivity.TAG,"Historial At time " + ev.getHistoricalEventTime(h));
	         for (int p = 0; p < pointerCount; p++) {
	        	 android.util.Log.v(FormularActivity.TAG, "  pointer "+ ev.getPointerId(p) + ": (" + ev.getHistoricalX(p, h) + ", " + ev.getHistoricalY(p, h)+ ")"  );
	         }
	     }
	     android.util.Log.v(FormularActivity.TAG,"Other At time"+ ev.getEventTime());
	     for (int p = 0; p < pointerCount; p++) {
	    	 android.util.Log.v(FormularActivity.TAG,"counter = " + counter+ "  pointer "+ ev.getPointerId(p)+": ("+  ev.getX(p)+ ", " + ev.getY(p)+")");
	     }
	     counter += 1;
	 }
	 */
	 private void move(int x, int y){
		 
		 int moveX, moveY;
		 int move[] = new int[7];
		 int tempX	= image_posX - x;
		 int tempY	= image_posY - y;
		 int width	= image.getWidth();
		 int length	= image.getHeight();
		 int destX, destY, srcX, srcY, twidth, theight;;
		 
		 if(tempX < 0){
			 tempX = 0;
		 }else if(tempX + width >= src_imagesizeX){
			 tempX = src_imagesizeX - width;
		 }

		 if(tempY < 0){
			 tempY = 0;
		 }else if(tempY + length >= src_imagesizeY){
			 tempY = src_imagesizeY - length;
		 }
		 if(tempX != image_posX || tempY != image_posY){
		 
			 moveX = -(tempX - image_posX);
			 moveY = -(tempY - image_posY);
			 mobilraw.moveBitmap(image, moveX, moveY);
			 if(moveX >= 0){
				 destX = 0;
				 srcX = tempX;
				 
				 destY 	= 0;
				 srcY	= tempY;
				 twidth = moveX;
				 theight= length;
			 }else{
				 destX 	= width + moveX;
				 srcX	= tempX + destX;
				 
				 destY 	= 0;
				 srcY	= tempY;
				 twidth = -moveX;
				 theight= length;
				 
			 }
			 move[0] = width;
			 move[1] = twidth;
			 move[2] = theight;
			 move[3] = destX;
			 move[4] = destY;
			 move[5] = srcX;
			 move[6] = srcY;
			 mobilraw.moveBitmap(image, move);

			 if(moveY >= 0){
				destY	= 0;
				srcY	= tempY;
				theight	= moveY;
				if(moveX >= 0){
					destX 	= moveX;
					twidth	= width - moveX;
					srcX	= tempX + destX;
				}else{
					destX	= 0;
					twidth	= width + moveX;
					srcX	= tempX;
				}
			 }else{
				 destY		= theight + moveY;
				 srcY		= tempY + destY;
				 theight 	= -moveY;
				 if(moveX >= 0){
					destX 	= moveX;
					twidth	= width - moveX;
					srcX	= tempX + destX;
				}else{
					destX	= 0;
					twidth	= width + moveX;
					srcX	= tempX;
				}
			 }
			 move[0] = width;
			 move[1] = twidth;
			 move[2] = theight;
			 move[3] = destX;
			 move[4] = destY;
			 move[5] = srcX;
			 move[6] = srcY;
			 mobilraw.moveBitmap(image, move);			 
			 
			 	 
			 image_posX = tempX;
			 image_posY = tempY;
			 
				 
			 invalidate();
		 }
	 }

	 private void drawImage(){
		 int width	= image.getWidth();
		 int length	= image.getHeight();	
		 int move[] = new int[7];
		 
		 move[0] = width;
		 move[1] = width;
		 move[2] = length;
		 move[3] = 0;
		 move[4] = 0;
		 move[5] = image_posX;
		 move[6] = image_posY;
		 mobilraw.moveBitmap(image, move);

		 
		 invalidate();
	 }
	 
	/* 
	 int farbe = 0x00ff0000;
	 void loadImage(int dest[], int destX, int destY, int srcX, int srcY, int destlinestride, int width, int length){
		 int posX, posY, end_posX;
		 
		 for(posY = destY; posY < destY + length; posY++){
			 posX = posY * destlinestride + destX;
			 end_posX = posX + width;
			 for(;posX < end_posX; posX++){
				 dest[posX] = farbe;
			 }
		 }
	 }
*/

}
