using Unity.VisualScripting;
using UnityEngine;

public class CEMoveState : CEBaseState
{
    CEVariables cEVariables;
    public Vector2 targetPosition;
    public Vector2 currentTargetVector;
    public int randomNumber;
    public Vector2 currentVelocity;
    public float timeUntilSwitch;

    public override void EnterState(CEStateManager CornerEnemy) //passiert nur das erste mal wo er aufgerufen wird
    {
        cEVariables = CornerEnemy.GetComponent<CEVariables>();
        currentTargetVector = new Vector2(Random.Range(0,2)*2-1, Random.Range(0,2)*2-1);
        RandomNeighborVector();
        timeUntilSwitch = cEVariables.moveDuration;
    }

    public override void UpdateState(CEStateManager CornerEnemy)    //passiert jeden frame
    {
        CalculateTargetPosition();
        timeUntilSwitch -= Time.smoothDeltaTime;
        if(timeUntilSwitch >= 0)
        {
            MoveToPosition(CornerEnemy);
        }
        else
        {
            CornerEnemy.SwitchState(CornerEnemy.ShootState);
            Debug.Log("switch to shoot");
        }
    }

    void CalculateTargetPosition()
    {
        targetPosition = cEVariables.playerData.playerPosition + currentTargetVector * cEVariables.moveDistance;
    }

    void RandomNeighborVector()
    {
        if(Random.Range(0,2) == 0)
        {
            currentTargetVector.x = currentTargetVector.x - (currentTargetVector.x * 2);
        }
        else
        {
            currentTargetVector.y = currentTargetVector.y - (currentTargetVector.y * 2);
        }
    }

    void MoveToPosition(CEStateManager CornerEnemy)
    {
        CornerEnemy.transform.position = Vector2.SmoothDamp(CornerEnemy.transform.position, targetPosition, ref currentVelocity, cEVariables.moveSpeed * Time.deltaTime);  
    } 
}
 