using UnityEngine;
using System.Collections;

public class HurtTrigger : MonoBehaviour {

    public float DelayBetweenFiring = 0.5f;

    public float JumpForceOnTouch = 0.0f;

    public float DamageOnTouch = 1.0f;

    private float m_fFireDelay;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update ()
	{
        if( m_fFireDelay > 0.0f )
    	    m_fFireDelay -= Time.deltaTime;
	}

    void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.name == "TestMans")
        {
            HandleTouchPlayer(other);
            return;
        }

        EnemyController ecs = other.GetComponent<EnemyController>();

        if (ecs != null)
        {
            if (JumpForceOnTouch != 0.0f)
            {
                Rigidbody2D otherBody = other.GetComponent<Rigidbody2D>();
                if (otherBody != null)
                    otherBody.AddForce(new Vector2(0, JumpForceOnTouch / 2));


            } 
        }
    }

    private void HandleTouchPlayer( Collider2D other)
    {
        if (m_fFireDelay <= 0.0f)
        {
            //Debug.LogWarning("OH SHIT I TOUCHED A " + other);
            m_fFireDelay = DelayBetweenFiring;
        }

        PlayerController pcs = other.GetComponent<PlayerController>();

        if (pcs.CantBeDamaged())
            return;

        pcs.TakeDamage(DamageOnTouch);

        //do something!
        if (JumpForceOnTouch != 0.0f)
        {
            Rigidbody2D otherBody = other.GetComponent<Rigidbody2D>();
            if (otherBody != null)
                otherBody.AddForce(new Vector2(0, JumpForceOnTouch));
        }

        GroundBlockMovementScript gbms = GetComponentInParent<GroundBlockMovementScript>();
        if (gbms != null)
        {
            gbms.BombedOut();
        }
    }
}
