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


public class UFOMove : MonoBehaviour
{

    float m_moveTime = 0;
    [SerializeField] float m_moveSpeed = 2.0f;
    bool m_moving;
    [SerializeField] AnimationCurve m_moveCurve;
    [SerializeField] Transform m_3DUFO;
    CowControl m_cowChild = null;
    CowCube m_3DcowChild = null;

    [SerializeField] LayerMask m_3dMask;
    [SerializeField] Animator m_animator;

    Rect m_moveRect;

    bool m_beamActivated;

    void Start()
    {
        m_moving = false;
    }

    void Update()
    {


        m_moveTime -= Time.deltaTime;


        float haxis = Input.GetAxis("Horizontal");
        float vaxis = Input.GetAxis("Vertical");


        if (m_moving == false)
        {

            bool? hax = Mathf.Abs(haxis) < 0.1f ? null : haxis > 0.0f;
            bool? vax = Mathf.Abs(vaxis) < 0.1f ? null : vaxis > 0.0f;
            if (hax != null || vax != null)
            {
                StartCoroutine(MoveCoro(hax, vax));
            }
        
            if(Input.GetKeyDown(KeyCode.Space))
            {
                ToggleBeam();
            }
        }

    }

    public void SetMoveRect(Rect _r)
    {
        m_moveRect = _r;
    }

    IEnumerator MoveCoro(bool? _haxis, bool? _vaxis)
    {
        m_moving = true;

        Vector3 startPos = transform.position;
        Vector3 endPos = transform.position;
        Vector3 ufo3dAdd = Vector3.zero;
        if(_haxis != null)
        {
            float scalar = _haxis == true ? 1.0f : -1.0f;
            endPos += new Vector3(1.0f, 0.5f) * scalar;

            ufo3dAdd += Vector3.right * scalar;
        }
        else if (_vaxis != null)
        {
            float scalar = _vaxis == true ? 1.0f : -1.0f;
            endPos += new Vector3(-1.0f, 0.5f) * scalar;

            ufo3dAdd += Vector3.forward * scalar;
        }
        else
        {
            yield break;
        }

        if(!InBounds(ufo3dAdd + m_3DUFO.position))
        {
            Debug.Log("out of bounds");
            yield return null;
            m_moving = false;
            yield break;
        }



        if(m_3DcowChild != null)
        {
            if (!m_3DcowChild.CheckMove( ufo3dAdd))
            {
                ToggleBeam();
            }
        }   

        float t = 0;

        while(t <1)
        {
            t = Mathf.MoveTowards(t, 1, Time.deltaTime * m_moveSpeed);
            transform.position = Vector3.Lerp(startPos, endPos, m_moveCurve.Evaluate(t));
            yield return null;
        }

        m_3DUFO.position += ufo3dAdd;
        if(m_cowChild != null)
        {
            m_3DcowChild.Rotate();
        }

        PuzzleManager.Instance.OnMove();

        transform.position = endPos;
        m_moving = false;
    }

    void ToggleBeam()
    {
        if(m_beamActivated == false)
        {
            //check below
            //if not flicker out.

            Ray r = new Ray(m_3DUFO.position, Vector3.down);
            if(Physics.Raycast(r,out RaycastHit hit, 5, m_3dMask))
            {
                m_3DcowChild = hit.transform.GetComponent<CowCube>();
                m_3DcowChild.Grab(m_3DUFO);
                m_cowChild = m_3DcowChild.m_linkedCow;
                m_cowChild.SetAxis(hit.transform.tag == "CowHead");

                m_cowChild.Grab(transform);
                m_beamActivated = true;
                

            }
            else
            {
                //flicker out
                m_beamActivated = false;
            }
        }
        else
        {
            //drop thing
            m_beamActivated = false;
            m_3DcowChild.LetGo();
            m_cowChild.LetGo();

            m_3DcowChild = null;
            m_cowChild = null;

        }
        m_animator.SetBool("SpeedUp", m_beamActivated);
    }

    bool InBounds(Vector3 _pos)
    {
        if(_pos.x >= m_moveRect.x && _pos.x <= m_moveRect.width
        && _pos.z >=m_moveRect.height && _pos.z <= m_moveRect.y)
        {
            return true;
        }
            //Debug.Log($"pos: x: {_pos.x}, z: {_pos.z}");
        return false;
    }

}
