using Assets.Source.Patterns.ObjectPool;
using UnityEngine;

namespace Assets.Source.Core.Gameplay.Topdown
{
    public class BulletTopdown : MonoBehaviour, IPoolObject, IDamager
    {
        public float Speed = 10;
        public Vector2 ExtraVelocity;

        public int Damage => 1;

        private float _defaultSpeed;

        void Awake()
        {
            _defaultSpeed = Speed;
        }

        void FixedUpdate()
        {
            var dir = transform.right;
            dir.z = 0;
            transform.position += dir * Time.fixedDeltaTime * Speed + (Vector3)ExtraVelocity * Time.fixedDeltaTime;
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            var d = collision.transform.GetComponentInParent<IDamageable>();
            if (d == null)
                return;
            if (d == GameManagerTopdown.Instance.Player)
                return;
            d.TakeDamage(this, 1);
            this.Release();
        }

        public void OnSpawnFromPool()
        {
        }

        public void OnReturnToPool()
        {
            Speed = _defaultSpeed;
            ExtraVelocity = Vector2.zero;
        }
    }
}
