using UnityEngine;
using System.Collections;

public class CarryTarget : MonoBehaviour {

    // Exposed variables & types
    //--------------------------------

    [SerializeField] string m_typeId;
    [SerializeField] GameObject m_filledVisual;
    [SerializeField] GameObject m_emptyVisual;

    // Private variables 
    //--------------------------------

    bool m_filled = false;
    
    // Public methods 
    //--------------------------------

    public bool IsFilled()
    {
        if (transform.parent && transform.parent.name == "Tile Palette")
            return true;
        
        return m_filled;
    }
    
    // Private methods 
    //--------------------------------
    
    void Awake () 
    {
        SetFilled(false);
    }

    void OnTriggerStay2D(Collider2D collider)
    {
        if (m_filled)
            return;
        
        BodyBlock bodyBlock = collider.GetComponent<BodyBlock>();
        if (bodyBlock)
        {
            if (bodyBlock.IsCarrying())
            {
                Carryable carryable = bodyBlock.GetCarryable();
                if (carryable.GetTypeId() == m_typeId)   
                {
					SystemAudio.Play(carryable.GetDropSound());
                    Destroy(carryable.gameObject);
                    SetFilled(true);
                }
            }
        }
    }

    void SetFilled(bool filled)
    {
        m_filled = filled;
        m_emptyVisual.SetActive(!filled);
        m_filledVisual.SetActive(filled);        
    }
}
