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

public class BalanceScale : MonoBehaviour
{
    [SerializeField] private Transform[] _leftSlots;
    [SerializeField] private Transform[] _rightSlots;
    [SerializeField] private Transform[] _scaleMarks;
    [SerializeField] private Transform _slider;
    [SerializeField] private float _animationSpeed = 2f;

    private Transform[] _leftOccupied;
    private Transform[] _rightOccupied;
    private Vector3 _targetSliderPos;

    private void Awake()
    {
        _leftOccupied = new Transform[_leftSlots.Length];
        _rightOccupied = new Transform[_rightSlots.Length];
        if (_slider != null)
            _targetSliderPos = _slider.localPosition;
    }

    private void Update()
    {
        if (_slider == null) return;
        _slider.localPosition = Vector3.Lerp(
            _slider.localPosition,
            _targetSliderPos,
            Time.deltaTime * _animationSpeed
        );
    }

    public void PlaceLeft(Transform obj)
    {
        Place(obj, _leftSlots, _leftOccupied);
    }

    public void PlaceRight(Transform obj)
    {
        Place(obj, _rightSlots, _rightOccupied);
    }

    private void Place(Transform obj, Transform[] slots, Transform[] occupied)
    {
        for (int i = 0; i < occupied.Length; i++)
            if (occupied[i] == obj) occupied[i] = null;

        for (int i = 0; i < slots.Length; i++)
        {
            if (occupied[i] == null)
            {
                occupied[i] = obj;
                obj.SetParent(null);
                Collider col = slots[i].GetComponent<Collider>();
                Vector3 targetPos = col != null ? col.bounds.center : slots[i].position;
                obj.position = targetPos;
                obj.rotation = slots[i].rotation;
                UpdateSlider();
                return;
            }
        }
        obj.SetParent(null, true);
    }

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

    private float GetMass(Transform[] occupied)
    {
        float total = 0f;
        foreach (var obj in occupied)
        {
            if (obj == null) continue;
            LiquidContainer lc = obj.GetComponent<LiquidContainer>()
                ?? obj.GetComponentInChildren<LiquidContainer>();
            Rigidbody rb = obj.GetComponent<Rigidbody>();
            if (lc != null) total += lc.GetTotalMassKg();
            else if (rb != null) total += rb.mass;
        }
        return total;
    }

    private void UpdateSlider()
    {
        if (_scaleMarks == null || _scaleMarks.Length == 0) return;

        float leftMass = GetMass(_leftOccupied);
        float rightMass = GetMass(_rightOccupied);

        float displayMass = Mathf.Max(leftMass, rightMass);

        int targetIndex = Mathf.Clamp(Mathf.RoundToInt(displayMass), 0, _scaleMarks.Length - 1);

        _targetSliderPos = _slider.parent != null
            ? _slider.parent.InverseTransformPoint(_scaleMarks[targetIndex].position)
            : _scaleMarks[targetIndex].position;
    }
}
