using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class Player : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator anim;
    public State state;
    private bool grounded;

    //FLY
    public float flyImpulse;
    public float flyImpulseDelay;
    private float flyImpulseCoolDown;

    //MOVE
    public float moveSpeed;

    //STATE
    public bool alive;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Use this for initialization
    void Start ()
    {
        flyImpulseCoolDown = flyImpulseDelay;
        alive = true;
        state = State.IDDLE;
	}

    // Update is called once per frame
    void Update()
    {
        if (state != State.DEAD)
        {
            flyImpulseCoolDown += Time.deltaTime;
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (flyImpulseCoolDown >= flyImpulseDelay)
                {
                    rb.velocity += (Vector2.up * flyImpulse);
                    flyImpulseCoolDown = 0;
                    state = State.FLIYING;
                }
            }
            rb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y);

            if (!grounded)
            {
                if (anim.GetInteger("state") != 1)
                {
                    anim.Play("birdFly");
                    anim.SetInteger("state", 1);
                    state = State.WALKING;
                }
            }
            else if (Mathf.Abs(rb.velocity.x) > 0)
            {
                if (anim.GetInteger("state") != 2)
                {
                    anim.Play("birdJump");
                    anim.SetInteger("state", 2);
                    state = State.WALKING;
                }
            }
            else
            {
                if (anim.GetInteger("state") != 0)
                {
                    anim.Play("birdEat");
                    anim.SetInteger("state", 0);
                }
            }

            if (rb.velocity.x > 0)
                transform.localScale.Set(1, 1, 1);
            else if (rb.velocity.x < 0)
                transform.localScale.Set(-1, 1, 1);

            if (rb.velocity.y == 0 && state == State.DEAD)
                anim.Play("Dead");
        }
	}

    public void Die(int index)
    {
        state = State.DEAD;
        if(index == 0)
        {
            print("dead0");
            GetComponent<SpriteRenderer>().enabled = false;
        }
        else
        {
            print("dead2");
            anim.Play("birdDie2");
            anim.SetInteger("state", 3);
        }
        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
            grounded = true;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
            grounded = false;
    }
}



public enum State
{
    FLIYING,
    IDDLE,
    WALKING,
    DEAD
}
