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;

    private void Start()
    {
        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)
            {
                if(movingForward)
                {
                    if(waypointDestination >= target.Length -1)
                {
                    movingForward = false;
                    waypointDestination--;
                }
                else
                {
                    waypointDestination++;
                }

                }

                else
                {
                    if (waypointDestination <= 0)
                    {
                        movingForward = true;
                        waypointDestination++;
                    }
                    else
                    {
                        waypointDestination--;
                    }
                }

            }
        }
        
    }

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

        if(collision.gameObject.tag == "Bomb")
        {
            isMoving = false;
            Debug.Log("Gotcha Bomb");
        }
    }


}
