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

public class Pipe : MonoBehaviour
{

    private GameObject waterObject;

    [SerializeField] private float waterLevel;
    [SerializeField] private float waterChangeRate;

    public float WaterChangeRate => waterChangeRate;

    public float WaterLevel => waterLevel;

    // Start is called before the first frame update
    void Start()
    {
        waterObject = transform.Find("Water Level").gameObject;
        waterObject.transform.localScale = new Vector3(waterObject.transform.localScale.x, WaterLevel, waterObject.transform.localScale.z);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public float ChangeWaterLevel(float changeSpeed, bool rawAmount = false)
    {
        float changeAmount = changeSpeed * waterChangeRate * (rawAmount ? 1 : Time.deltaTime);
        float actualChangeAmount;

        if (WaterLevel + changeAmount > 1)
        {
            actualChangeAmount = 1 - WaterLevel;
        }
        else if (WaterLevel + changeAmount < 0)
        {
            actualChangeAmount = WaterLevel;
        }
        else
        {
            actualChangeAmount = changeAmount;
        }

        waterLevel = Mathf.Clamp(WaterLevel + actualChangeAmount, 0, 1);
        waterObject.transform.localScale = new Vector3(waterObject.transform.localScale.x, WaterLevel, waterObject.transform.localScale.z);
        return actualChangeAmount;
    }
}
