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

public class UFOScript : MonoBehaviour
{
    [SerializeField]
    float p_mySpeed;
    [SerializeField]
    float p_myDistance;
    [SerializeField]
    float p_myWalkTime;
    [SerializeField]
    float p_myWaitTime;
    [SerializeField]
    float p_myLifeTime;
    [SerializeField]
    AudioSource p_myAudioSource;

    GameObject p_myEarth;

    bool canDestroy;

    void Start()
    {
        p_myAudioSource = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(5).GetComponent<AudioSource>();
        if (!p_myAudioSource.isPlaying)
        {
            p_myAudioSource.Play();
        }
        p_myEarth = GameObject.FindGameObjectWithTag("Earth");
        StartCoroutine(ApproachEarth());
    }

    void Update()
    {
        if (GetComponent<Renderer>().isVisible)
        {
            canDestroy = true;
        }
        else if (canDestroy)
        {
            Destroy(this.gameObject);
        }
        if (Vector3.Distance(p_myEarth.transform.position, transform.position) > 50)
        {
            Destroy(this.gameObject);
        }
    }

    IEnumerator ApproachEarth()
    {
        float distance = Vector3.Distance(transform.position, p_myEarth.transform.position);
        while (distance > p_myDistance)
        {
            distance = Vector3.Distance(transform.position, p_myEarth.transform.position);
            transform.LookAt(p_myEarth.transform.position);
            transform.position += transform.forward * p_mySpeed * Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
        StartCoroutine(IAUFO());
    }

    IEnumerator IAUFO()
    {
        float lifeTime = RandomManager.getFloatRandom(p_myLifeTime);
        while (lifeTime > 0)
        {
            float walkTime = RandomManager.getFloatRandom(p_myWalkTime);
            float angle = RandomManager.getIntRandom(0, 360);
            while (walkTime > 0)
            {
                transform.LookAt(p_myEarth.transform.position);
                transform.position = transform.forward * -p_myDistance;
                transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, angle);
                transform.position += transform.right * p_mySpeed * Time.deltaTime;
                yield return new WaitForEndOfFrame();
                lifeTime -= Time.deltaTime;
                walkTime -= Time.deltaTime;
            }
            float waitTime = RandomManager.getFloatRandom(p_myWaitTime);
            while (waitTime > 0)
            {
                RaycastHit hit;
                if (Physics.Raycast(transform.position, transform.forward, out hit))
                {
                    if (hit.collider.tag == "Human")
                    {
                        hit.collider.GetComponent<HumanScript>().setUfo(this.gameObject);
                    }
                }
                yield return new WaitForEndOfFrame();
                lifeTime -= Time.deltaTime;
                waitTime -= Time.deltaTime;
            }
        }
        StartCoroutine(LeaveEarth());
    }

    IEnumerator LeaveEarth()
    {
        transform.LookAt(p_myEarth.transform.position);
        Vector3 newPos = transform.position + transform.forward * -1;
        transform.LookAt(newPos);
        while (true)
        {
            transform.position += transform.forward * p_mySpeed * Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
    }
}