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

public class CableGenerator : MonoBehaviour {
    
    public static Cable CreateCableWithSettingsAtPos (SocketChannelSettings channel, CableSettings cableSettings, Vector3 defaultPos)
    {
        List<CableSection> cableSections = new List<CableSection>();
        GameObject cableObject = new GameObject();
        Cable generatedCable = cableObject.AddComponent<Cable>();
        generatedCable.channel = channel;

        cableObject.transform.position = defaultPos;
        cableObject.gameObject.name = "GeneratedCable_" + cableSettings.cableName;
        defaultPos += new Vector3(0, cableSettings.heightOfSection/2, 0);
        
        LineRenderer cableRenderer = generatedCable.gameObject.AddComponent<LineRenderer>();
        generatedCable.lineRenderer = cableRenderer;
        cableRenderer.startWidth = 0.3f;
        cableRenderer.endWidth= 0.3f;

        cableRenderer.positionCount = cableSettings.numberOfSections;
        cableRenderer.material = channel.channelCableMaterial;

        for (int i = 0; i < cableSettings.numberOfSections; i++)
        {
            GameObject cableSectionPrefab = cableSettings.sectionPrefab;
            if (cableSectionPrefab != null)
            {
                cableSectionPrefab.SetActive(false);
                GameObject cableSectionObject = Instantiate(cableSectionPrefab);
                cableSectionPrefab.SetActive(true);

                cableSectionObject.transform.SetParent(cableObject.transform);

                CableSection addedSection = cableSectionObject.GetComponent<CableSection>();
                cableSections.Add(addedSection);

                addedSection.meshRenderer.enabled = false;

                float heightIteration = cableSettings.heightOfSection;
                cableSectionObject.transform.position = defaultPos + new Vector3(0, i * heightIteration, 0);

                if (i > 0)
                {
                    addedSection.joint.connectedBody = cableSections [i - 1].sectionBody;
                }
                else
                {
                    addedSection.sectionBody.isKinematic = true;
                }
                if (i == cableSettings.numberOfSections - 1)
                {
                    addedSection.isTerminal = true;
                    generatedCable.terminalSection = addedSection;

                    CableGrabSphere grabSphere = addedSection.gameObject.AddComponent<CableGrabSphere>();
                    grabSphere.generateColliderWithRadius(cableSettings.grabSphereRadius);
                    grabSphere.gameObject.layer = LayerMask.NameToLayer("CableGrabSphere");
                    grabSphere.parentCable = generatedCable;
                }
                if (i % 2 == 0)
                {
                    addedSection.sectionBody.useGravity = false;
                }
                generatedCable.cableSections = cableSections;
                addedSection.gameObject.SetActive(true);
            }
        }
        generatedCable.isActive = true;
        generatedCable.UpdateLineRendererPositions();
        generatedCable.terminalSection.sectionBody.AddForce(new Vector3(Random.Range(0, 0.1f), Random.Range(0, 0.1f), Random.Range(0, 0.1f)));

        return generatedCable;
    }
}
