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

public class SheepMove : MonoBehaviour
{
    [SerializeField]
    float rotateSpeed = 90f;

    [SerializeField]
    float MoveSpeed = 5f;

    [SerializeField]
    float limit_x = 30f;

    [SerializeField]
    float limit_z = 20f;

    Vector3 pos;
    Vector3 startPos;

    float angle;
    float angleDifferance;

    public Sheep sheep;
    enum State
    {
        Set,
        Look,
        Move,
        Wait
    }

    State state = State.Set;
    private void FixedUpdate()
    {
        switch (state)
        {
            case State.Set:
                pos = SetNextPosition();
                angle = (transform.forward = (pos - transform.position).normalized).z;
                angleDifferance =  angle - transform.forward.y;
                startPos = transform.position;
                

                break;

            case State.Look:
                //LookChange(pos,angle);
                state = State.Move;
                break;

            case State.Move:
                sheep.ChangeState(SheepState.Walk);
                SheepMoving(pos);
                break;

            case State.Wait:
                
                break;

                
        }
    }

    private Vector3 SetNextPosition()
    {       
        float angle = Random.Range(0, 360);
        float moveDistance = Random.Range(2, 10);

        Vector3 direction = Quaternion.Euler(0, angle, 0) * transform.rotation * Vector3.forward;

        Vector3 position = direction * moveDistance + transform.position;

        if((position.x < limit_x && -limit_x < position.x ) && (position.z < limit_z && -limit_z < position.z))
        {

            state = State.Look;
        }
      
        return position;
    }

    private void LookChange(Vector3 position, float angle)
    {

        float nowAngle = (transform.forward = ((transform.position * 2) - transform.position).normalized).z;
        //Quaternion.Lerp(Quaternion.Euler(Vector3.up * nowAngle), Quaternion.Euler(Vector3.up * angle), );
       

        if (angle == nowAngle)
        {
            state = State.Move;
            return;
        }

        
    }

    private void SheepMoving(Vector3 nextPosition)
    {
        transform.forward = (nextPosition - startPos).normalized;
        transform.Translate((nextPosition - startPos).magnitude * Time.fixedDeltaTime * MoveSpeed * Vector3.forward);

        if ((nextPosition - startPos).magnitude <= (transform.position - startPos).magnitude)
        {
            
            transform.position = nextPosition;
            state = State.Wait;
            StartCoroutine(Wait());
            
        }
    }

   
    IEnumerator Wait()
    {
        sheep.ChangeState(SheepState.Idle);
        yield return new WaitForSeconds(3f);
        state = State.Set;
    }

    private void OnDrawGizmos()
    {
        
        Gizmos.DrawRay(transform.position, new Vector3(Mathf.Cos(angle), 0,Mathf.Tan(angle)));
    }
}
