package com.backfire.room.surface;

import java.util.Calendar;

import net.yeeyo.doudizhu.feiyu.R;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Typeface;

import com.backfire.arithmetic.Card;
import com.backfire.arithmetic.CardAssist;
import com.backfire.doudizhu.common.BitmapManager;
import com.backfire.doudizhu.common.CriticalPool;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.common.PNumber;
import com.backfire.doudizhu.userinfo.UserMessageCenter;
import com.backfire.graphics.PCanvas;
import com.backfire.room.data.ConfigCenter;
import com.backfire.room.data.ResCenter;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.State.GameState;

public class RoomSurface {
	private Bitmap room_bg = null;
	// private Bitmap seaBorder = null;
	private Bitmap toolBarBorder = null;

	private String timeString = null;
	private int lastMinute;
	private Bitmap timeBorder = null;

	private Bitmap roomType = null;
	private Bitmap basePointTag = null;
	private Bitmap ratioTag = null;

	private BitmapManager bitmapManager = null;

	private boolean isLastLaiziSeaCardDisplay = false;

	private Card[] backCards = null;

	public RoomSurface() {
		bitmapManager = DdzApplication.getApplication().getBmpPool();
		int screenW = DdzApplication.getApplication().screenWidth;
		int screenH = DdzApplication.getApplication().screenHeight;

		room_bg = bitmapManager.getBitmap(R.drawable.room_bg, screenW, screenH);
		/*
		 * seaBorder = bitmapManager.getBitmap(R.drawable.dipai_border,
		 * ConfigCenter.seaW, ConfigCenter.seaH);
		 */
		toolBarBorder = bitmapManager.getBitmap(R.drawable.tool_border,
				ConfigCenter.toolbarW, ConfigCenter.toolbarH);

		/*
		 * timeBorder = bitmapManager.getBitmap(R.drawable.time_border,
		 * ConfigCenter.timeBorderW, ConfigCenter.timeBorderH);
		 */

		basePointTag = bitmapManager.getBitmap(R.drawable.dizhu_text,
				ConfigCenter.basePointTagW, ConfigCenter.basePointTagH);
		ratioTag = bitmapManager.getBitmap(R.drawable.beishu_text,
				ConfigCenter.ratioTagW, ConfigCenter.ratioTagH);

		Calendar calendar = Calendar.getInstance();
		lastMinute = calendar.get(Calendar.MINUTE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		timeString = String.format("%02d:%02d", hour, lastMinute);

		backCards = new Card[3];
		for (int i = 0; i < backCards.length; i++) {
			backCards[i] = new Card();
		}
	}

	public void draw(PCanvas canvas, int gameState, int selfState) {
		Paint paint = CriticalPool.getPaint();
		if (room_bg != null) {
			canvas.drawBitmap(room_bg, 0, 0, null);
		}
		drawTime(canvas, paint);

		canvas.drawBitmap(
				toolBarBorder,
				(DdzApplication.getApplication().screenWidth - ConfigCenter.toolbarW) / 2,
				ConfigCenter.toolbarY, null);
		if (gameState >= GameState.GAME_QIANG_ZHUANG) {
			drawSeaCard(canvas, paint);
		} else {// 绘制3张平铺的背面的牌
			drawBackSeaCard(canvas, paint);
		}
		
		drawDownInfo(canvas, paint);
	}

	public static void drawMatrix(PCanvas canvas, Bitmap bitmap, int x, int y,
			float scaleX, float scaleY) {
		if (null == bitmap)
			return;

		Matrix matrix = CriticalPool.getMatrix();
		matrix.reset();
		matrix.setScale(scaleX, scaleY);
		matrix.postTranslate((float) x, (float) y);
		canvas.drawBitmap(bitmap, matrix, null);
	}

	private void drawSeaCard(PCanvas canvas, Paint paint) {
		// canvas.drawBitmap(seaBorder, ConfigCenter.seaX, ConfigCenter.seaY,
		// null);
		boolean isLaiziType=RoomInfo.getInstance().isLaiziType();
		if(isLaiziType){
			drawLaiziSeaCard(canvas, paint);
		}else{
			drawHappySeaCard(canvas, paint);
		}
	}

	private void drawLaiziSeaCard(PCanvas canvas, Paint paint) {
		RoomInfo roomInfo = RoomInfo.getInstance();
		Card[] tmpCommonCards = CardAssist.copyArray(roomInfo.getCommcard());
		int seaX = (DdzApplication.getApplication().screenWidth - ConfigCenter.seaW) / 2;
		int offsetX = ConfigCenter.sea_card_laizi_Off_W;
		int startX = seaX + ConfigCenter.seaW
				- ConfigCenter.sea_card_laizi_gap_border_Off_W
				- (3 * offsetX + ConfigCenter.sea_card_W - offsetX);//3张牌
		drawSeaCard(canvas, paint, tmpCommonCards, startX, offsetX, true);

		Card[] laiziCards = CardAssist.copyArray(roomInfo.getLaizi());
		startX = seaX + ConfigCenter.sea_card_laizi_gap_border_Off_W;
		boolean isLastLaiziDisplay = roomInfo.isLastLaiziSeaCardDisplay();
		drawSeaCard(canvas, paint, laiziCards, startX, offsetX,
				isLastLaiziDisplay);
	}

	public void drawBackSeaCard(PCanvas canvas, Paint paint) {
		int seaX = (DdzApplication.getApplication().screenWidth - ConfigCenter.seaW) / 2;
		int startX = seaX + ConfigCenter.sea_card_noLaizi_gap_border_Off_W;
		int offsetX = ConfigCenter.sea_card_W+ConfigCenter.sea_card_hapyy_Off_W;
		drawSeaCard(canvas, paint, backCards, startX, offsetX, true);
	}

	public void drawHappySeaCard(PCanvas canvas, Paint paint) {
		RoomInfo roomInfo = RoomInfo.getInstance();
		Card[] tmpCards = CardAssist.copyArray(roomInfo.getCommcard());
		int seaX = (DdzApplication.getApplication().screenWidth - ConfigCenter.seaW) / 2;
		int startX = seaX + ConfigCenter.sea_card_noLaizi_gap_border_Off_W;
		int offsetX = ConfigCenter.sea_card_W+ConfigCenter.sea_card_hapyy_Off_W;
		drawSeaCard(canvas, paint, tmpCards, startX, offsetX, true);
	}

	public void drawSeaCard(PCanvas canvas, Paint paint, Card[] cards,
			int startX, int offsetX, boolean isLastLaiziDisplay) {
		if (null == cards)
			return;
		for (int i = 0; i < cards.length; i++) {
			Card card = cards[i];
			if (null != card) {
				if (!isLastLaiziDisplay
						&& (i == RoomInfo.getLastLaiziIndex(cards))) {
					return;
				}
				Bitmap bitmap = ResCenter.getCardImg(card);
				int posX = startX + i * offsetX;
				int posY = ConfigCenter.sea_card_Y;
				if (null != bitmap) {
					canvas.drawBitmap(bitmap, ConfigCenter.sea_card_W,
							ConfigCenter.sea_card_H, posX, posY, null);
					if (card.isSmallLaizi()) {
						Bitmap flower = ResCenter.getFlower();
						if (flower != null) {
							canvas.drawBitmap(flower, ConfigCenter.sea_card_W,
									ConfigCenter.sea_card_H, posX, posY, null);
						}
					}
				}
			}
		}
	}

	private void formatTimeString() {
		Calendar calendar = Calendar.getInstance();
		int minute = calendar.get(Calendar.MINUTE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		if (minute != lastMinute) {
			lastMinute = minute;
			timeString = String.format("%02d:%02d", hour, minute);
		}
	}

	public void drawTime(PCanvas canvas, Paint paint) {
		formatTimeString();

		char[] tmpArrayCharTime = timeString.toCharArray();
		for (int i = 0; i < tmpArrayCharTime.length; i++) {
			Bitmap bitmap = ResCenter.getTimeNumber(tmpArrayCharTime[i],
					ConfigCenter.timeNumW, ConfigCenter.timeNumH);
			canvas.drawBitmap(bitmap, ConfigCenter.timePosX + i
					* (ConfigCenter.timeNumW + ConfigCenter.timeNum_Offset_W),
					ConfigCenter.timePosY, null);
		}

	}

	private void drawDownInfo(PCanvas canvas, Paint paint) {
		RoomInfo roomInfo = RoomInfo.getInstance();
		
		/*GameType gameType = roomInfo.getGameType();
		Bitmap gameTypeImg = null;
		int posX = (DdzApplication.getApplication().screenWidth - ConfigCenter.game_type_text_W) / 2;
		int posY = (DdzApplication.getApplication().screenHeight - ConfigCenter.game_type_text_H) / 2;
		switch (gameType) {
		case HAPPY:
			gameTypeImg = bitmapManager.getBitmap(R.drawable.ddz_happy_text,
					ConfigCenter.game_type_text_W,
					ConfigCenter.game_type_text_H);
			canvas.drawBitmap(gameTypeImg, posX, posY, null);
			break;
		case LAIZI:
			gameTypeImg = bitmapManager.getBitmap(R.drawable.ddz_laizi_text,
					ConfigCenter.game_type_text_W,
					ConfigCenter.game_type_text_H);
			canvas.drawBitmap(gameTypeImg, posX, posY, null);
			break;
		case SUPER_LAIZI:
			gameTypeImg = bitmapManager.getBitmap(R.drawable.ddz_super_laizi_text,
					ConfigCenter.game_type_text_W,
					ConfigCenter.game_type_text_H);
			canvas.drawBitmap(gameTypeImg, posX, posY, null);
		default:
			break;
		}*/
		float posX=(DdzApplication.getApplication().screenWidth) / 2f;
		float posY=(DdzApplication.getApplication().screenHeight - ConfigCenter.roomNameFont_size) / 2f;
		paint.setARGB(130, 14, 73, 134);
		paint.setTextSize(ConfigCenter.roomNameFont_size);
		paint.setFakeBoldText(true);
		paint.setTextAlign(Align.CENTER);
		if(RoomInfo.getInstance().getRoomName()!=null){
			canvas.drawText(RoomInfo.getInstance().getRoomName(), posX, posY, paint);
		}

		canvas.drawBitmap(basePointTag, ConfigCenter.basePointTagX,
				ConfigCenter.basePointTagY, null);
		int basePointArry[] = PNumber.getNumEachPost(roomInfo.getBasepoint());
		for (int i = 0; i < basePointArry.length; i++) {
			Bitmap bitmap = ResCenter.getNumber(basePointArry[i],
					ConfigCenter.yNumberW, ConfigCenter.yNumberH);
			drawText(canvas, bitmap, ConfigCenter.basePointX + i
					* ConfigCenter.yNumberW, ConfigCenter.basePointY);
		}

		canvas.drawBitmap(ratioTag, ConfigCenter.ratioTagX,
				ConfigCenter.ratioTagY, null);
		int rateArry[] = PNumber.getNumEachPost(roomInfo.getRatio());
		for (int i = 0; i < rateArry.length; i++) {
			Bitmap bitmap = ResCenter.getNumber(rateArry[i],
					ConfigCenter.yNumberW, ConfigCenter.yNumberH);
			drawText(canvas, bitmap, ConfigCenter.ratioX + i
					* ConfigCenter.yNumberW, ConfigCenter.ratioY);
		}

	}

	public void drawText(PCanvas PCanvas, Bitmap bitmap, int x, int y) {
		PCanvas.drawBitmap(bitmap, x, y, null);
	}

	public void drawText(PCanvas canvas, String text, int x, int y, int w,
			int h, int font) {

		if (text == null || text.length() == 0) {
			return;
		}
		Paint paint = CriticalPool.getPaint();
		paint.setColor(Color.argb(180, 255, 255, 255));
		paint.setTextSize(font);
		paint.setStyle(Style.FILL);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setFakeBoldText(true);
		Rect rect = CriticalPool.getRect();
		paint.getTextBounds(text, 0, 1, rect);
		int textWidth = (int) (paint.measureText(text));
		if (textWidth > w) {
			int position = paint.breakText(text, true, w - 2, null);
			text = text.substring(0, position) + "..";
			canvas.drawText(text, x, y + (h + rect.bottom - rect.top) / 2
					- ConfigCenter.getFontAddHeight(paint), paint);
		} else {
			int center = textWidth / 2;
			canvas.drawText(
					text,
					x - center + w / 2,
					y + (h + rect.bottom - rect.top) / 2
							- ConfigCenter.getFontAddHeight(paint), paint);
		}
	}
}
