using UnityEngine;
using System.Collections;

public class AcceptState : MonoBehaviour {

	public Player play;
	public bool col;
	public float attackTimer;
	public float greenTimer;

    public int collisionCount = 0;

	private Animator anim;
	public bool damage;
	public bool green;
	
    public bool angry;


    void Start(){
		attackTimer = 0f;
		anim = GetComponent<Animator>();
        damage = true;
	}

	void OnTriggerExit2D(Collider2D coll){
		if (coll.gameObject.tag == "Pattern") {
			Destroy (coll.gameObject);
			damage = true;
            angry = true;
			anim.SetBool("Damage", true);
			col = false;
            collisionCount--;

		}
	}
		
	void OnTriggerEnter2D(Collider2D coll){
		col = true;
        collisionCount++;
	}

	void OnTriggerStay2D(Collider2D other) {
        if (other.gameObject.name == "Down(Clone)")
        {
            if ((play.Key5 == 1))
            {
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }
        }
        else if (other.gameObject.name == "Left(Clone)")
        {
            if ((play.Key4 == 1))
            {	 
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }    
        }
        else if (other.gameObject.name == "Right(Clone)")
        {
            if ((play.Key6 == 1))
            {
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }   
        }
        else if (other.gameObject.name == "s(Clone)")
        {
            if ((play.key2 == 1))
            {  
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }
        }else if (other.gameObject.name == "a(Clone)")
        {
            if ((play.key1 == 1))
            {  
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }
        }else if (other.gameObject.name == "d(Clone)")
        {
            if ((play.key3 == 1))
            {  
                Destroy(other.gameObject);
                green = true;
                col = false;
                anim.SetBool("Green", true);
            }
        }
	}

	void Update(){
		if (damage ){
			attackTimer += Time.deltaTime;
		}

		if (green)
		{
			greenTimer += Time.deltaTime;
		}



		if (attackTimer > 0.2f)
		{
			damage = false;
			anim.SetBool("Damage", false);
			attackTimer = 0;
		}

		if (greenTimer > 0.2f)
		{
			green = false;
			anim.SetBool("Green", false);
			greenTimer = 0;
		}

        if (collisionCount == 0)
        {   
            if (play.Key4 == 1 || play.Key5 == 1 || play.Key6 == 1)
            {
                damage = true;
                anim.SetBool("Damage", true);
                Debug.Log ("Damage");
                col = false;
            }
        }

	}
}
