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

public class ScaleSurface : MonoBehaviour
{
    [SerializeField] private Transform[] _slots;

    private Transform[] _slotOccupied;

    private void Awake()
    {
        _slotOccupied = new Transform[_slots.Length];
    }

    public void PlaceObject(Transform obj)
    {
        obj.SetParent(null);

        for (int i = 0; i < _slotOccupied.Length; i++)
            if (_slotOccupied[i] == obj)
                _slotOccupied[i] = null;

        for (int i = 0; i < _slots.Length; i++)
        {
            if (_slotOccupied[i] == null)
            {
                _slotOccupied[i] = obj;

                Collider col = _slots[i].GetComponent<Collider>();
                Vector3 targetPos = col != null ? col.bounds.center : _slots[i].position;

                obj.position = targetPos;
                obj.rotation = _slots[i].rotation;

                MassController mc = GetComponentInParent<MassController>();
                if (mc == null) mc = FindObjectOfType<MassController>();
                if (mc != null)
                    mc.AddObject(obj.GetComponent<Rigidbody>());

                return;
            }
        }

        obj.SetParent(null, true);
    }

    public void RemoveObject(Transform obj)
    {
        for (int i = 0; i < _slotOccupied.Length; i++)
            if (_slotOccupied[i] == obj)
                _slotOccupied[i] = null;
    }
}
