using UnityEngine;
using System.Collections;



public class AssignCtrl : MonoBehaviour {

	int playerNumber =1;
	bool isChecking = false;
	bool assigned = false;
	int currentJoystick;
	public int[] player = new int[4];
	bool fu;
	void Start () {
	}
	

	void Update(){
		if (playerNumber < 5) {
		/*if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.2 || Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2){
			isChecking=true;
		}else{
			isChecking=false;
		}*/
		print(isChecking);
		print(playerNumber);
		/*for( int i = 0; i<3;i++){
			print(player[i]);
		}*/
		for (int i = 0; i < 11; i++) {			
			if ((Mathf.Abs(Input.GetAxis("Vertical"+i.ToString())) > 0.2 || Mathf.Abs(Input.GetAxis("Horizontal"+i.ToString())) > 0.2) && i != player[0] && i != player[1] && i != player[2] && i != player[3]){
				Debug.Log (Input.GetJoystickNames()[i-1]+" is moved");
				assigned = true;
				player[playerNumber-1]=i;
			}else if(assigned){

				assigned = false;
				playerNumber++;
			}
		}
		}
	}

	public int getPlayerControl (int trg) {
		return player[trg + 1];
	}

	
	void OnGUI () {
		if(Application.loadedLevel==0){
			if (playerNumber < 5) {
			GUI.Box (new Rect (30,60,1000,50), "Player" + playerNumber +  "use left Joystick" );
			}
			GUI.Box (new Rect (0,60,300,50), "Player1 Controller: " + player[0].ToString ());
			GUI.Box (new Rect (0,120,300,50), "Player2 Controller: " + player[1].ToString ());
			GUI.Box (new Rect (0,180,300,50), "Player3 Controller: " + player[2].ToString ());
			GUI.Box (new Rect (0,240,300,50), "Player4 Controller: " + player[3].ToString ());
		}
	}
}
