package com.backfire.Animation;

import java.util.concurrent.CopyOnWriteArrayList;

import net.yeeyo.doudizhu.feiyu.R;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;

import com.backfire.arithmetic.Card;
import com.backfire.doudizhu.common.BitmapManager;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.graphics.PCanvas;
import com.backfire.room.data.ConfigCenter;
import com.backfire.room.data.ResCenter;

/**
 * @Title: AnimationSelectLaizi.java
 * @Package com.backfire.Animation
 * @Description: 抽选癞子动画
 * @author Potter
 * @date 2013-12-17 上午10:58:20
 * @version V2.0
 */
public class AnimationSelectLaizi extends Animation {

	private static enum Dir {
		LEFT, RIGHT
	}

	private static enum State {
		SHAKE, RISE, MOVE, FINISH
	}

	private int lastTime = 0;
	private long playTime = 6;

	private State state = State.SHAKE;

	private static final float startX = (DdzApplication.getApplication().screenWidth - ConfigCenter.self_hand_card_W) / 2;
	private static final float startY = (DdzApplication.getApplication().screenHeight - ConfigCenter.self_hand_card_H) / 2;
	private static final int desW = ConfigCenter.out_card_W;
	private static final int desH = ConfigCenter.out_card_H;

	private int desX = 0;// 目的x坐标
	private int desY = 0;// 目的y坐标

	private float desScaleX = ConfigCenter.out_card_W
			/ (float) ConfigCenter.self_hand_card_W;
	private float desScaleY = ConfigCenter.out_card_H
			/ (float) ConfigCenter.self_hand_card_H;

	private float moveStepVX = 0;// 移动过程，移动x方向的移动速度
	private float moveStepVY = 0;// 移动过程，移动y方向的移动速度
	private float moveScaleXV = 0;// 移动过程，移动x方向的缩放速度
	private float moveScaleYV = 0;// 移动过程，移动y方向的缩放速度

	private float moveScaleX = 1;
	private float moveScaleY = 1;

	private float posX = 0;
	private float posY = 0;

	private Dir dir = Dir.LEFT;

	private long useTime = 0;

	private Matrix matrix = null;

	private Card card = null;

	// ----------------------摇癞子过程-------------------------------
	private final int[] textures = { R.drawable.select_anim_01,
			R.drawable.select_anim_02, R.drawable.select_anim_01,
			R.drawable.select_anim_02, R.drawable.select_anim_05 };
	private final int PLAY_TIMES = 4;// 播放次数
	private int index = 0;
	private int playTimes = 0;
	private final int width = ConfigCenter.select_laizi_anim_W;
	private final int hight = ConfigCenter.select_laizi_anim_H;
	private final int x = (DdzApplication.getApplication().screenWidth - width) / 2;
	private final int y = (DdzApplication.getApplication().screenHeight - hight) / 2;

	private Rect srcRect = null;
	private Rect displayRect = null;
	// ----------------------上升过程--------------------------------
	private int riseH = ConfigCenter.self_hand_card_H; // 上升高度
	private float riseV = 0; // 上升速度
	private long riseSurplusTime = 0;// 上升剩余时间
	private int riseStartH = 0;

