package com.view;

import java.util.Timer;
import java.util.TimerTask;

import basicClass.Event;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;

public class EventsCoverFlow extends View implements OnGestureListener{
	
	private String userName;
	private String userAddr;
	private String userPortrDir;
	private Event[] eventItems;
	
	private GestureDetector detector;
	
	public EventsCoverFlow(Context context)
	{
		super(context);
		// TODO Auto-generated constructor stub
		
		this.setFocusableInTouchMode(true);
		detector=new GestureDetector(this);
		
	}
	
	public EventsCoverFlow(Event[] event, Context context)
	{
		this(context);
		this.eventItems=event;
	}
	
	Bitmap bmp_src;
	Bitmap[] bmp_dst=new Bitmap[5];
	int short_height[]={140,140,200,140,140};
	int long_height[]={180,180,200,180,180};
	int left[]={40,100,160,340,380};
	int width[]={80,80,200,80,80};
	int top_centric=60;
	int top_side=70;
	int top[]={top_side,top_side,top_centric,top_side,top_side};
	int width_centric=200;
	int width_side=80;
	int centric_index=0;
	
	boolean toLeft=true;
	
	String currentEventDes="";
	
	int timer_cnt=20;
	
	public void onDraw(Canvas canvas)
	{
		int begin_bmp_index;
		if(centric_index<3) begin_bmp_index=2-centric_index;
		else begin_bmp_index=0;
		
		
		/*
		 * when connecting to server successfully, the following statement should be:
		 * Bitmap.decodeStream(...)
		 */
		
		bmp_src=BitmapFactory.decodeResource(this.getResources(), R.drawable.eg);
//		for(int i=0;i<centric_index+3;i++)
//			bmp_dst[begin_bmp_index+i]=Bitmap.createScaledBitmap(bmp_src,
//					width[begin_bmp_index+i], long_height[begin_bmp_index+i], true);
		int []src;int[] dst;
		for(int i=begin_bmp_index;i<5;i++)
		{
			bmp_dst[i]=Bitmap.createScaledBitmap(bmp_src,
					width[i], long_height[i], true);
			
			src=new int[width[i]*long_height[i]];dst=new int[width[i]*long_height[i]];
			bmp_dst[i].getPixels(src, 0, width[i], 0, 0, width[i], long_height[i]);
			if(i<2)
				dst=convertToSide(src,long_height[i],short_height[i],width[i]);
			else if(i>2)
				dst=convertToSide(src,short_height[i],long_height[i],width[i]);
			else
			{
				if(toLeft==true)
					dst=convertToSide(src,long_height[2],short_height[2],width[2]);
				else
					dst=convertToSide(src,short_height[2],long_height[2],width[2]);
			}
			
			bmp_dst[i].setPixels(dst, 0, width[i], 0, 0, width[i], long_height[i]);
			
			//if(i!=0) canvas.drawBitmap(bmp_dst[i], left[i], top[i], null);
		}
		
		Log.e("onDraw","EventsCoverFlow success");
	}
	
