using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Assertions;
using UnityEngine.UI;

public class Wifi2 : MonoBehaviour
{

    [SerializeField]
    Transform target;

    public Transform awal;

    public bool _playerinrange;

    public float timepocongsadar, time;
    public Transform[] patroli;
    private NavMeshAgent nav;
    public bool pause;
    Animator anim;
    public Text pesan;
    float speed;
    public bool backtoasal;
	private AudioSource audio;

	private bool playingSound;
    void Awake()
    {
        Assert.IsNotNull(target);
    }

    // Use this for initialization
    void Start()
    {
		audio = GetComponent<AudioSource> ();
        nav = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();
        timepocongsadar = Random.Range(20, 40);

    }

    // Update is called once per frame
    void Update()
    {
        if (!pause)
        {
            time += (Time.deltaTime * 1.5f);
            speed = Random.Range(2, 4);
            nav.speed = speed;
            if (time > timepocongsadar)
            {
                if (!backtoasal)
                {
                    if (Vector3.Distance(transform.position, target.transform.position) < 7 && Vector3.Distance(transform.position, awal.transform.position) < 7)
                    {
						StartCoroutine(playSound()); 
                        if (time > timepocongsadar)
                            _playerinrange = true;
                        pesan.text = "satan follow you";
                    }
                    else if ((Vector3.Distance(transform.position, target.transform.position) > 7 && Vector3.Distance(transform.position, awal.transform.position) > 7) ||
					         (Vector3.Distance(transform.position, target.transform.position) > 7 && Vector3.Distance(transform.position, awal.transform.position) < 7))
                    {
                        nav.SetDestination(awal.position);
                        pesan.text = "you safe bruh";
                        _playerinrange = false;
                    }

                    if (_playerinrange)
                    {
                        nav.SetDestination(target.position);

                    }
                    else
                    {
                        StartCoroutine(backtime());
                    }
                }
                else
                {
                    nav.SetDestination(awal.position);
                }
            }
        }
    }

    IEnumerator backtime()
    {
        if (Vector3.Distance(transform.position, awal.transform.position) < 2)
        {
            timepocongsadar = Random.Range(20, 40);
            yield return null;
            time = 0;
        }
    }

	IEnumerator playSound(){
		if (playingSound) {
			yield return null;
		} else {
			audio.PlayOneShot(audio.clip);
			playingSound = true;
			yield return new WaitForSeconds(5f);
			playingSound = false;
		}
	}


}
