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

public class CableGrabSphere : MonoBehaviour {

    public Cable parentCable;
    public SphereCollider grabSphere;

    public float sphereRadius;

    public TopSocket topSocketPluggedInto;

    public void generateColliderWithRadius (float radius)
    {
        grabSphere = gameObject.AddComponent<SphereCollider>();
        grabSphere.isTrigger = true;
        grabSphere.radius = radius;
    }

    public void SetGrabbed (bool grabbed)
    {
        if (grabbed)
        {
            parentCable.terminalSection.sectionBody.isKinematic = true;
        }
        else
        {
            releaseTopSocket();
            parentCable.terminalSection.sectionBody.isKinematic = false;
        }
    }

    public void MoveToPosition (Vector3 posToMoveTo)
    {
        Debug.DrawRay(transform.position, posToMoveTo - transform.position, Color.green);
        parentCable.terminalSection.sectionBody.MovePosition(posToMoveTo);
    }

    public void joinToTopSocket (TopSocket topSocketToJoin)
    {
        topSocketPluggedInto = topSocketToJoin;
        topSocketPluggedInto.setConnectedCableSphere(this);
        parentCable.terminalSection.sectionBody.position = topSocketToJoin.transform.position;
    }

    public void releaseTopSocket ()
    {
        if (topSocketPluggedInto != null)
        {
            topSocketPluggedInto.connectedCableSphere = null;
            topSocketPluggedInto = null;
        }
    }
}