	public AnimationSelectLaizi(int time, int desX, int desY, Card card) {
		this.card = new Card(card);
		srcRect = new Rect();
		srcRect.set(x, y, x + width, y + hight);
		displayRect = new Rect();
		displayRect.set(0, 0, DdzApplication.getApplication().screenWidth, y
				+ ConfigCenter.select_laizi_offset_H);

		deleteFlag = false;
		lastTime = time;
		this.desX = desX;
		this.desY = desY;

		if (desX > startX) {
			dir = Dir.LEFT;
		} else {
			dir = Dir.RIGHT;
		}

		matrix = new Matrix();
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	@Override
	public void exec(PCanvas canvas, long sleep) {
		super.exec(canvas, sleep);
		useTime += this.sleep;
		if (playTimes < PLAY_TIMES) {
			index = getCurrentBitmap(useTime, textures);
			if (index == textures.length - 1) {
				index = 0;
				playTimes++;
				if (playTimes >= PLAY_TIMES) {// 绘制最后一帧
					index = textures.length - 1;
					state = State.RISE;// 上升过程
					long riseUsableTime = (lastTime - useTime) / 2;
					initRiseData(riseUsableTime);
				}
			}
		}
		BitmapManager bpool = DdzApplication.getApplication().getBmpPool();
		Bitmap texture = bpool.getBitmap(textures[index], width, hight);
		if (null != texture) {
			canvas.drawBitmap(texture, null, srcRect, null);
		}

		if (state != State.SHAKE) {
			// 根据使用时间，来控制状态
			if (State.RISE == state && useTime >= lastTime - riseSurplusTime) {
				state = State.MOVE;
				initMoveData();
			}
			float[] valuse = null;
			matrix.reset();
			switch (state) {
			case RISE:// 上升
				valuse = new float[] { 1, 0, posX, 0, 1, posY, 0, 0, 1 };
				matrix.setValues(valuse);
				posY = (posY - sleep * riseV);
				if (posY < riseStartH - riseH) {
					state = State.MOVE;
					initMoveData();
				}
				// 控制显示区域
				drawCard(canvas, matrix, card);
				break;
			case MOVE:// 移动到指定位置
				valuse = new float[] { moveScaleX, 0, posX, 0, moveScaleY,
						posY, 0, 0, 1 };
				matrix.setValues(valuse);
				posX = (posX + sleep * moveStepVX);
				posY = (posY + sleep * moveStepVY);
				if (isCheckBoundary()) {
					this.deleteFlag = true;
				}
				moveScaleX = moveScaleX - (sleep) * moveScaleXV;
				moveScaleY = moveScaleY - (sleep) * moveScaleYV;
				if (moveScaleX <= desScaleX || moveScaleY <= desScaleY) {
					moveScaleX = desScaleX;
					moveScaleY = desScaleY;
				}
				drawCard(canvas, matrix, card);
				break;
			}
		}
	}

	public void drawCard(PCanvas canvas, Matrix matrix, Card card) {
		if (null == card)
			return;

		Bitmap bitmap = ResCenter.getCardImg(card);
		if (null == bitmap)
			return;

		canvas.save();
		canvas.clipRect(displayRect);

		canvas.drawBitmap(bitmap, matrix, null);
		if (card.isSmallLaizi()) {
			Bitmap flower = ResCenter.getFlower();
			if (flower != null) {
				canvas.drawBitmap(flower, matrix, null);
			}
		}

		canvas.restore();
	}

	/**
	 * 是否到达边界
	 * 
	 * @return
	 */
	public boolean isCheckBoundary() {
		boolean result = false;
		switch (dir) {
		case LEFT:
			if (posX <= desX && posY <= desY) {
				result = true;
			}
			break;
		case RIGHT:
			if (posX >= desX && posY <= desY) {
				result = true;
			}
			break;
		}
		return result;
	}

	public void initRiseData(long usableTime) {
		riseSurplusTime = usableTime;
		riseV = (ConfigCenter.select_laizi_rise_H / (float) riseSurplusTime)
				* ConfigCenter.select_laizi_rise_V;
		riseStartH = y + ConfigCenter.select_laizi_offset_H;
		posX = startX;
		posY = riseStartH;
	}

	public void initMoveData() {
		moveScaleX = 1;
		moveScaleY = 1;
		this.moveStepVX = ((desX - startX) / (float) (lastTime - useTime))*ConfigCenter.select_laizi_move_V;
		this.moveStepVY = ((desY - startY) / (float) (lastTime - useTime))*ConfigCenter.select_laizi_move_V;

		this.moveScaleXV = ((desW / (float) ConfigCenter.self_hand_card_W)
				/ (float) (lastTime - useTime))*ConfigCenter.select_laizi_scale_V;
		this.moveScaleYV = ((desH / (float) ConfigCenter.self_hand_card_H)
				/ (float) (lastTime - useTime))*ConfigCenter.select_laizi_scale_V;
	}

	@Override
	public void unload() {

	}

	@Override
	public void overstatue() {
		this.deleteFlag = true;
	}
}