	public  boolean onTouchEvent(MotionEvent e)
	{
		detector.onTouchEvent(e);
		return true;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		
		int y=(int) e2.getY();
		if(distanceX>0)
		{
			if(distanceX<40)
			{
				for(int i=0;i<5;i++)
					left[i]-=distanceX;
			}
			else if(distanceX>60)
			{
				Timer timer=new Timer(true);
				
				timer.schedule(task_left, 0, 30);
				if(timer_cnt<=0)
				{
					timer.cancel();timer_cnt=20;timer.purge();
				}
			}
		}
		
		else if(distanceX<0)
		{
			if(distanceX>-40)
			{
				for(int i=0;i<5;i++)
					left[i]-=distanceX;
			}
			else if(distanceX<-60)
			{
				Timer timer=new Timer(true);
				timer.schedule(task_right, 0, 30);
				if(timer_cnt<=0)
				{
					timer.cancel();timer_cnt=20;timer.purge();
				}
			}
		}
		EventsCoverFlow.this.postInvalidate();
		
		return true;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public int[] convertToSide(int[] src,int left_height,int right_height,int width)
	{
		int bg=this.getDrawingCacheBackgroundColor();
		int pixels[]=new int[width*right_height+right_height];
		double unit=1;
		int index,r,g,b;
		int alpha=0x05000000;
		boolean centric=false;
		if(left_height==right_height) centric=true;
		
		if(left_height>=right_height)
		{
			pixels=new int[width*left_height+left_height];
			unit=((double)(left_height-right_height))/((double)width*2);
			alpha=0x18000000;
			for(int x=0;x<width;x++)
			{
				for(int y=(int) (unit*x);y<left_height-unit*x;y++)
				{
					int y0=(int) (x*unit);
					double current_h_r=left_height-2*y0;
					int y_orig=(int) ((y-y0)*left_height/current_h_r);
					pixels[y*width+x]=src[y_orig*width+x];
					
					if(!centric)
					{
						index=y*width+x;
						r=(pixels[index]>>16) & 0xff;
						g=(pixels[index]>>8) & 0xff;
						b=pixels[index] & 0xff;
						pixels[index]=alpha| (r<<16) | (g<<8) | b;
					}
				}
				if(!centric)
					alpha-=0x04000000;
				
			}
			
			for(int x=0;x<width;x++)
			{
				for(int y=0;y<unit*x;y++)
				{
					pixels[y*width+x]=bg;
					pixels[width*(left_height-y)+x]=bg;
				}
			}
		}
		else
		{
			alpha=0x0f000000;
			unit=((double)(right_height-left_height))/((double)width*2);
			for(int x=0;x<width;x++)
			{
				for(int y=(int) (unit*(width-x));y<right_height-unit*(width-x);y++)
				{
					int y0=(int) ((width-x)*unit);
					double current_h_l=right_height-2*y0;
					int y_orig=(int) ((y-y0)*right_height/current_h_l);
					pixels[y*width+x]=src[y_orig*width+x];
					if(!centric)
					{
						index=y*width+x;
						r=(pixels[index]>>16) & 0xff;
						g=(pixels[index]>>8) & 0xff;
						b=pixels[index] & 0xff;
						pixels[index]=alpha| (r<<16) | (g<<8) | b;
					}
				}
				if(!centric)
					alpha+=0x03000000;
			}
			for(int x=0;x<width;x++)
			{
				for(int y=0;y<unit*(width-x);y++)
				{
					pixels[y*width+x]=bg;
					pixels[width*(right_height-y)+x]=bg;
				}
			}
		}
		return pixels;
	}

	TimerTask task_left=new TimerTask()
	{
		public void run()
		{
			timer_cnt--;
			if(timer_cnt<=0) this.cancel();
			Message msg=new Message();
			msg.what=0;
			handler.sendMessage(msg);
		}
	};
	
	TimerTask task_right=new TimerTask()
	{
		public void run()
		{
			timer_cnt--;
			if(timer_cnt<=0) this.cancel();
			Message msg=new Message();
			msg.what=1;
			handler.sendMessage(msg);
		}
	};
	
	final Handler handler=new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch(msg.what)
			{
			case 0:
			{
				Log.e("Move Dir", "To Left**************************************");
				width[2]-=6;
				width[3]+=6;
				left[3]+=6;
				if(width[2]<=80) 
				{
					width[2]=200;
					width[3]=80;
					left[3]=320;
				}
				EventsCoverFlow.this.postInvalidate();
				break;
			}
			case 1:
			{
				Log.e("Move Dir", "To Right****************************************");
				width[1]+=6;
				width[2]-=6;
				left[2]+=6;
				if(width[3]<=80)
				{
					width[1]=80;
					width[2]=200;
					left[2]=140;
				}
				EventsCoverFlow.this.postInvalidate();
			}
			}
		}
	};
}
