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

public class Switch : MonoBehaviour, IInteractable
{
    [SerializeField] private byte m_SwitchId = 0;

    [SerializeField] private GameObject m_LinkedObject;

    [SerializeField] private SoundEffect m_InteractionSound;

    [SerializeField] private Sprite[] m_Sprites;

    private int m_SpriteIndex;

    private SpriteRenderer m_Renderer;

    private ITriggerable m_LinkedTriggerable;

    private void Awake()
    {
        m_Renderer = GetComponentInChildren<SpriteRenderer>();

        m_LinkedTriggerable = m_LinkedObject.GetComponentInChildren<ITriggerable>();

        if (m_LinkedTriggerable == null)
        {
            Debug.LogError(name.ToUpper() + ": provided object " + m_LinkedObject.name + " has not Triggerable component. Disabling.");

            gameObject.SetActive(false);

        }
    }

    public void Interact(PlayerController actor)
    {
        if (m_Sprites.Length > 1)
        {
            m_SpriteIndex = m_SpriteIndex == 0 ? 1 : 0;
            m_Renderer.sprite = m_Sprites[m_SpriteIndex];
        }

        m_InteractionSound?.Play(transform.position);

        m_LinkedTriggerable.Trigger(m_SwitchId);
    }
}
