using UnityEngine;

public class LevelSwitch : MonoBehaviour
{
    public LevelSwitchData switchData;
    public SpriteRenderer iconRenderer;
    private Interactable interactable;

    void Awake()
    {
        interactable = GetComponent<Interactable>();
        if (interactable != null)
        {
            interactable.Interacted += OnInteracted;
        }
        ApplySwitchData();
    }

    void OnDestroy()
    {
        if (interactable != null)
        {
            interactable.Interacted -= OnInteracted;
        }
    }

#if UNITY_EDITOR
    void OnValidate()
    {
        ApplySwitchData();
    }
#endif

    public void ApplySwitchData()
    {
        if (switchData != null)
        {
            if (iconRenderer != null)
                iconRenderer.sprite = switchData.mapSprite;
            gameObject.name = switchData.targetLevelSceneName;
            // Optionally update other fields, e.g. description
        }
    }

    private void OnInteracted()
    {
        Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
        // TODO: Add scene loading logic here, e.g. UnityEngine.SceneManagement.SceneManager.LoadScene(switchData.targetLevelSceneName);
    }
}
