private void selectHoveredPart()
    {
        if (hoveredPart != null)
        {
            selectedPart = hoveredPart;
            Vector3 minV = selectedPart.BoundingBox.MinValues.ToVector3() + new Vector3(-0.02f, -0.02f, -0.02f);
            Vector3 maxV = selectedPart.BoundingBox.MaxValues.ToVector3() + new Vector3(1.02f, 1.02f, 1.02f);

            Vector3[] boundingBox = new Vector3[16];
            boundingBox[0]  = new Vector3(minV.x, minV.y, minV.z);
            boundingBox[1]  = new Vector3(minV.x, minV.y, maxV.z);
            boundingBox[2]  = new Vector3(minV.x, maxV.y, maxV.z);
            boundingBox[3]  = new Vector3(minV.x, maxV.y, minV.z);
            boundingBox[4]  = new Vector3(minV.x, minV.y, minV.z);
            boundingBox[5]  = new Vector3(maxV.x, minV.y, minV.z);
            boundingBox[6]  = new Vector3(maxV.x, maxV.y, minV.z);
            boundingBox[7]  = new Vector3(maxV.x, maxV.y, maxV.z);
            boundingBox[8]  = new Vector3(maxV.x, minV.y, maxV.z);
            boundingBox[9]  = new Vector3(minV.x, minV.y, maxV.z);
            boundingBox[10] = new Vector3(minV.x, maxV.y, maxV.z);
            boundingBox[11] = new Vector3(maxV.x, maxV.y, maxV.z);
            boundingBox[12] = new Vector3(maxV.x, minV.y, maxV.z);
            boundingBox[13] = new Vector3(maxV.x, minV.y, minV.z);
            boundingBox[14] = new Vector3(maxV.x, maxV.y, minV.z);
            boundingBox[15] = new Vector3(minV.x, maxV.y, minV.z);

            for (int i = 0; i < boundingBox.Length; i++)
            {
                boundingBox[i] = boundingBox[i] + selectedPart.Position.ToVector3() - new Vector3(0.5f, 0.5f, 0.5f);
            }

            LineRenderer lines = lineRendererSelected.GetComponent <LineRenderer>();
            lines.SetPositions(boundingBox);

            // Remove hovered-box
            Vector3[] boundingBox2 = new Vector3[16];
            for (int i = 0; i < boundingBox2.Length; i++)
            {
                boundingBox2[i] = new Vector3(0f, 0f, 0f);
            }
            LineRenderer lines2 = GetComponent <LineRenderer>();
            lines2.SetPositions(boundingBox2);
        }
        else
        {
            selectedPart = null;
            Vector3[] boundingBox = new Vector3[16];
            for (int i = 0; i < boundingBox.Length; i++)
            {
                boundingBox[i] = new Vector3(0f, 0f, 0f);
            }
            LineRenderer lines = lineRendererSelected.GetComponent <LineRenderer>();
            lines.SetPositions(boundingBox);
        }
    }
    public bool RemovePart(PartBuilding part)
    {
        for (int i = 0; i < parts.Count; i++)
        {
            if (part == parts[i])
            {
                disconnectOverlappingConnectors(part);
                Destroy(part.gameObject);
                parts.RemoveAt(i);
                return(true);
            }
        }

        return(false);
    }
    private void connectOverlapingConnectors(PartBuilding placedPart)
    {
        bool oneComplication = false;

        for (int i = 0; i < placedPart.ConnectionPoints.Length; i++)
        {
            for (int j = 0; j < parts.Count; j++)
            {
                // First check the bounding box of the part

                if (parts[j] != placedPart && parts[j].BoundingBoxContains(placedPart.ConnectionPoints[i].ConnectorPosition.Add(placedPart.Position)))
                {
                    for (int k = 0; k < parts[j].ConnectionPoints.Length; k++)
                    {
                        if (parts[j].ConnectionPoints[k].ConnectorPosition.Add(parts[j].Position).Equals(placedPart.ConnectionPoints[i].ConnectorPosition.Add(placedPart.Position)))
                        {
                            bool canOneDirectionBe = false;
                            for (int l = 0; l < parts[j].ConnectionPoints[k].BoxCollidersAmount; l++)
                            {
                                bool canBe = parts[j].ConnectionPoints[k].CanBeConnected(placedPart.ConnectionPoints[i].Connectortype, placedPart.ConnectionPoints[i].FacedDirection, l);
                                if (canBe)
                                {
                                    canOneDirectionBe = true;
                                    break;
                                }
                            }

                            if (canOneDirectionBe)
                            {
                                parts[j].ConnectionPoints[k].IsConnected   = true;
                                placedPart.ConnectionPoints[i].IsConnected = true;
                            }
                        }
                    }

                    if (oneComplication)
                    {
                        break;
                    }
                }
            }

            if (oneComplication)
            {
                break;
            }
        }
    }
    private PartType getHoveredPartType()
    {
        Ray        ray = activeCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Connector"))
        {
            PartBuilding hitPart = partBuildingContainer.HitPart(hit.collider);

            if (hitPart != null)
            {
                return(hitPart.Type);
            }
        }
        return(PartType.NONE);
    }
    private void hoverPart(bool deHover)
    {
        hoveredPart = partBuildingContainer.HitPart(lastCollider);

        if (hoveredPart != null && deHover == false && hoveredPart != selectedPart)
        {
            Vector3 minV = hoveredPart.BoundingBox.MinValues.ToVector3() + new Vector3(-0.02f, -0.02f, -0.02f);
            Vector3 maxV = hoveredPart.BoundingBox.MaxValues.ToVector3() + new Vector3(1.02f, 1.02f, 1.02f);

            Vector3[] boundingBox = new Vector3[16];
            boundingBox[0]  = new Vector3(minV.x, minV.y, minV.z);
            boundingBox[1]  = new Vector3(minV.x, minV.y, maxV.z);
            boundingBox[2]  = new Vector3(minV.x, maxV.y, maxV.z);
            boundingBox[3]  = new Vector3(minV.x, maxV.y, minV.z);
            boundingBox[4]  = new Vector3(minV.x, minV.y, minV.z);
            boundingBox[5]  = new Vector3(maxV.x, minV.y, minV.z);
            boundingBox[6]  = new Vector3(maxV.x, maxV.y, minV.z);
            boundingBox[7]  = new Vector3(maxV.x, maxV.y, maxV.z);
            boundingBox[8]  = new Vector3(maxV.x, minV.y, maxV.z);
            boundingBox[9]  = new Vector3(minV.x, minV.y, maxV.z);
            boundingBox[10] = new Vector3(minV.x, maxV.y, maxV.z);
            boundingBox[11] = new Vector3(maxV.x, maxV.y, maxV.z);
            boundingBox[12] = new Vector3(maxV.x, minV.y, maxV.z);
            boundingBox[13] = new Vector3(maxV.x, minV.y, minV.z);
            boundingBox[14] = new Vector3(maxV.x, maxV.y, minV.z);
            boundingBox[15] = new Vector3(minV.x, maxV.y, minV.z);

            for (int i = 0; i < boundingBox.Length; i++)
            {
                boundingBox[i] = boundingBox[i] + hoveredPart.Position.ToVector3() - new Vector3(0.5f, 0.5f, 0.5f);
            }

            LineRenderer lines = GetComponent <LineRenderer>();
            lines.SetPositions(boundingBox);
        }
        else
        {
            hoveredPart = null;
            Vector3[] boundingBox = new Vector3[16];
            for (int i = 0; i < boundingBox.Length; i++)
            {
                boundingBox[i] = new Vector3(0f, 0f, 0f);
            }
            LineRenderer lines = GetComponent <LineRenderer>();
            lines.SetPositions(boundingBox);
        }
    }
    public void SwitchToMode(CursorMode mode)
    {
        switch (mode)
        {
        case CursorMode.IDLE:
            partView.GetComponent <PartView>().Visible = false;
            cursorMode = CursorMode.IDLE;
            break;

        case CursorMode.PLACING:
            hoveredPart  = null;
            selectedPart = null;
            hoverPart(true);
            selectHoveredPart();
            cursorMode = CursorMode.PLACING;
            break;
        }
    }
    public PartBuilding HitPart(Collider hitCollider)
    {
        if (hitCollider != null)
        {
            PartBuilding thePart = null;
            for (int i = 0; i < parts.Count; i++)
            {
                if (parts[i].IsOneOfYourColliders(hitCollider))
                {
                    thePart = parts[i];
                    break;
                }
            }

            return(thePart);
        }
        else
        {
            return(null);
        }
    }
    private void disconnectOverlappingConnectors(PartBuilding removedPart)
    {
        for (int i = 0; i < removedPart.ConnectionPoints.Length; i++)
        {
            for (int j = 0; j < parts.Count; j++)
            {
                // First check the bounding box of the part

                if (parts[j].BoundingBoxContains(removedPart.ConnectionPoints[i].ConnectorPosition.Add(removedPart.Position)))
                {
                    for (int k = 0; k < parts[j].ConnectionPoints.Length; k++)
                    {
                        if (parts[j].ConnectionPoints[k].ConnectorPosition.Add(parts[j].Position).Equals(removedPart.ConnectionPoints[i].ConnectorPosition.Add(removedPart.Position)))
                        {
                            parts[j].ConnectionPoints[k].IsConnected    = false;
                            removedPart.ConnectionPoints[i].IsConnected = false;
                        }
                    }
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        Ray        ray = activeCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Connector") && guiOpened == false)
        {
            if (cursorMode == CursorMode.PLACING && (lastCollider != hit.collider || forceUpdate) && partView.GetComponent <PartView>().IsNotEmpty)
            {
                //Vector3 hitpoint = hit.point;
                //hitpoint.x = (int)(hitpoint.x - 0.5f);
                //hitpoint.y = (int)(hitpoint.y + 1);
                //hitpoint.z = (int)(hitpoint.z + 0.5f);
                //Vector3 wayToOrigin = new Vector3(1f, 0f, 0f);
                //PartDirection originDirection = PartDirection.West;
                //PartRotation originRotation = PartRotation.Up;

                forceUpdate = false;

                lastCollider = hit.collider;

                fillAllPossibleConfigurations(hit.collider);

                applyCurrentConfiguration();

                //Debug.Log("Amount: " + configurations.Count);

                //ConnectorType connectorType = partView.GetComponent<PartView>().ConnectionPoints[connectorIndex].Connectortype;
                //PartDirection connectorDirection = partView.GetComponent<PartView>().ConnectionPoints[connectorIndex].FacedDirection;
                //PartType partType = partView.GetComponent<PartView>().Type;
                //PartDirection partDirection = partView.GetComponent<PartView>().Direction;
                //PartRotation partRotation = partView.GetComponent<PartView>().Rotation;
                //
                ////Vector3Int offsetedPartPosition = partView.GetComponent<PartView>().Position.Sub(partView.GetComponent<PartView>().ConnectionPoints[connectorIndex].ConnectorPosition);
                //
                //
                //bool canMatch = partBuildingContainer.HitCollider(hit.collider, connectorType, connectorDirection, out connectorPosition);
                //
                //if (canMatch)
                //{
                //    offsetedPartPosition = connectorPosition.Sub(partView.GetComponent<PartView>().ConnectionPoints[connectorIndex].ConnectorPosition);
                //
                //    canSomethingBeConnected = partBuildingContainer.CanBePlaced(partType, partDirection, partRotation, offsetedPartPosition);
                //
                //    if (canSomethingBeConnected)
                //    {
                //        partView.GetComponent<PartView>().Visible = true;
                //        //debugCube.transform.position = connectorPosition.ToVector3();
                //        partView.GetComponent<PartView>().SetPosition(offsetedPartPosition);
                //        Debug.Log("Can match");
                //    }
                //    else
                //    {
                //        canSomethingBeConnected = false;
                //        partView.GetComponent<PartView>().Visible = false;
                //        //debugCube.transform.position = new Vector3(0f, -5f, 0f);
                //        //Debug.Log("Can NOT match [1]");
                //    }
                //}
                //else
                //{
                //    canSomethingBeConnected = false;
                //    partView.GetComponent<PartView>().Visible = false;
                //    //debugCube.transform.position = new Vector3(0f, -5f, 0f);
                //    //Debug.Log("Can NOT match [0]");
                //}
            }

            if (cursorMode == CursorMode.IDLE && lastCollider != hit.collider)
            {
                lastCollider = hit.collider;

                hoverPart(false);
            }
        }
        else
        {
            if (cursorMode == CursorMode.IDLE)
            {
                hoverPart(true);
            }
        }

        if (Input.GetMouseButtonDown(0) && guiOpened == false) //&& canSomethingBeConnected
        {
            switch (cursorMode)
            {
            case CursorMode.PLACING:
                if (configurations.Count > 0)
                {
                    PartType      partType      = partView.GetComponent <PartView>().Type;
                    PartDirection partDirection = partView.GetComponent <PartView>().Direction;
                    PartRotation  partRotation  = partView.GetComponent <PartView>().Rotation;

                    partBuildingContainer.AddPart(partType, partDirection, partRotation, partView.GetComponent <PartView>().Position);

                    manager.ReducePartAmount(partType);

                    Debug.Log("Placed a part at: " + partView.GetComponent <PartView>().Position.ToString());
                }
                break;

            case CursorMode.IDLE:
                selectHoveredPart();
                break;
            }
        }

        if (Input.GetMouseButtonDown(1) && guiOpened == false)
        {
            switch (cursorMode)
            {
            case CursorMode.PLACING:
                if (configurations.Count > 0)
                {
                    configurationIndex++;
                    applyCurrentConfiguration();
                    //PartType partType = partView.GetComponent<PartView>().Type;
                    //PartDirection partDirection = partView.GetComponent<PartView>().Direction;
                    //PartRotation partRotation = partView.GetComponent<PartView>().Rotation;
                    //
                    //partBuildingContainer.AddPart(partType, partDirection, partRotation, offsetedPartPosition);
                    //
                    //Debug.Log("Placed a part at: " + offsetedPartPosition.ToString());
                }
                break;

            case CursorMode.IDLE:

                break;
            }
        }

        if (Input.GetMouseButtonDown(2) && guiOpened == false)
        {
            PartType type = getHoveredPartType();
            if (type != PartType.NONE)
            {
                SelectPart(type);
                SwitchToMode(CursorMode.PLACING);
            }

            switch (cursorMode)
            {
            case CursorMode.PLACING:
                break;

            case CursorMode.IDLE:
                break;
            }
        }

        //if (Input.GetButton("Key1") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x3);
        //}
        //else if (Input.GetButton("Key2") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x5);
        //}
        //else if (Input.GetButton("Key3") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x7);
        //}
        //else if (Input.GetButton("Key4") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x9);
        //}
        //else if (Input.GetButton("Key5") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x11);
        //}
        //else if (Input.GetButton("Key6") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x13);
        //}
        //else if (Input.GetButton("Key7") && guiOpened == false)
        //{
        //    SelectPart(PartType.Part1x1x15);
        //}
        //else if (Input.GetButton("Key8") && guiOpened == false)
        //{
        //    SelectPart(PartType.PartPinRound2x1);
        //}
        //else if (Input.GetButton("Key9") && guiOpened == false)
        //{
        //    SelectPart(PartType.PartTurn1x1x2);
        //}
        //else if (Input.GetButton("Key0") && guiOpened == false)
        //{
        //    SelectPart(PartType.PartPinRound3x1);
        //}



        else if (Input.GetKey(KeyCode.B) && guiOpened == false)
        {
            SwitchToMode(CursorMode.PLACING);
        }
        else if (Input.GetKey(KeyCode.V) && guiOpened == false)
        {
            SwitchToMode(CursorMode.IDLE);
        }

        if (Input.GetKey(KeyCode.Delete) && guiOpened == false)
        {
            if (selectedPart != null)
            {
                PartType removedType = selectedPart.Type;
                bool     success     = partBuildingContainer.RemovePart(selectedPart);

                if (success)
                {
                    manager.IncreasePartAmount(removedType);
                }

                Debug.Log("Tried to remove part: " + success.ToString());
                selectedPart = null;
                hoveredPart  = null;
                selectHoveredPart();
            }
        }


        if (Input.GetAxis("Mouse ScrollWheel") > 0 && guiOpened == false)
        {
            SelectConnector(connectorIndex + 1);
        }
        else if (Input.GetAxis("Mouse ScrollWheel") < 0 && guiOpened == false)
        {
            SelectConnector(connectorIndex - 1);
        }

        //if (Input.GetButton("Part1"))
        //{
        //    SelectPart(PartType.Part1x1x5);
        //}
        //if (Input.GetButton("Part2"))
        //{
        //    SelectPart(PartType.PartPinRound2x1);
        //}

        if (Input.GetKey(KeyCode.Escape))
        {
            //SceneManager.LoadScene("Garages");
            manager.ButtonBackToGarageClick();
        }
    }