using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class gameScript : MonoBehaviour {

	public Text actionText;
    public Text myScore;
    public float countdown = 3.0f;

	public GameObject castle1;
    public GameObject castle1b;
    public GameObject castle2;
    public GameObject castle2b;
    private float f,eq;
    private float[] pos = {-3.6f, -2.69f, -3.86f};
    bool isPressed,goingUp,goingDown = false;
    int correct=0,questionNo = 1;
    int click = 0,tmp=1;

    void Start()
    {

        actionText.text = "Ready! ...";
        //f = castle1.GetComponent<SpriteRenderer>().sprite.bounds.size.y;//castle1.transform.position.y;
        //pos[0] = castle1.transform.position.y + (f/3);//(castle1.transform.position.y/3);
    }

	//void showSprite(GameObject g)
	//{
	//	//g.transform.up =  
	//	g.transform.position = new Vector3 (0,g.transform.position.y+(f/3f), 0);
	//	//Debug.Log(g.transform.position.y+(f/3));
	//	//actionText.text = (g.transform.position.y+(f/3f)).ToString();
	//}

	/// <summary>
	/// 
	/// </summary>
	void Update() {
		//distination1.position = targetPosition;
		countdown -= Time.deltaTime;

        //if (countdown <= 3f && countdown >= 0f) 
        //{
        //actionText.text = castle2.transform.position.y.ToString() + " " + goingUp.ToString()+" "+goingDown.ToString() ;

        switch (questionNo)
        {
            case 1:
                actionText.text = "press 'A' ... " + countdown.ToString();
                if (Input.GetKey(KeyCode.A) == true && countdown <= 3f && countdown >= 0f)
                {
                    isPressed = true;
                    click = 0;
                }
                break;
            case 2:
                castle2.SetActive(true);
                castle2b.SetActive(true);
                actionText.text = "Press 'Space' ..." + countdown.ToString(); ;
                if (Input.GetKey(KeyCode.Space) == true && castle2.transform.position.y < .3
                && countdown <= 6f && countdown >= 0f && castle2.transform.position.y > -.3)
                {
                    isPressed = true;
                    click = 1;
                    tmp = 100;
                }
                break;
        }

        if (isPressed && countdown <= 6f && countdown >= 0f)
        {
            if (questionNo == 1) {
                if (castle1.transform.position.y <= pos[click])
                    castle1.transform.Translate(Vector3.up * Time.deltaTime);
            }
            if (questionNo == 2)
            {
                if (castle1b.transform.position.y <= pos[click])
                    castle1b.transform.Translate(Vector3.up * Time.deltaTime);
            }
            //Debug.Log(click.ToString());
            if (castle1.transform.position.y >= pos[click]) {
                questionNo = 2;
            }
            //isPressed = false;
            myScore.text = "Score: "+(questionNo* tmp).ToString();
        }

        //if (correct >= 0)
        if (castle2.transform.position.y <= -1.58)
        {
            //castle2.transform.Translate(Vector3.up * Time.deltaTime);
            goingUp = true;
            goingDown = false;
        }
        if (goingUp)
            castle2.transform.Translate(Vector3.up * Time.deltaTime*Random.Range(1f,15f));
        if (goingDown)
            castle2.transform.Translate(Vector3.down * Time.deltaTime * Random.Range(.8f, 12.5f));

        if (castle2.transform.position.y >= 1.57)
        {
            goingDown = true;
            goingUp = false;
        }
        //if (castle2.transform.position.y < .3 && castle2.transform.position.y > -.3)
        //    Debug.Log("yes!");
         //   goingUp = false;


        //actionText.text = castle1.transform.position.y.ToString();//eq.ToString();

        //myText.text = ((int)(gameTime)).ToString()+" "+ myWave1.transform.position.y.ToString();
        //transform.Translate(Vector3.forward * Time.deltaTime);
        if (countdown <= 0f )//&& !isPressed)
        {
            actionText.text = "You missed!";
            countdown = 6f;
        }
	}
}
