using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MonoBehaviour {

    public float gravity;
    public float jumpHeight;
    public float waterVelocity;
    public float vy;
    public float vx = 0;
    public bool isJumping;
    public bool inWater;
    private SpriteRenderer spriteRenderer;
    private Animator animator;
    private Rigidbody2D rigidBody;
    public int score = 0;
    public GameObject splashPrefab;
    public Text scoreText;
    public Text gameOverText;
    public GameObject seagull;
    

	// Use this for initialization
	void Start () {
        vy = 0;
        isJumping = false;
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        rigidBody = GetComponent<Rigidbody2D>();
        scoreText.text = "Score: " + score;
	}
	
	// Update is called once per frame
	void Update () {
        var jump = Input.GetAxis("Jump");


        if (inWater)
        {
            vy = (jump > 0) ? waterVelocity : -waterVelocity;
        }

        else
            vy -= gravity;

        if (!inWater && !isJumping && jump > 0)
        {
            isJumping = true;
            vy = jumpHeight;
        }

        if (jump == 0)
        {
            isJumping = false;
        }

        if (transform.position.y < -8)
        {
            vy = 0;
            transform.position = new Vector3(transform.position.x, -8);
        }

        // Temporary, prevent it from leaving the screen
        if (transform.position.y > 12)
            isJumping = true;

        if (transform.position.x < -12)
        {
            GameOver();
        }

    }

    void LateUpdate()
    {
        transform.position += new Vector3(vx * Time.deltaTime, vy * Time.deltaTime, 0);

        if (vy > 0)
            transform.eulerAngles = new Vector3(0, 0, 5);
        else if (vy < 0)
            transform.eulerAngles = new Vector3(0, 0, -5);
        else
            transform.eulerAngles = new Vector3(0, 0, 0);

        if (seagull != null)
        {
            transform.position = seagull.transform.position;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        //Debug.Log("Collided with " + other.name);
        if(other.gameObject.CompareTag("Water"))
        {
            inWater = true;
            animator.SetBool("inWater", true);
            GameObject splash = Instantiate(splashPrefab);
            var splashScript = splash.GetComponent<Splash>();
            splash.transform.position = new Vector3(transform.position.x, 3f, 0);
            //splashScript.speed = 4.5f;

        }
        else if (other.gameObject.CompareTag("Sky"))
        {
            inWater = false;
            animator.SetBool("inWater", false);
        }
        else if (other.gameObject.CompareTag("Obstacle"))
        {
            animator.SetTrigger("isHurt");
        }
        else if (other.gameObject.CompareTag("Pickup"))
        {
            other.gameObject.SetActive(false);
            PickupObject();
        }
        else if (other.gameObject.CompareTag("Seagull"))
        {
            seagull = other.gameObject;
        }
    }

    public void PickupObject()
    {
        score += 1;
        scoreText.text = "Score: " + score;
        // TODO: Add sound
    }

    public void GameOver()
    {
        gameOverText.text = "Game over!";
    }
}
