package com.nodie;

import android.os.Bundle;

import com.nodie.runnable.MyThread;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity implements Callback, OnTouchListener {

	private SurfaceView mSurfaceView;
	private MyThread thread;
	private int w, h;

	private int gameType = 2;
	private int gameSpan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);
		starGameView();
	}

	private void starGameView() {
		mSurfaceView = new SurfaceView(this);
		mSurfaceView.getHolder().addCallback(this);
		mSurfaceView.setOnTouchListener(this);
		setContentView(mSurfaceView);

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// 鍒涘缓鐢诲竷鏄垵濮嬪寲
		// 寰楀埌鐢绘澘 鐒跺悗鐢绘澘鍜岀嚎绋嬬粦瀹?
		w = mSurfaceView.getWidth();// 鐢绘澘瀹?
		h = mSurfaceView.getHeight();// 鐢绘澘楂?
		// 寰楀埌娓告垙鍖哄煙
		// 鐣?/5鏄剧ず鏃堕棿
		gameSpan = h * 4 / (5 * gameType);
		thread = new MyThread(this, holder, w, h, gameType);
		// mSurfaceView.
		thread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		thread.setStart(false);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// 鐐瑰嚮鍖哄煙璁╃簿鐏佃烦璧锋潵
		switch (thread.getGameStatu()) {
		case MyThread.RUNNING:
			confirmRole(event);
			break;
		case MyThread.STANDOFF:
			resStart(event);
			break;

		default:
			break;
		}

		return true;
	}

	private void resStart(MotionEvent event) {
		int action = event.getAction();
		switch (action & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			// 绗竴鏍规墜鎸囨寜涓?
			float y = event.getY();
			// 鐐瑰嚮浜嗛噸鏉?
			if (y >= h * 0.5 / 5 && y <= h * 1 / 5){
				gameType=2;
				starGameView();
		    }else if(y >= h * 1  / 5 && y <= h * 1.5 / 5){
		    	gameType=3;
		    	starGameView();
		    }
			
			break;

		default:
			break;
		}

	}

	private void confirmRole(MotionEvent event) {
		int action = event.getAction();
		switch (action & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			// 绗竴鏍规墜鎸囨寜涓?
			float y = event.getY();
			if (thread.isStart())
				roleJump(y);
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			// 鍏朵粬鎵嬫寚闄嗙画鎸変笅
			float y2 = event.getY(event.getPointerCount() - 1);
			if (thread.isStart())
				roleJump(y2);
			break;
		default:
			break;
		}
	}

	/**
	 * 鑾峰彇瀵瑰簲鍖哄煙璁╁搴斿尯鍩熶汉鐗╄烦璧锋潵
	 * 
	 * @param y
	 */
	private void roleJump(float y) {
		// 鎵惧埌鍖哄煙
		try {
			for (int i = 0; i < gameType; i++) {
				// 鍒ゆ柇鍦ㄩ偅涓€涓父鎴忓尯鍩?
				int lineD = h / 10 + (i + 1) * gameSpan;
				int lineU = h / 10 + (i) * gameSpan;
				if (y >= lineU && y < lineD && !thread.roles[i].isJump()) {
					thread.roles[i].setSpeedY(-h / 48);
					// 璁剧疆璺宠捣鐘舵€?
					thread.roles[i].setJump(true);
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

}