package com.view;

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

import viewbg.EventItem;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScrolledEventsView extends View implements OnGestureListener{
	
	private ImageView[] iv_pics;
	private String de_pic_dir="R.drawable.logo";
	private ImageView iv_up,iv_down;
	private String[] st_names;
	private TextView[] tv_names;
	private LinearLayout llayout,playout,nlayout;
	static final int CANVAS_TOP=100;
	static final int CANVAS_LEFT=0;
	
	private EventItem[] items;
	private GestureDetector detector;
	
	private Canvas canvas;
	Bitmap bmp1;//source bitmap
	Bitmap bmp2;//centric bitmap
	Bitmap bmp4;//bitmap scaled as long_width*height;
	Bitmap bmp6;//bitmap on the top;
	Bitmap bmp5;//bitmap on the layer1 bottom
	Bitmap bmp7;//bitmap on the layer2 bottom
	int short_width=140;
	int long_width=180;
	int short_width1=140;int long_width1=180;
	int short_width2=140;int long_width2=180;
	int centric_width=150;
	int centric_height=150;
	int height0=40;//height of top;
	int height1=40;//height of layer1 bottom
	int height2=60;//height of layer2 bottom
	int top0=10;//top of top;
	int top1=210;//top of layer1 bottom
	int top2=230;//top of layer2 bottom
	int topc=55;
	int height_s=35;
	String[] des=new String[]
	                        {
			"Description for event1...","Description for event2...",
			"Description for event3...","Description for event4..."
	                        };
	int[] des_tops=new int[]{20,65,225,270};
	public ScrolledEventsView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		this.setFocusableInTouchMode(true);
		detector=new GestureDetector(this);
	
		bmp1=BitmapFactory.decodeResource(this.getResources(), R.drawable.eg);
		bmp2=Bitmap.createScaledBitmap(bmp1, centric_width, centric_height, true);
		//int short_width=140;
		//int long_width=180;
		//int height=40;
		bmp4=Bitmap.createScaledBitmap(bmp1, long_width1, height1, true);
		bmp5=Bitmap.createBitmap(long_width1, height1, Bitmap.Config.ARGB_8888);
		int[] src=new int[long_width1*(height1+1)];
		bmp4.getPixels(src, 0, long_width1, 0, 0, long_width1, height1);
		bmp5.setPixels(convertToDown(src,short_width1,long_width1,height1), 0, long_width1, 0, 0, long_width1, height1);
		
		bmp4=Bitmap.createScaledBitmap(bmp1, long_width, height0, true);
		bmp6=Bitmap.createBitmap(long_width, height0, Bitmap.Config.ARGB_8888);
		int[] src6=new int[long_width*height0+long_width];
		bmp4.getPixels(src6, 0, long_width, 0, 0, long_width, height0);
		bmp6.setPixels(convertToUp(src6,short_width,long_width,height0), 0, long_width, 0, 0, long_width, height0);
		
		//int height2=50;
		bmp7=Bitmap.createBitmap(long_width2, height2, Bitmap.Config.ARGB_8888);
		int[] src7=new int[long_width2*height2+long_width2];
		Bitmap.createScaledBitmap(bmp1, long_width2, height2, true).getPixels(src7, 0, long_width2, 0, 0, long_width2, height2);
		bmp7.setPixels(convertToDown(src7,short_width2+10,long_width2,height2), 0, long_width2, 0, 0, long_width2, height2);
	}
	public void scaleUp(int short_width,int long_width,int height)
	{
		int[] src6=new int[long_width*height+long_width];
		Bitmap.createScaledBitmap(bmp1, long_width, height, true).getPixels(src6, 0, long_width, 0, 0, long_width, height);
		bmp6.setPixels(convertToUp(src6,short_width,long_width,height), 0, long_width, 0, 0, long_width, height);
		this.postInvalidate();
	}
	public void scaleDown(int layer,int short_width,int long_width,int height)
	{
		int[] src6=new int[long_width*height+long_width];
		Bitmap.createScaledBitmap(bmp1, long_width, height, true).getPixels(src6, 0, long_width, 0, 0, long_width, height);
		
		if(layer==1)
			bmp5.setPixels(convertToDown(src6,short_width,long_width,height), 0, long_width, 0, 0, long_width, height);
		else if(layer==2)
			bmp7.setPixels(convertToDown(src6,short_width,long_width,height), 0, long_width, 0, 0, long_width, height);
		this.postInvalidate();	
	}
	public ScrolledEventsView(Context context,EventItem[] items)
	{
		this(context);
		
		this.items=items;
		this.iv_pics=new ImageView[items.length];
		this.st_names=new String[items.length];
		
		for(int i=0;i<items.length;i++)
		{
			iv_pics[i]=new ImageView(context);
			iv_pics[i].setImageDrawable(items[i].getImg());
			
			st_names[i]=items[i].getName();
			tv_names[i]=new TextView(context);
			tv_names[i].setText(st_names[i]);
			
		}
		int[] tv_tops=new int[]{30,100,175,250};
		for(int i=0;i<4;i++)
		{
			playout.addView(iv_pics[i]);
			nlayout.addView(tv_names[i]);
			tv_names[i].layout(0, tv_tops[i], 80, tv_tops[i]+25);
			}
		
		}
	public void recoverTops()
	{
		top0=10;top1=210;top2=230;topc=55;
		Log.e("Recover","Tops recovered rrrrrrrrrrrrrrrrrrrrrrr");
		this.postInvalidate();
	}
	public  void recoverHeights()
	{
		centric_height=150;
		height0=40;
		height1=40;
		height2=60;
		scaleUp(long_width,short_width,height0);
		scaleDown(1,long_width1,short_width1,height1);
		scaleDown(2,long_width2,short_width2,height2);
		Log.e("Recover","Heights recovered rrrrrrrrrrrrrrrrrrrrrrr");
		//this.postInvalidate();
	}
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		/*
		 * when connecting to server successfully, the following statement should be:
		 * Bitmap.decodeStream(...)
		 */
		
