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

public class CowCube : MonoBehaviour
{
    [SerializeField] CowCube m_otherCube;
    public CowControl m_linkedCow;

    bool m_grabbed;

    void Start()
    {
        
    }

    public void Grab(Transform _grabber)
    {
        //Debug.Log("grab");
        transform.parent = _grabber;
        m_grabbed = true;
        m_otherCube.transform.parent = transform;
    }

    public void LetGo()
    {
        //Debug.Log("let go");
        m_otherCube.transform.parent = null;
        m_grabbed = false;
        transform.parent = null;
    }

    public bool CheckMove(Vector3 _addPos)
    {
        Vector3 posME = new Vector3(_addPos.x, 0, _addPos.z) + transform.position;
        Vector3 posU = new Vector3(_addPos.x, 0, _addPos.z) + m_otherCube.transform.position;



        var colls = Physics.OverlapSphere(posME, 0.4f); //check if I can make it

        bool foundOther = false;
        foreach(var cl in colls)
        {
            if (cl.transform != transform && cl.transform != m_otherCube.transform)
            {
                foundOther = true;
                //Debug.Log("first found: " + cl.transform.name);
                break;
            }
        }
        if(foundOther)
        {
            return false;
        }

        //check rot
        colls = Physics.OverlapSphere(transform.position + transform.forward, 0.4f);
        foundOther = false;
        foreach (Collider cl in colls)
        {
            if (cl.transform != transform && cl.transform != m_otherCube.transform)
            {
                foundOther = true;
                //Debug.Log("second found: " + cl.transform.name);
                break;
            }
        }

        if(foundOther)//not rotatinf
        {
            //check other
            colls = Physics.OverlapSphere(posU, 0.4f);
            foundOther = false;
            foreach (Collider cl in colls)
            {
                if (cl.transform != transform && cl.transform != m_otherCube.transform)
                {
                    foundOther = true;
                    //Debug.Log("third found: " + cl.transform.name);
                    break;
                }
            }
            if(foundOther)
            {
                return false;
            }
        }

        return true;
    }

    public void Rotate()
    {
        //check first
        //Debug.Break();
        Collider[] colls = Physics.OverlapSphere(transform.position + transform.forward, 0.4f);
        bool foundOther = false;
        foreach(Collider cl in colls)
        {
            if(cl.transform != transform && cl.transform != m_otherCube.transform)
            {
                foundOther = true;
                //Debug.Log("found: " + cl.transform.name);
                break;
            }
        }

        if(!foundOther)
        {
            Vector3 rot = transform.eulerAngles;
            rot += Vector3.up * 90;
            transform.eulerAngles = rot;
            m_linkedCow.Rotate(true);
        }
        /*else
        {
            Debug.Log("NOT ROTATING");
        }*/
    }

    private void OnDrawGizmos()
    {
        
        if(m_grabbed)
        {
            Gizmos.color = Color.cyan;

            Gizmos.DrawLine(transform.position, transform.position + Vector3.up * 3);
            Gizmos.DrawWireSphere(transform.position + transform.forward, 0.4f);
        }
        Gizmos.DrawLine(transform.position, transform.position + transform.forward);
    }

}
