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

public class SphericalEnemy : MonoBehaviour
{
    
    public float Health = 1.0f;
    public float moveSpeed = 4.0f;
    private float minDist = 1f;
    public AudioClip[] audioClips;
    public AudioSource audioSource;
    public AudioSource trumpAudioSource;
    public AudioClip trumpDies;
    public AudioSource explosionSource;
    public AudioClip explosion;
    public AudioListener audioListener;
    //public ParticleSystem deathParticle;

    public bool does_move = true;

    Vector3 moveAmount;
	Vector3 smoothMoveVelocity;
	float verticalLookRotation;
	Transform cameraTransform;


    public Vector3 dir;
    public float starting_height;

    private GameObject player;
    Player m_Player;
    Rigidbody rb;

    void Start()
    {
        player = GameObject.FindGameObjectsWithTag("Player")[0];
        m_Player = player.GetComponent<Player>();
        rb = GetComponent<Rigidbody> ();

        starting_height = transform.position.magnitude;
        audioListener = GetComponent<AudioListener>();
        audioSource = GetComponent<AudioSource>();
        explosionSource = GetComponent<AudioSource>();
        trumpAudioSource = GetComponent<AudioSource>();

    }

    void Update()
    {
        if (!does_move) return;
        

        if (Vector3.Distance(transform.position, player.transform.position) > minDist)
        {
            // Vector3 moveDir = new Vector3(1.0f,0, 1.0f).normalized;
            // Vector3 targetMoveAmount = moveDir * moveSpeed;
            // moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);

            // Vector3 dir = Vector3(1.0f,0.0f,0.0f);//player.transform.position-transform.position;//Vector3.Cross(transform.forward,Vector3.Cross(transform.forward,transform.position));
            // Vector3 localMove = dir * Time.deltaTime * moveSpeed;
            // //transform.position += dir.normalized * moveSpeed * Time.deltaTime;
            // rigidbody.MovePosition(rigidbody.position + localMove);
            Vector3 pos = transform.position.normalized;
            Vector3 target = player.transform.position.normalized;

            Vector3 axis = Vector3.Cross(target,pos);
            //Vector3 axis = Vector3.Cross(pos,target);

            Debug.DrawRay(transform.position,axis,Color.red);

            float angle = Mathf.Acos(Vector3.Dot(pos,target));

            Vector3 dir = Vector3.Cross(pos,axis).normalized;

            // starting_height = Mathf.Lerp(transform.position.magnitude,starting_height,0.9f);
            // dir = Vector3.Lerp((player.transform.position-transform.position).normalized,dir,0.9f).normalized;
            //Vector3 pos = transform.position + dir * moveSpeed;

            //pos = Vector3.Normalize(pos)*starting_height;

            //dir = Vector3.Normalize(pos-transform.position);


            Debug.DrawRay(transform.position,dir);

            transform.LookAt(transform.position+dir,transform.position.normalized);
            

            transform.position += dir * Time.deltaTime * moveSpeed;

            

        }
        else
        {
            m_Player.MakeDeath();
        }
    }

    public void OnProjectileHit(SphericalProjectileScript projectile)
    {
        Health -= projectile.damage;

        if (Health <= 0.0f) {
            OnDeath();
        } else
        {
            PlayRandom();
        }
    }

    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "bullet") 
        {
            SphericalProjectileScript projectile = collision.gameObject.GetComponent<SphericalProjectileScript>();
            OnProjectileHit(projectile);
            
            Destroy(collision.gameObject); 
        }
    }

    public void OnDeath()
    {
        if (does_move == false)
        {
            /*explosionSource.PlayOneShot(explosion);
            gameObject.SetActive(false);*/
            Destroy(gameObject); //waits till audio is finished playing before destroying.
            //StartCoroutine(WaitAndDestroyFactory());
        } else
        {
            /*trumpAudioSource.PlayOneShot(trumpDies);
            gameObject.SetActive(false);*/
            Destroy(gameObject); //waits till audio is finished playing before destroying.
            //StartCoroutine(WaitAndDestroyTrump());
        }
    }

    void PlayRandom()
    {
        audioSource.clip = audioClips[Random.Range(0, audioClips.Length)];
        audioSource.Play();
    }

    IEnumerator WaitAndDestroyFactory()
    {
        if (!explosionSource.isPlaying)
        {
            explosionSource.PlayOneShot(explosion);
        }
        yield return new WaitForSeconds(2.0f); //float time in seconds
        Destroy(gameObject);
    }

    IEnumerator WaitAndDestroyTrump()
    {
        if (!trumpAudioSource.isPlaying)
        {
            trumpAudioSource.PlayOneShot(trumpDies);
        }
        yield return new WaitForSeconds(1.0f); //float time in seconds
        Destroy(gameObject);
    }

}
