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

public class EnemyObjectShared : ObjectShared
{
    public EnemyTypeName enemyTypeName;

    // Tracking the spawner this gameObject was created by so we can reduce the counter for it later
    private ObjectSpawning _spawner;

    public AudioClip spawnInSFX;
    public GameObject spawnInVFX;

    private AudioSource _currentAudioSource;
    private bool _didSpawnInVFX = false;

    public int deadEngineChance;
    public float deadEngineInitialCheckTime;
    public float deadEngineRecheckTime;
    private float _deadEngineCheckTick;

    private bool _engineFailing;
    private bool _engineDead;

    public ObjectSpawning Spawner
    {
        get
        {
            return _spawner;
        }
        set
        {
            _spawner = value;
        }
    }

    private void Start()
    {
        _currentAudioSource = gameObject.GetComponent<AudioSource>();
    }

    public void DoFailure()
    {
        HoverMovement currentHoverMovement = gameObject.GetComponent<HoverMovement>();
        if (currentHoverMovement != null)
        {
            currentHoverMovement.freezeMovement = true;
            currentHoverMovement.blackHoleImmune = false;
            TrailRenderer[] currentTrails = gameObject.GetComponentsInChildren<TrailRenderer>();
            foreach (TrailRenderer trail in currentTrails)
            {
                trail.enabled = false;
            }
        }
        _engineFailing = true;
        _engineDead = true;
    }

    private void Update()
    {
        if (!_didSpawnInVFX)
        {
            if (spawnInVFX != null)
            {
                Instantiate(spawnInVFX, gameObject.transform);
            }
            if (spawnInSFX != null)
            {
                if (_currentAudioSource != null)
                {
                    _currentAudioSource.PlayOneShot(spawnInSFX);
                }
            }
            _didSpawnInVFX = true;
        }

        if (!GameEngine.isPaused)
        {
            if (_engineFailing)
            {
                if (!_engineDead)
                {
                    if (_deadEngineCheckTick >= deadEngineRecheckTime)
                    {
                        int rngRoll = Random.Range(0, 100);
                        if (rngRoll <= deadEngineChance)
                        {
                            DoFailure();
                        }
                        _deadEngineCheckTick = 0.0f;
                    }
                    else
                    {
                        _deadEngineCheckTick += Time.deltaTime;
                    }
                }
            }
            else
            {
                if (_deadEngineCheckTick >= deadEngineInitialCheckTime)
                {
                    _engineFailing = true;
                    _deadEngineCheckTick = 0.0f;
                }
                else
                {
                    _deadEngineCheckTick += Time.deltaTime;
                }
            }
        }
    }
}
