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

namespace ToadieTechnika.GGJ2018Game
{
    public class BluetoothLock : MonoBehaviour
    {
        [SerializeField]
        BluetoothObject m_BluetoothParent;

        [SerializeField]
        Collider2D m_Collider2D;
        ContactFilter2D m_ContactFilter2D;

        bool m_SoundOnce;

        void Start()
        {
            //Initiate contact filter
            m_ContactFilter2D = new ContactFilter2D();
            m_ContactFilter2D.SetLayerMask(1 << LayerMask.NameToLayer("Overlapping"));
            m_ContactFilter2D.useLayerMask = true;
            m_ContactFilter2D.useTriggers = true;
        }

        void Update()
        {
            m_BluetoothParent.IsOpened = (m_Collider2D.OverlapCollider(m_ContactFilter2D, m_BluetoothParent.GetTransmissionColliders) > 0);

            //When to play sound
            if (m_BluetoothParent.IsOpened && !m_SoundOnce)
            {
                GameController.instance.PlaySound(SFX.BluetoothPanel);
                m_SoundOnce = true;
            }
            else if (!m_BluetoothParent.IsOpened)
                m_SoundOnce = false;
        }
    }
}