using UnityEngine;
using System.Collections;

public class DropFloorBehaviourScript : MonoBehaviour 
{

    PlayerMovement playerScript;
    public PlatformEffector2D platform;

    float fTimeToRestorePlayerCollision;
    float fTimeToRestoreMonsterCollision;

	// Use this for initialization
	void Start () 
    {
        //Debug.Log("OG Collider mask: " + platform.colliderMask);
        platform.colliderMask = 0;
        platform.colliderMask |= (int)Mathf.Pow(2,LayerMask.NameToLayer("PlayerCollide"));
        platform.colliderMask |= (int)Mathf.Pow(2, LayerMask.NameToLayer("MonsterCollide"));
        platform.colliderMask |= (int)Mathf.Pow(2, LayerMask.NameToLayer("Default"));
        //Debug.Log("Molested Collider mask: " + platform.colliderMask);

	}
	
    bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if( go != null )
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

	// Update is called once per frame
	void Update () 
    {
        if( fTimeToRestorePlayerCollision > 0.0f )
        {
            fTimeToRestorePlayerCollision -= Time.smoothDeltaTime;
            if (fTimeToRestorePlayerCollision <= 0.0f)
            {
                platform.colliderMask |= (int)Mathf.Pow(2, LayerMask.NameToLayer("PlayerCollide"));
                //Debug.Log("Turning on floor collision, probably. " + platform.colliderMask.ToString());
            }
        }

        if (playerScript == null && !GetPlayerScript())
            return;

        //check to see if we need to disable player collision.
        if (playerScript.PlayerIsFloorDropping())
        {
            //Debug.Log("Floor Collision was.... " + platform.colliderMask.ToString());
            fTimeToRestorePlayerCollision = 0.1f;
            platform.colliderMask &= ~(int)Mathf.Pow(2, LayerMask.NameToLayer("PlayerCollide"));
            //Debug.Log("Turning off floor collision, maybe. " + platform.colliderMask.ToString());
        }
	}
}
