package com.nodie.runnable;

import java.util.Random;

import com.nodie.R;
import com.nodie.role.Role;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.view.SurfaceHolder;

public class MyThread extends Thread {

	private SurfaceHolder holder;
	private Context context;
	private int w, h;
	private int gameType;

	private boolean isStart = true;
	// 鑾峰緱鍥剧墖
	private int[] paths;
	private Bitmap[] bms;

	//
	private int gameStatu = 0;
	// 淇濆瓨娓告垙鐘舵€?
	public static final int RUNNING = 0;
	public static final int STANDOFF = 1;
	public static final int GAMEOVER = 2;
	// 绮剧伒

	private int gameSpan;

	public Role[] roles;
	private Rect[] rects;
	// 鐢荤瑪
	private Paint paint;

	// 鍒濆鍖栧紑濮嬫椂闂?
	private long beginTime;
	// 涓婁竴鏃堕棿姣
	private long preTime = 0;
	// 璁板綍绗竴娆′綅缃?
	private float fristLocation = 0;

	public MyThread(Context context, SurfaceHolder holder, int w, int h, int gameType) {

		this.holder = holder;
		this.context = context;
		this.w = w;
		this.h = h;
		this.gameType = gameType;

		paint = new Paint();
		// 鍒濆鍖栨暟缁?
		paths = new int[] { R.drawable.jingling1, R.drawable.jingling2 };
		bms = new Bitmap[paths.length];
		for (int i = 0; i < paths.length; i++) {
			// 鏍规嵁璺緞鍔犺浇鍥剧墖
			bms[i] = BitmapFactory.decodeResource(context.getResources(), paths[i]);
		}
		// 娓告垙鍖哄煙
		gameSpan = h * 4 / (5 * gameType);

		roles = new Role[gameType];
		rects = new Rect[gameType];

		// 鍒濆鍖栫簿鐏?
		initSpirit();

	}

	private void initSpirit() {
		// 鍒濆鍖栫簿鐏?
		for (int i = 0; i < gameType; i++) {
			// 鐢荤嚎 Y鍧愭爣
			int lineY = h / 10 + (i + 1) * gameSpan;
			// 鍒涘缓绮剧伒
			Role role = new Role(bms);
			role.setX(w / 8);
			role.setY(lineY - role.getHeight());
			roles[i] = role;
			// 鍒涘缓闅滅鐗?
			Rect rect = new Rect();
			// 闅忔満闅滅鐗╁楂?
			int random_w = (int) (role.getWidth() * (Math.random() * 5 + 1) /(3*gameType));
			int random_h = (int) (role.getHeight() * (Math.random() * 5 + 1) /(3*gameType));

			rect.left = 3 * w / 2+random_w;
			rect.right = rect.left + random_w;
			rect.bottom = lineY;
			rect.top = rect.bottom - random_h;
			rects[i] = rect;
		}
	}

	@Override
	public void run() {
		super.run();
		beginTime = System.currentTimeMillis();
		while (isStart) {
			// 鑾峰彇鐢诲竷
			Canvas canvas = null;
			try {
				canvas = holder.lockCanvas();
				if (canvas != null) {
					// 寮€濮嬬敾
					switch (gameStatu) {
					case RUNNING:
						// 姝e父杩愯
						drawRunningView(canvas);
						break;

					default:
						break;
					}

				}
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				if (canvas != null) {
					holder.unlockCanvasAndPost(canvas);

				}
			}

		}
	}

