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

public class Enemy : MonoBehaviour
{
    [SerializeField] private Transform[] target;
    [SerializeField] private float moveSpeed = 1f;

    Rigidbody myRigidBody;
    AudioSource myAudioSource;


    private bool isMoving = true;
    private bool movingForward = true;
    private int waypointDestination = 0;

    [SerializeField] private float minDelayTime = 0.25f;
    [SerializeField] private float maxDelayTime = 2f;

    [SerializeField] private int scoreValue = 50;

    private bool isDefeated = false;

    private Animator myAnimator;



    private void Start()
    {

        if(target.Length == 0)
        {
            isMoving = false;
        }
        
        myRigidBody = GetComponent<Rigidbody>();
        myAnimator = GetComponent<Animator>();
        myAudioSource = GetComponent<AudioSource>();
    }

    private void Update()
    {
        UpdateAnimator();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
       
        if (isMoving)
        {
            myRigidBody.MovePosition(Vector3.MoveTowards(transform.position, target[waypointDestination].position, Time.deltaTime * moveSpeed));
            transform.LookAt(target[waypointDestination].position);
            if(Vector3.Distance(transform.position, target[waypointDestination].position) < 0.1f)
            {
                isMoving = false;
                if(movingForward)
                {
                    if(waypointDestination >= target.Length -1)
                {
                    movingForward = false;
                    Invoke("DecreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
                }
                else
                {
                        Invoke("IncreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
                }

                }

                else
                {
                    if (waypointDestination <= 0)
                    {
                        movingForward = true;
                        Invoke("IncreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
                    }
                    else
                    {
                        Invoke("DecreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
                    }
                }

            }
        }
        
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            //isMoving = false;
        }

        if(collision.gameObject.tag == "Bomb")
        {
            isMoving = false;
            if(movingForward)
            {
                Invoke("DecreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
            } else
            {
                Invoke("IncreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
            }

            movingForward = !movingForward;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Bomb")
        {
            isMoving = false;
            if (movingForward)
            {
                Invoke("DecreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
            }
            else
            {
                Invoke("IncreaseWaypointDestination", Random.Range(minDelayTime, maxDelayTime));
            }

            movingForward = !movingForward;
    }

    }

    public void Defeated()
    {
        if(!isDefeated)
        {
            isDefeated = true;
            GameManager myGameManager = FindObjectOfType<GameManager>();
            myGameManager.UpdateScore(scoreValue);
            myGameManager.EnemyIsDefeated();
            myAudioSource.Play();
            Destroy(gameObject, 0.7f);
        }

    }

    private void IncreaseWaypointDestination()
    {
        if(waypointDestination +1 < target.Length)
        {
            waypointDestination++;
        }

        isMoving = true;
    }

    private void DecreaseWaypointDestination()
    {
        if(waypointDestination -1 >= 0)
        {
            waypointDestination--;
        }

        isMoving = true;
    }

    private void UpdateAnimator()
    {
        myAnimator.SetBool("isWalking", isMoving);
    }

}
