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

public class LiquidContainer : MonoBehaviour
{
    public enum LiquidType
    {
        None, Water, Milk, Oil, Honey,
        StrawberrySyrup, CherrySyrup, Chlorophyll, Phenolphthalein, Bromothymol
    }

    [SerializeField] private Renderer _liquidRenderer;
    [SerializeField] private float _minScaleY = 0.01f;
    [SerializeField] public float capacity = 0.01f;
    [SerializeField] private float _emptyMassKg = 0.05f;

    public LiquidType liquidType = LiquidType.None;
    [Range(0f, 1f)] public float amount = 0f;
    public Color color = Color.blue;
    public float density = 1.0f;

    private Rigidbody _rb;

    public bool IsEmpty() => amount <= 0f;
    public bool IsFull() => amount >= 1f;
    public float GetTotalMassKg() => _emptyMassKg + capacity * amount * density;

    static float GetDensity(LiquidType type)
    {
        switch (type)
        {
            case LiquidType.Honey: return 1.4f;
            case LiquidType.StrawberrySyrup: return 1.1f;
            case LiquidType.CherrySyrup: return 1.05f;
            case LiquidType.Milk: return 1.03f;
            case LiquidType.Bromothymol: return 1.02f;
            case LiquidType.Chlorophyll: return 1.01f;
            case LiquidType.Water: return 1.0f;
            case LiquidType.Oil: return 0.9f;
            case LiquidType.Phenolphthalein: return 0.79f;
            default: return 1.0f;
        }
    }

    private void Awake()
    {
        _rb = GetComponent<Rigidbody>();
    }

    private void Start()
    {
        if (liquidType != LiquidType.None && amount > 0f)
            density = GetDensity(liquidType);
        if (_liquidRenderer != null && amount > 0f && _liquidRenderer.material.HasProperty("_c1"))
            color = _liquidRenderer.material.GetColor("_c1");
        UpdateVisual();
        UpdateMass();
    }

    private void OnValidate()
    {
        if (liquidType != LiquidType.None && amount > 0f)
            density = GetDensity(liquidType);
    }

    public void AddLiquid(Color newColor, float addAmount, float liquidDensity, LiquidType type)
    {
        float canAdd = Mathf.Clamp(addAmount, 0f, 1f - amount);
        if (canAdd <= 0f) return;
        color = Color.Lerp(color, newColor, canAdd / (amount + canAdd));
        amount += canAdd;
        density = liquidDensity;
        liquidType = type;
        UpdateVisual();
        UpdateMass();
    }

    public float RemoveLiquid(float removeAmount)
    {
        float taken = Mathf.Min(amount, removeAmount);
        amount -= taken;
        if (amount <= 0f)
        {
            liquidType = LiquidType.None;
            density = 1f;
        }
        UpdateVisual();
        UpdateMass();
        return taken;
    }

    public void UpdateMass()
    {
        if (_rb == null) return;
        _rb.mass = _emptyMassKg + capacity * amount * density;
    }

    public void UpdateVisual()
    {
        if (_liquidRenderer == null) return;
        if (amount <= 0f)
        {
            _liquidRenderer.gameObject.SetActive(false);
            return;
        }
        _liquidRenderer.gameObject.SetActive(true);
        Vector3 scale = _liquidRenderer.transform.localScale;
        scale.y = Mathf.Max(_minScaleY, amount);
        _liquidRenderer.transform.localScale = scale;
        Material mat = _liquidRenderer.material;
        if (mat.HasProperty("_c1")) mat.SetColor("_c1", color);
        if (mat.HasProperty("_c2")) mat.SetColor("_c2", color);
        if (!mat.HasProperty("_c1") && !mat.HasProperty("_c2")) mat.color = color;
    }
}