	private void drawRunningView(Canvas canvas) {
		// 璁剧疆鐢荤瑪灞炴€?
		paint.setStrokeWidth(5);
		canvas.drawColor(Color.WHITE);
		for (int i = 0; i < gameType; i++) {

			// 鐢诲簳閮ㄩ儴text鐗堟潈
			String text = "灏忔潕宸ヤ綔瀹ょ増鏉冩墍鏈夛紒";
			paint.setTextSize(60);
			canvas.drawText(text, w / 2 - (text.length() * 60 / 2), h - h / 20, paint);
			// 鐢籭鏍圭嚎
			int lineY = h / 10 + (i + 1) * gameSpan;
			// 妯℃嫙绮剧伒鍚戜笅閫熷害 寰呭畬鍠?

			roles[i].setSpeedY(roles[i].getSpeedY() + h*gameType / 3100f);
			roles[i].setY(roles[i].getY() + roles[i].getSpeedY());

			// 涓嶈兘鎺夊埌绾夸笅
			if (roles[i].getY() + roles[i].getHeight() >= lineY) {
				// 姝ゆ椂宸茬粡钀藉埌绾夸笂浜?
				roles[i].setY(lineY - roles[i].getHeight());
				roles[i].setSpeedY(0);
				// 鏀瑰彉鐘舵€?鍏堣灏忎汉璺宠捣鏉ュ啓鍦ㄧ偣鍑诲尯鍩熶腑鐨刼ntouch浜嬩欢涓?
				roles[i].setJump(false);
			}

			// 璁╅殰纰嶇墿閲嶅彸寰€宸︾Щ鍔紝鏀瑰彉left鍜宺ight
			rects[i].left = rects[i].left - (h / 300);
			rects[i].right = rects[i].right - (h / 300);
			// 闅滅鐗╃Щ鍑虹晫闈?
			if (rects[i].right <= 0) {
				// 閲嶆柊鍥炲埌鍙宠竟鐢熸垚鐭╁舰
				// 闅忔満闅滅鐗╁楂?
				int random_w = (int) (roles[i].getWidth() * (Math.random() * 5 + 1)/(3*gameType));
				int random_h = (int) (roles[i].getHeight() * (Math.random() * 5 + 1)/(3*gameType));
				int random_start = (int) (w * (new Random().nextFloat() / 5));

				rects[i].left = 3 * w / 2 + random_start;
				rects[i].right = rects[i].left + random_w;
				// bottom涓嶅彉
				// rects[i].bottom = lineY;
				rects[i].top = rects[i].bottom - random_h;
			}
			// 纰版挒妫€娴?寰呭畬鍠?
			Rect roleRect = roles[i].getRectFromRole();
			// 娣诲姞椤堕儴鏃堕棿
			long myTime = System.currentTimeMillis() - beginTime;
			String myTimeStr = myTime / 1000 + "." + myTime % 1000;
			String timeText = myTimeStr.substring(0, myTimeStr.length() - 1) + "''";

			if (rects[i].intersect(roleRect)) {
				canvas.drawColor(Color.WHITE,PorterDuff.Mode.CLEAR);
				canvas.drawColor(Color.WHITE);
				canvas.drawText(text, w / 2 - (text.length() * 60 / 2), h - h / 20, paint);
				// 宸茬粡鍙戠敓纰版挒
				// 鏀瑰彉娓告垙鐘舵€?
				// roleRect.
				paint.setTextSize(50);
				String topText = "浣犳寕浜嗭紝鏈鎴愮哗涓? + myTimeStr.substring(0, myTimeStr.length() - 1) + "绉?";

				canvas.drawText(topText, w / 2 - (topText.length() * 40 / 2), (float) (h * 0.5 / 5), paint);

				canvas.drawText(" 鏅?  閫? ", w / 2 - (topText.length() * 10 / 2), (float) (h / 5), paint);
				canvas.drawText(" 鍥?  闅? ", w / 2 - (topText.length() * 10 / 2), (float) (h *1.5/ 5), paint);

				Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), 
						R.drawable.weixing);
				canvas.drawBitmap(bitmap, w / 2 - bitmap.getWidth()/ 2, 
						(float) (h*2/ 5), paint);
				isStart = false;
				gameStatu = STANDOFF;
				return;

			} else {
				if (fristLocation <= 0) {
					fristLocation = w / 2 - (timeText.length() * 25 / 2);
				}

				if (myTime - preTime > 10) {
					canvas.drawText(timeText, 0, (float) (h * 0.5 / 5), paint);
					preTime = myTime;
				}
			}

			// 鐢荤嚎
			canvas.drawLine(0, lineY, w, lineY, paint);
			// 鐢诲皬浜?
			roles[i].drawSelf(canvas);
			// 鐢婚殰纰嶇墿
			canvas.drawRect(rects[i], paint);

		}
	}

	public boolean isStart() {
		return isStart;
	}

	public void setStart(boolean isStart) {
		this.isStart = isStart;
	}

	public int getGameStatu() {
		return gameStatu;
	}

	public void setGameStatu(int gameStatu) {
		this.gameStatu = gameStatu;
	}
}