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

public class LandingPad : MonoBehaviour
{
    public float radius = 20;
    public float damage = 10000;
    [SerializeField] private ParticleSystem ThrusterEffect;
    [SerializeField] private float thrusterDelay = 2.5f;

	private void Start()
	{
        ThrusterEffect.Stop();
    }

	private void OnDrawGizmos()
	{
        Gizmos.color = Color.magenta;
        Gizmos.DrawWireSphere(transform.position, radius);
	}

    private IEnumerator ThrusterDestruction()
	{
        yield return new WaitForSeconds(thrusterDelay);

        Collider[] hits = Physics.OverlapSphere(transform.position, radius, GameManager.AttackableLayers);

        if (hits.Length > 0)
        {
            foreach (Collider hit in hits)
		    {
                if (hit.GetComponent<Ship>() != null)
			    {
                    continue;
			    }

                Attackable attackable = hit.GetComponent<Attackable>();
                if (attackable != null)
			    {
                    attackable.TakeDamage(damage);
			    }
		    }
        }
    }

	public void Landing()
    {
        ThrusterEffect.Play();
        GameManager.Retreat();
        StartCoroutine(ThrusterDestruction());
    }

    public void StopLanding()
    {
        GameManager.StopRetreat();
        ThrusterEffect.Stop();
    }

    public void Launching()
    {
        ThrusterEffect.Play();
        GameManager.Retreat();
        StartCoroutine(ThrusterDestruction());
    }

    public void StopLaunching()
    {
        GameManager.StopRetreat();
        ThrusterEffect.Stop();
    }
}
