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

public class EnemyEnterIslandState : EnemyBaseState
{
    public override void EnterState(EnemyStateManager manager)
    {
        manager.agent.SetDestination(manager.pointToEnterIsland.position);
    }

    public override void UpdateState(EnemyStateManager manager)
    {
        SearchForPlayer(manager);
    }

    private void SearchForPlayer(EnemyStateManager manager){
        float distance = Vector3.Distance(manager.transform.position, manager.player.position);
        if(distance < manager.playerBehindDetectionThreshold){
            if(!CheckPlayerVisibility(manager))
                return;

            manager.SwitchState(manager.followState);
        }
        else if(distance < manager.playerInFrontDetectionThreshold){
            if(!CheckPlayerVisibility(manager))
                return;
            Vector3 playerDirection = manager.player.transform.position-manager.transform.position;
            if(Vector3.Dot(manager.transform.forward, playerDirection) > manager.dotProductThreshold){
                manager.SwitchState(manager.followState);
            }
        }
    }

    private bool CheckPlayerVisibility(EnemyStateManager manager){
        if(Physics.Linecast(manager.head.position, manager.playerHead.position, out var hit)){
            if(hit.transform.CompareTag("Player"))
                return true;
            else
                return false;
        }
        else{
            return false;
        }
    }
}
