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

public class MagnetAttachedPowerUpEffect : AttachablePowerUpEffect
{
    private GameObject _player;
    private PlayerObjectShared _playerObjectShared;

    public float magnetPowerForce;
    public float magnetPowerRange;

    // Start is called before the first frame update
    void Start()
    {
        _player = GameEngine.mainPlayer;
        _playerObjectShared = _player.GetComponent<PlayerObjectShared>();
        _abilityCooldown = false;
        _abilityCooldownTimer = new TimerHelper(abilityCooldown, false);
    }
    
    public override bool OnUseEffect()
    {
        if (!_abilityCooldown)
        {
            foreach (BlackHoleForce currentBHF in BlackHoleManager.blackHoleForceObjects)
            {   
                GameObject currentObject = currentBHF.gameObject;
                if (currentObject.tag != "Player")
                {
                    Vector3 directionToObject = gameObject.transform.position - currentObject.transform.position;
                    float distanceToObject = directionToObject.magnitude;
                    if (distanceToObject <= magnetPowerRange)
                    {
                        Rigidbody currentRigidBody = currentObject.GetComponent<Rigidbody>();
                        if (currentRigidBody != null)
                        {
                            currentRigidBody.AddForce(directionToObject.normalized * magnetPowerForce, ForceMode.VelocityChange);
                        }
                        if (currentObject.tag == "Enemy")
                        {
                            EnemyColliderManager enemyColliderManager = currentObject.GetComponent<EnemyColliderManager>();
                            enemyColliderManager.MagnetizeEnemy();
                        }
                        else if (currentObject.tag == "Mine")
                        {
                            MineColliderManager mineCM = currentObject.GetComponent<MineColliderManager>();
                            if (mineCM != null)
                            {
                                mineCM.magnetizeMine();
                            }
                        }
                    }
                }
            }

            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)
        {
            foreach (BlackHoleForce currentBHF in BlackHoleManager.blackHoleForceObjects)
            {
                GameObject currentObject = currentBHF.gameObject;
                if (currentObject.tag != "Player")
                {
                    Vector3 directionToObject = gameObject.transform.position - currentObject.transform.position;
                    float distanceToObject = directionToObject.magnitude;
                    if (distanceToObject <= magnetPowerRange)
                    {
                        Rigidbody currentRigidBody = currentObject.GetComponent<Rigidbody>();
                        if (currentRigidBody != null)
                        {
                            currentRigidBody.AddForce(-directionToObject.normalized * magnetPowerForce, ForceMode.VelocityChange);
                        }
                        if (currentObject.tag == "Enemy")
                        {
                            EnemyColliderManager enemyColliderManager = currentObject.GetComponent<EnemyColliderManager>();
                            enemyColliderManager.MagnetizeEnemy();
                        }
                        else if (currentObject.tag == "Mine")
                        {
                            MineColliderManager mineCM = currentObject.GetComponent<MineColliderManager>();
                            if (mineCM != null)
                            {
                                mineCM.magnetizeMine();
                            }
                        }
                    }
                }
            }

            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;
            }
        }
    }
}