//		canvas.restore();
		top0=10;top1=210;top2=230;topc=55;
		canvas.drawBitmap(bmp6, CANVAS_LEFT+5, CANVAS_TOP+top0, null);
//		this.canvas=new Canvas(bmp6);
		canvas.drawBitmap(bmp2, CANVAS_LEFT+20, CANVAS_TOP+topc, null);
		canvas.drawBitmap(bmp7, CANVAS_LEFT+5, CANVAS_TOP+top2, null);
		canvas.drawBitmap(bmp5, CANVAS_LEFT+5, CANVAS_TOP+top1, null);
		//Log.e("Test data",top1+" top1+++++++++");
		
		//shade
		Bitmap bmp_s=Bitmap.createBitmap(long_width2, height_s, Bitmap.Config.ARGB_8888);
		bmp_s.setPixels(convertToShadow(bmp1), 0, long_width2, 0, 0, long_width2, height_s);
		canvas.drawBitmap(bmp_s, CANVAS_LEFT+5, CANVAS_TOP+300, null);
		
		Paint paint=new Paint();
		paint.setColor(color.white);paint.setTextSize(16);
		for(int i=0;i<4;i++)
			canvas.drawText(des[i], CANVAS_LEFT+195, CANVAS_TOP+des_tops[i], paint);
		//canvas.save();
		
	}

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

	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
		
	}
