4/19/21

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

public class Spoke_Handler : MonoBehaviour
{
    // +x,+y,+z,-x,-y,-z
    public List<Spoke_AllShape> spoke_AS = new List<Spoke_AllShape>();
    public Rigidbody rb;

    private AF_Instance af_Instance;

    public bool isBeingHeld = false;

    public SphereCollider sphereCollider;

    public AF_Network_Grabable netGrabbable;

    public Touch_Lighting_and_Sound tls;

    void Start()
    {
        af_Instance = AF_Instance.Instance;
        tls = GetComponent<Touch_Lighting_and_Sound>();
        //TAG Spokes to access target transforms
        for (int i = 0; i<spoke_AS.Count; i++)
        {
            spoke_AS[i].SetParent_AllShape(this, i);
            tls.initializeColors_in_Spoke(spoke_AS[i]);
        }

        //TEST
        //for (int i = 0; i < spoke_AS.Count; i++)
        //{
        //    tls.Light_Occupied(spoke_AS[i],true);
        //}
    }
    //HANDLE IT HERE
    public void SpokeTriggered(int idx, Spoke_AllShape sourceSpoke)
    {
        af_Instance.Initiate_AllShape_Bond(spoke_AS[idx], sourceSpoke);
    }

    public void setOccupied(int spokeIDX, bool bOccupied, Spoke_AllShape connectedSpoke)
    {
        spoke_AS[spokeIDX].bOccupied = bOccupied;
        tls.Light_Occupied(spoke_AS[spokeIDX], bOccupied);

        spoke_AS[spokeIDX].connectedSpoke = connectedSpoke;
    }

    //public void Set_All_Colliders(bool bOn)
    //{
    //    sphereCollider.enabled = bOn;
    //    rb.isKinematic = true; //MORE CONSIDERATIONS HERE
    //    for (int i = 0; i < spoke_AS.Count; i++)
    //    {
    //        spoke_AS[i].enableCollider(bOn);
    //    }
    //}

    public int Closest_Fin(Vector3 testPos)
    {
        int idxMin = 0;
        float minDistance = 1000000f;
        float dist;
        for (int i = 0; i < spoke_AS.Count; i++)
        {
            dist = (spoke_AS[i].fin_LOC.position - testPos).sqrMagnitude;
            if(dist<minDistance)
            {
                idxMin = i;
                minDistance = dist;
            }
        }
        return idxMin;
    }

    public void SetAllColliders(bool bOn)
    {
        sphereCollider.enabled = bOn;
        rb.isKinematic = true; //MORE CONSIDERATIONS HERE
        for (int i = 0; i < spoke_AS.Count; i++)
        {
            spoke_AS[i].bOccupied = false;
            spoke_AS[i].enableCollider(bOn);          
        }
    }

    public void SetAllActive_Colliders(bool bOn, bool bSphereCollider)
    {
        sphereCollider.enabled = bSphereCollider;
        rb.isKinematic = true; //MORE CONSIDERATIONS HERE
        for (int i = 0; i < spoke_AS.Count; i++)
        {
            if (spoke_AS[i].bOccupied)
            {
                spoke_AS[i].enableCollider(false);
            }
            else
            {
                spoke_AS[i].enableCollider(bOn);
            }
        }
    }

    public void Reset_Spokes_On_Grab()
    {
        for (int i = 0; i < spoke_AS.Count; i++)
        {
            if (spoke_AS[i].bOccupied)
            {
                spoke_AS[i].setOccupied(false);
            }
        }
    }

    void Update()
    {
        
    }
}