package com.backfire.doudizhu.luck;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.backfire.doudizhu.common.CriticalPool;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.room.data.ConfigCenter;

public class ZhuanPanView extends View implements Runnable {
	// 界面需要的图片
	private Bitmap zhuanpanImage;
	// 旋转矩阵
	private Matrix matrix = new Matrix();

	private boolean ifRotate = false;

	private int gift;
	
	private ZhuanPanStopListener stopListener;

	private Paint paint = CriticalPool.getPaint();

	private PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0,
			Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

	private int startingX = 0; // 转盘转动的起始位置

	private byte[] zhuan_sync = new byte[1];

	private LuckActivity context;


	private Button choujiang_anniu;
	

	public volatile boolean flag = true;
	
	private boolean isdraw = true;
	
	private boolean startStop = false;
	
	private int needRotate = 0;
	private int reduceRotate = 0;
	private final int width_pan = 252;

	public ZhuanPanView(Context context, AttributeSet attrs) {

		super(context, attrs);

	}

	public void setContext(LuckActivity activity, Bitmap zhuanpanImg, Button choujiang_anniu) {
		this.context = activity;
		this.choujiang_anniu = choujiang_anniu;
		// 生成图片
		if (zhuanpanImage == null && zhuanpanImg!=null) { // 从本地缓存取图片
			zhuanpanImage = DdzApplication.getApplication().getBmpPool().getBitmap(zhuanpanImg, 
					ConfigCenter.adaptW(width_pan*2), ConfigCenter.adaptW(width_pan*2));
			isdraw = true;
		}
		// 这个函数强制UI线程刷新界面
		this.postInvalidate();

	}
	// 重写View类的onDraw()函数
	protected void onDraw(Canvas canvas) {
		if (!isdraw) {
			matrix.reset();
			matrix.setRotate(gift, ConfigCenter.adaptW(width_pan), ConfigCenter.adaptW(width_pan));
			paint.setAntiAlias(true);
			canvas.setDrawFilter(pfd);
			if (zhuanpanImage != null) {
				canvas.drawBitmap(zhuanpanImage, matrix, paint);
			}
			return;
		}
		// 设置转盘旋转矩阵为以（252,252）坐标为圆心，旋转X角度
		synchronized (zhuan_sync) {
			gift = this.startingX;
		}
		matrix.reset();
		matrix.setRotate(gift, ConfigCenter.adaptW(width_pan), ConfigCenter.adaptW(width_pan));

		// 加抗锯齿
		paint.setAntiAlias(true);
		canvas.setDrawFilter(pfd);
		if (zhuanpanImage != null) {
			canvas.drawBitmap(zhuanpanImage, matrix, paint);
		}

	}

	// 重写的run函数，用来控制转盘转动
	public void run() {
		try {
			while (flag) {

				if (ifRotate) {
					synchronized (zhuan_sync) {
						if(startStop){
							this.startingX += 9-reduceRotate;
							if (this.startingX >= 360) {
								this.startingX = 0;
								needRotate -=360; 
								reduceRotate++;
							}
							if(needRotate - startingX<=0){
								this.ifRotate = false;
								new Handler(context.getMainLooper()).post(new Runnable() {
									
									@Override
									public void run() {
										stop();
									}
								});
							}
						}else{
							this.startingX += 9;
							if (this.startingX >= 360) {
								this.startingX = 0;
							}
						}
					}

					// 这个函数强制UI线程刷新界面
					this.postInvalidate();

					Thread.sleep(30);

				}

			}

		} catch (InterruptedException e) {

			e.printStackTrace();
		}

	}

	public void startRotate() {
		synchronized (zhuan_sync) {
			isdraw=true;
			this.ifRotate = true;
			startStop = false;
			needRotate = 0;
			reduceRotate = 0;
			if(choujiang_anniu!=null)choujiang_anniu.setEnabled(false);
		}
	}
	private void stop(){
		synchronized (zhuan_sync) {
			isdraw=false;
			this.ifRotate = false;
			startStop = false;
			needRotate = 0;
			reduceRotate = 0;
			if(choujiang_anniu!=null)choujiang_anniu.setEnabled(true);
		}
		if(stopListener!=null){
			stopListener.onFinish();
		}
	}
	public void stopRotate(int degree,ZhuanPanStopListener stopListener) {
		synchronized (zhuan_sync) {
			needRotate = degree + 8*90;
			reduceRotate = 0;
			startStop = true;
			this.stopListener = stopListener;
		}
	}
	public boolean isRotate() {
		
		 return this.ifRotate;
	}
	public void clear(){
		flag = false;
	}
}
