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

public class Bonus : MonoBehaviour
{
    #region Fields
    [SerializeField] private bool _bonus = true;
    [SerializeField] private UnityEvent _onBonusCollected = null;
    private bool _isCollected = false;
    private float _scaling = 1.0f;
    #endregion Fields

    #region Events
    private event Action<Bonus> _onBonusCollectedEvent = null;
    public event Action<Bonus> OnBonusCollected
    {
        add
        {
            _onBonusCollectedEvent -= value;
            _onBonusCollectedEvent += value;
        }
        remove
        {
            _onBonusCollectedEvent -= value;
        }
    }
    #endregion Events

    #region Methods
    public void Init(float scaling)
    {
        _scaling = _bonus? scaling : 1.0f / _scaling;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (_isCollected)
            return;

        if (collision.TryGetComponent(out RocketScaler scaler))
        {
            scaler.Rescale(_scaling);
            _isCollected = true;
            _onBonusCollected?.Invoke();
            _onBonusCollectedEvent?.Invoke(this);
            Destroy(gameObject, 1.0f);
        }
    }
    #endregion Methods
}
