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

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

    Rigidbody myRigidBody;

    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 void Start()
    {
        if(target.Length == 0)
        {
            isMoving = false;
        }
        myRigidBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (isMoving)
        {
            myRigidBody.MovePosition(Vector3.MoveTowards(transform.position, target[waypointDestination].position, Time.deltaTime * moveSpeed));
            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;
            Debug.Log("Gotcha Player");
        }

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

            movingForward = !movingForward;
            Debug.Log("Gotcha Bomb");
        }
    }

    public void Defeated()
    {
        FindObjectOfType<GameManager>().UpdateScore(scoreValue);
        Destroy(gameObject);
    }

    private void IncreaseWaypointDestination()
    {
        waypointDestination++;
        isMoving = true;
    }

    private void DecreaseWaypointDestination()
    {
        waypointDestination--;
        isMoving = true;
    }
}
