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

/// <summary>
/// Class to deal with the other collisions for the core
/// </summary>
public class CoreColliderManager : ColliderManager
{
    public AudioClip corePickupSFX;
    private AudioSource _audioSource;

    private TimerHelper _scoreValueTimer = new TimerHelper(20.0f, false, TimerDirection.TimerDecrement);

    public override void DoAwakeTasks()
    {
        GameObject playerGO = GameObject.Find("Player");
        _audioSource = playerGO.GetComponent<AudioSource>();
        base.DoAwakeTasks();
    }

    // Bool checks if the core has been collected, this is because the ships are made of multiple collision boxes and would trigger more than once.
    private bool _coreCollected = false;

    public override void ProcessCollision(CollisionDirection direction, Collision collision, bool wasChild)
    {
        GameObject collisionGO;
        if (wasChild)
        {
            collisionGO = collision.transform.parent.gameObject;
        }
        else
        {
            collisionGO = collision.gameObject;
        }

        // Check the tag of the collided object, Direction doesnt matter i assume
        switch (collisionGO.tag)
        {
            case "Player":
                PickupCore();
                break;
        }
        
    }

    /// <summary>
    /// Process the core pick up and destroy the core object.
    /// </summary>
    public void PickupCore()
    {
        // If the core has been collected ignore it.
        if (!_coreCollected)
        {
            _coreCollected = true;
            if (_audioSource != null)
            {
                _audioSource.PlayOneShot(corePickupSFX);
            }            
            GameManager.Instance.AddCore();
            int pickUpScore = (int)(100.0f * (1.0f + (1.0f * _scoreValueTimer.Position)));                        
            GameManager.Instance.AddScore(pickUpScore - (pickUpScore % 10));

            if (transform.childCount > 0)
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    transform.GetChild(i).gameObject.SetActive(false);
                }
            }
            
            Destroy(gameObject, 2.0f);
        }
    }

    private void Update()
    {
        if (!GameEngine.isPaused)
        {
            _scoreValueTimer.HasTicked(Time.deltaTime);
        }
    }
}
