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

public class DropAttachedPowerUpEffect : AttachablePowerUpEffect
{
    private GameObject _player;
    private PlayerObjectShared _playerObjectShared;
    private PlayerColliderManager _playerColliderManager;
    private Rigidbody _playerRigidBody;

    public float dropPowerForce;

    // Start is called before the first frame update
    void Start()
    {
        _player = GameEngine.mainPlayer;
        _playerObjectShared = _player.GetComponent<PlayerObjectShared>();
        _playerRigidBody = _player.GetComponent<Rigidbody>();
        _playerColliderManager = _player.GetComponent<PlayerColliderManager>();
        _abilityCooldown = false;
        _abilityCooldownTimer = new TimerHelper(abilityCooldown, false);
    }

    public override bool OnUseEffect()
    {
        if (!_abilityCooldown)
        {   
            _playerRigidBody.AddForce(-_player.transform.up * dropPowerForce, ForceMode.VelocityChange);
            _playerColliderManager.SetDeadly();

            Transform particleEffect = powerUpModel.transform.Find("PowerUsed");
            if (particleEffect != null)
            {
                ParticleSystem particleSys = particleEffect.gameObject.GetComponent<ParticleSystem>();
                particleSys.Play();
            }

            if (onUseSFX != null)
            {
                _playerObjectShared.PlaySFX(onUseSFX);
            }

            _abilityCooldown = true;
            _abilityCooldownTimer.RestartTimer();
            return true;
        }
        else
        {
            return false;
        }
    }

    public override bool OnAltUseEffect()
    {
        if (!_abilityCooldown)
        {
            _playerRigidBody.AddForce(_player.transform.up * dropPowerForce, ForceMode.VelocityChange);
            _playerColliderManager.SetDeadly();

            Transform particleEffect = powerUpModel.transform.Find("AltUsed");
            if (particleEffect != null)
            {
                ParticleSystem particleSys = particleEffect.gameObject.GetComponent<ParticleSystem>();
                particleSys.Play();
            }

            if (onAltSFX != null)
            {
                _playerObjectShared.PlaySFX(onAltSFX);
            }

            _abilityCooldown = true;
            _abilityCooldownTimer.RestartTimer();
            return true;
        }
        else
        {
            return false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (_abilityCooldown)
        {
            if (_abilityCooldownTimer.HasTicked(Time.deltaTime))
            {
                _abilityCooldown = false;
            }
        }
    }
}
