using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class AssController : MonoBehaviour {

	public KeyCode ResponseKey;
	public SpriteRenderer[] feet;
	private bool coliding = false;
	bool isNormal=false;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (!GameManager.instance.GameOver) {



			float v = Input.GetAxis ("Vertical");

			if (v > 0) {
				//this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y + GameManager.instance.StepAss, this.transform.position.z);
		
				AssUp();

			}

			if (v < 0) {
				assDown ();
				//this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y - GameManager.instance.StepAss, this.transform.position.z);

			}
			if (v == 0) {
				assNormal ();
			}
			//Debug.Log (v);
			/*
			//GET KEYS
			if (Input.GetKey (ResponseKey)) {
				this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y + GameManager.instance.StepAss, this.transform.position.z);

			} else {
				this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y - GameManager.instance.StepAss, this.transform.position.z);
			}


			//ASS LIMITER
			if (this.transform.position.y > GameManager.instance.MaxUpAss) {
				this.transform.position = new Vector3 (this.transform.position.x, GameManager.instance.MaxUpAss, this.transform.position.z);
			}

			if (this.transform.position.y < GameManager.instance.MaxDownAss) {
				this.transform.position = new Vector3 (this.transform.position.x, GameManager.instance.MaxDownAss, this.transform.position.z);
			}
			*/

			//chk for colision
			if (transform.position.y < GameManager.instance.PoserAssPosition + GameManager.instance.chkPositionErrorLimit && transform.position.y > GameManager.instance.PoserAssPosition - GameManager.instance.chkPositionErrorLimit) {
				coliding = true;
			} else {
				coliding = false;
			}



			if (coliding) {
				colideStart ();
			} else {
				colideEnd ();
			}

		} else {
		
		//gameover
			coliding=false;
			colideEnd ();
		}
			
	}



	public void changePose()
	{
		if (isNormal) {
			if (0==Random.Range (0, 2)) {
				assDown ();
			} else {
				AssUp ();
			}

			isNormal = false;
		} else {
			assNormal ();
			isNormal = true;
		}

		//		Vector3 newPos = new Vector3 (transform.position.x, Random.Range (GameManager.instance.MaxDownAss, GameManager.instance.MaxUpAss), transform.position.y);
		//		transform.DOMove (newPos, .112f);
	}

	public void assDown()
	{
		moveAss( GameManager.instance.MaxDownAss+0.25f);
	}

	public void AssUp()
	{
		moveAss( GameManager.instance.MaxUpAss);

	}

	public void assNormal()
	{
		moveAss( 2.73f);

	}


	private void moveAss(float assPos)
	{
		//DOTween.Kill ("playermove");
		transform.DOMove ( new Vector3 (transform.position.x, assPos, transform.position.y),GameManager.instance.CurrentGameSpeed/2f).SetTarget("playermove");

	}












	void colideStart() {
		GameManager.instance.Player2AddScore (gameObject);
		/*
		int n;
		for (n = 0; n < 11; n++) {

			feet[n].color = new Color(0f, 1f, 0f, 1f);
		}
		*/
	}
		

	void colideEnd() 
	{
		/*
		int n;
		for (n = 0; n < 11; n++) {
		
			feet[n].color = new Color (1f, 1f, 1f, 1f);
		}
*/
		}







}
