package com.backfire.Animation;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;

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

public class AnimationChatToast  extends Animation{
	public static final int ToastTag =  2015;
	/** 提示信息 */
	private String tips;

	/** 背景图比文字要大一点 */
	private final int bgExtraPx = 20;
	private float startLength;
	private int toast_bg;
	private int textWidth;
	private int textHeight;
	private int chatX;
	private int chatY;
	private int wordX;
	private int wordToX;
	private float wordY;
	/**是否需要移动**/
	private boolean isSizeFit;
	public int seatid;
	/**总长度**/
	private float width;
	private Path path;
	private final long WIDTH_TIME = 6000;
	private long timestart;
	private long timeend;
	private Rect rect;
	public AnimationChatToast(int seatId,int icon,String msg, int x,int y,int p){
		this.deleteFlag = false;
		this.tag = ToastTag;
		this.seatid = seatId;
		toast_bg = icon;
		this.tips = msg;
		chatX = x;
		chatY = y;
		Paint paint = CriticalPool.getPaint();
		paint.setTextSize(ConfigCenter.room_chatFontSize);
		textWidth = (int) paint.measureText(tips) + 1;
	
		if(textWidth+ConfigCenter.room_chat_offset_W>ConfigCenter.room_chatW){
			isSizeFit=true;
			wordX=chatX+textWidth+ConfigCenter.room_chat_offset_W;
			wordToX=this.chatX+ConfigCenter.room_chat_offset_W;
			width=textWidth;
		}else{
			isSizeFit=false;
			wordToX=wordX=chatX+ConfigCenter.room_chat_offset_W;
			width=ConfigCenter.room_chatW;
		}
		wordY=chatY+(ConfigCenter.room_chatH-ConfigCenter.room_chatFontSize)/2f+ConfigCenter.room_chatFontSize;
		rect=new Rect(chatX, chatY, chatX+ConfigCenter.room_chatW, chatY+ConfigCenter.room_chatH);
		
		path = new Path();
		path.moveTo(wordToX,wordY);
		path.lineTo(wordX, wordY);
		startLength = 0;
		timestart = 0;
		timeend = System.currentTimeMillis();
	}
	
	@Override
	public void exec(PCanvas canvas,long sleep) {
		super.exec(canvas,sleep);
		timestart = timeend;
		timeend = System.currentTimeMillis();
        Paint paint = CriticalPool.getPaint();
		paint.setAntiAlias(true);// 文字显示不要抗锯齿，减小内存消耗
		paint.setColor(Color.YELLOW);
		paint.setTextSize(ConfigCenter.room_chatFontSize);
		paint.setTypeface(Typeface.SERIF);
		Bitmap bg = null;
		bg = DdzApplication.getApplication().getBmpPool()
				.getBitmap(toast_bg, ConfigCenter.room_chatW, ConfigCenter.room_chatH);
		if (bg != null) {
			canvas.drawBitmap(bg, chatX ,chatY, null);
		}
		if (!isSizeFit) {
			canvas.drawText(tips, wordX,wordY, paint);
		} else {
			canvas.save();
			canvas.clipRect(rect);
			canvas.drawTextOnPath(tips, path, startLength, 0, paint);
			canvas.restore();
		}
		float step = (width*(timeend-timestart))/WIDTH_TIME;
		
		startLength-= step;
		if(startLength<=-width){
			this.deleteFlag = true;
			path=null;
		}
	}
	
	@Override
	public void unload() {
		// TODO Auto-generated method stub
		
	}

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