//	boolean isCurrentTaskRunning=false;
	Timer timer;
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		int x=(int) e2.getX();
		int y=(int) e2.getY();
		/*
		if(y>=CANVAS_TOP+55 && y<=CANVAS_TOP+210)
		{
			//centric_height=y-CANVAS_TOP-60;
		//	
		//	if(y<=CANVAS_TOP+130 && distanceX<0)
		//		topc=150-centric_height;*/
			
			/*
			 * distanceY>0 upward
			 * 		if height==150 	向上缩
			 * 		if topc!=55 	向上伸
			 * distanceY<0 downward
			 * 		if height==150	 向下缩
			 * 		if topc!=55		向下伸
			 */
			/*
			if(distanceY>0)
			{
				if(centric_height==150)
				{
					topc=55;
					centric_height=y-CANVAS_TOP-55;
				}
				if(topc!=55)
				{
					centric_height=CANVAS_TOP+55+150-y;
					topc=y-CANVAS_TOP;
				}
			}
			else
			{
				if(centric_height==150)
				{
					topc=y-CANVAS_TOP;
					centric_height=205-topc;
				}
				if(topc!=55)
				{
					topc=55;
					centric_height=y-CANVAS_TOP-55;
				}
			}
		}*/
		int y0=(int) e1.getY();
		int removeDis=(y0-y>=0)?(y0-y):(y-y0);
		if(distanceY>0)
		{
			if(removeDis%100<60)
			{
				int gap=(int)((removeDis%100)/2);
				top0-=gap;
				topc-=gap;
				top1-=gap;
				top2-=gap;
			}
			else if(removeDis%100>60)
			{
				recoverTops();
				timer=new Timer(true);
				timer.schedule(timer_item3, 0, 30);
				//time_cnt++;
				if(height1<=10)
				{	timer.cancel();time_cnt=0;timer.purge();}
				recoverHeights();
			}
//			Log.e("Dir", "UP?++++++++++++++++++++++++++++");
		}
		else
		{
			int disY=(int) -distanceY;
			if(removeDis%100<60)
			{
				int gap=(removeDis%100)/2;
				top0+=gap;
				topc+=gap;
				top1+=gap;
				top2+=gap;
			}
			else if(removeDis%100>60)
			{
				recoverTops();
				Timer timer=new Timer(true);
				timer.schedule(timer_item1, 0, 30);
				//time_cnt++;
				if(height0<=10)
				{	timer.cancel();time_cnt=0;timer.purge();}
				recoverHeights();
			}
		}
		ScrolledEventsView.this.postInvalidate();
		Log.e("Scroll","Scroll Init sssssssssssssssssssssssssssssss");
		return true;
	}

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

	double time_cnt=0;
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		int x=(int) e.getX();
		int y=(int) e.getY();
		if(y>=CANVAS_TOP+5 && y<=CANVAS_TOP+50)
		{
			if(timer==null){
			timer=new Timer(true);
			timer.schedule(timer_item1, 0, 30);
			
			}
			
		}
		else if(y>=CANVAS_TOP+210 && y<=CANVAS_TOP+255)
		{
			if(timer==null)
			{	timer=new Timer(true);
			timer.schedule(timer_item3, 0, 30);
			//time_cnt++;
			}
			
			
		}
		else if(y>=CANVAS_TOP+255 && y<=CANVAS_TOP+290)
		{
			if(timer==null)
			{
			timer=new Timer(true);
			timer.schedule(timer_item4, 0, 30);
			//time_cnt++;
			}
			
		}
		return true;
	}
	
	
	public boolean onTouchEvent(MotionEvent event)
	{
		//GestureDetector listener= new GestureDetector(this);
//		if(!(event.getAction()==MotionEvent.ACTION_UP))
			detector.onTouchEvent(event);
		
		return true;
		
	}
	
	public int[] convertToDown(int[] src,int width_up,int width,int height)
	{
		//int len=(int) Math.sqrt(src.length);
		//int wid=len/2
		int bg=this.getDrawingCacheBackgroundColor();
		int pixels[]=new int[width*height+width];
		double unit=((double)(width-width_up))/((double)height*2);
		for(int y=0;y<height;y++)
		{
			double y_unit=((double)width)/((double)(width-y*unit*2));
			for(int x=0;x<width-2*unit*(height-y);x++)
			{
				pixels[(int)(unit*(height-y))+y*width+x]=src[y*width+(int)(x*y_unit)];
			}
		}
		int index,r,g,b;
		int alpha=0x05000000;
		for(int y=0;y<height;y++)
		{	for(int x=0;x<width;x++)
			{
				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;
			}
		alpha+=0x05000000;
		}
		for(int y=0;y<height;y++)
		{
			for(int x=0;x<height/2-y/2;x++)
			{
				pixels[y*width+x]=bg;
				pixels[y*width+width-1-x]=bg;
			}
		}
		return pixels;
	}
	public int[] convertToShadow(Bitmap src_bmp)
	{
		int h=height_s;
		int[] src=new int[long_width2*h];
	//	int[] dst=new int[long_width2*50];
		Bitmap.createScaledBitmap(src_bmp, long_width2, h, true).getPixels(src, 0, long_width2, 0, 0, long_width2, h);
		for(int j=0;j<h/2;j++)
		{
			for(int i=0;i<long_width2;i++)
			{
				int tmp=src[j*long_width+i];
				src[j*long_width2+i]=src[(h-1-j)*long_width2+i];
				src[(h-1-j)*long_width2+i]=tmp;
			}
		}
		int index,r,g,b;
		int alpha=0x00000000;
		for(int y=h-1;y>=0;y--)
		{	for(int x=0;x<long_width2;x++)
			{
				index=y*long_width2+x;
				r=(src[index]>>16) & 0xff;
				g=(src[index]>>8)&0xff;
				b=src[index]&0xff;
				
				src[index]=alpha | (r<<16)|(g<<8)|b;
			}
		alpha+=0x02000000;
		}
		
		return src;
		
	}
	public int[] convertToUp(int[] src,int width_up,int width,int height)
	{
		int bg=this.getDrawingCacheBackgroundColor();
		double unit=((double)(width-width_up))/((double)height*2);
		int pixels[]=new int[width*height+width];
		
		for(int y=0;y<height;y++)
		{
			double y_unit=((double)width)/((double)(width-y*unit*2));
			for(int x=0;x<width-y*unit*2;x++)
			{
				pixels[(int)(unit*y)+y*width+x]=src[y*width+(int)(x*y_unit)];
			}
		}
		int index,r,g,b;
		int alpha=0x05000000;
		for(int y=height-1;y>=0;y--)
		{	for(int x=0;x<width;x++)
			{
				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;
			}
			alpha+=0x06000000;
		}
		for(int y=0;y<height;y++)
		{
			for(int x=0;x<unit*y;x++)
			{
				pixels[y*width+x]=bg;
				pixels[y*width+width-1-x]=bg;
			}
		}
		return pixels;
	}
	final static int[] item_indexes=new int[]{1,2,3,4};
	TimerTask timer_item1=new TimerTask()
	{
		public void run()
		{
			//time_cnt++;
			Message msg=new Message();
			msg.what=item_indexes[0];
			handler.sendMessage(msg);
			if(time_cnt>=28) 
				{
				handler.removeMessages(item_indexes[0]);
				timer.cancel();
				time_cnt=0;
				timer.purge();
				this.cancel();timer=null;
				recoverHeights();
				recoverTops();
				Log.e("End", "Task 1 finished fffffffffffffffffffff");
				
				}
			
		}
	};
	TimerTask timer_item3=new TimerTask()
	{
		public void run()
		{
			time_cnt++;
			Message msg=new Message();
			msg.what=item_indexes[2];
			handler.sendMessage(msg);
			if(height1<=10) 
			{this.cancel();
			recoverHeights();recoverTops();
			handler.removeMessages(item_indexes[2]);
			timer.cancel();
			time_cnt=0;
			timer.purge();
			timer=null;
			Log.e("End", "Task 3 finished fffffffffffffffffffff");
			}
			
		}
	};
	TimerTask timer_item4=new TimerTask()
	{
		public void run()
		{
			time_cnt++;
			Message msg=new Message();
			msg.what=item_indexes[3];
			handler.sendMessage(msg);
			if(height2<=15) 
			{this.cancel();
			recoverHeights();recoverTops();
			handler.removeMessages(item_indexes[3]);
			timer.cancel();
			time_cnt=0;
			timer.purge();
			timer=null;
			Log.e("End", "Task 4 finished fffffffffffffffffffff");
			}
		}
	};
	
	final Handler handler=new Handler()
	{
		//public double time_cnt=0;
		public void handleMessage(Message msg)
		{
			switch(msg.what)
			{
			case 1:
			{
				Log.e("anim", "Anim begins,time_cnt="+time_cnt+"*****************");
				time_cnt++;
				height0--;top0++;
		//		if(time_cnt>=28) 
			//		{this.removeMessages(1);
			//		}
				scaleUp(short_width,long_width,height0);
			//	ScrolledEventsView.this.postInvalidate();	
				break;
			}
			case 3:
			{
				height1--;
				if(height1<=10) this.removeMessages(3);
				scaleDown(1,short_width1,long_width1,height1);
			//	ScrolledEventsView.this.postInvalidate();	
				break;
			}
			case 4:
			{
				if(height2<=15) this.removeMessages(4);
				if(height1>=10) {height1-=2;height2--;}
				else
					{height2--;scaleDown(2,short_width2,long_width2,height2);}
				
				//ScrolledEventsView.this.postInvalidate();
			}
			}
		}
	};
	
}
