using UnityEngine;

// 플레이어가 먹을 수 있는 마일리지 토큰
public class MileagePickup : MonoBehaviour
{
    public int mileageValue = 10; // 획득 마일리지

    private bool collected = false; // 이미 획득했는지 여부
    private SpriteRenderer spriteRenderer;
    private Collider2D pickupCollider;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        pickupCollider = GetComponent<Collider2D>();
    }

    public void Setup(Sprite sprite, int value)
    {
        mileageValue = value;

        if(spriteRenderer != null)
        {
            spriteRenderer.sprite = sprite;
        }
    }

    public void ResetPickup()
    {
        collected = false;
        gameObject.SetActive(true);

        if(pickupCollider != null)
        {
            pickupCollider.enabled = true;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(collected || !other.CompareTag("Player"))
        {
            return;
        }

        collected = true;

        if(GameManager.instance != null)
        {
            GameManager.instance.AddMileage(mileageValue);
        }

        gameObject.SetActive(false);
    }
}
