//#define ZOMBIEAI_DEBUG
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class ZombiePatrolState : BaseZombieAIState {

    private float waitAtTarget = 3;
    private float targetReachTime = 0;
    
    private NavMeshPath path;
    private int currentPathIndex;

    private bool targetReached = false;

    public override void InitState()
    {
        base.InitState();

        this.controller.Target = null;

        this.GenerateNewTarget();
    }

    public override void UpdateState()
    {
        base.UpdateState();

        if (!this.targetReached) {
            
            if (Vector3.Distance(this.controller.transform.position, this.controller.Zombie.Movement.NavAgent.destination) < 3) {
#if ZOMBIEAI_DEBUG
                Debug.Log("[Zombie AI] Target reached");
#endif
                this.targetReachTime = Time.time;
                this.targetReached = true;
            }
        }

        if (this.targetReached && (Time.time - this.targetReachTime > this.waitAtTarget)) {
            this.GenerateNewTarget();
            return;
        }
    }

    private void GenerateNewTarget () {

        Vector3 target = this.controller.transform.position + new Vector3(Random.Range(-15, 15), 0, Random.Range(-15, 15));
#if ZOMBIEAI_DEBUG
        Debug.Log("[Zombie AI] Generate new target: " + target);
#endif

        this.targetReached = false;
        this.controller.Zombie.Movement.MoveTo(target);
    }
}