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

[RequireComponent(typeof(HingeJoint))]
public class Lever : MonoBehaviour
{

    private float minX => hinge.limits.min;
    private float maxX => hinge.limits.max;

    [SerializeField] [Range(0f, .5f)] private float deadZonePercentage;

    [SerializeField] private float currentLevel;
    [SerializeField] [Range(.2f, .7f)] private float multiplier;


    [SerializeField] private Pipe pipe;
    [SerializeField] private PipeGroup pipeGroup;

    private HingeJoint hinge;

    // Start is called before the first frame update
    void Start()
    {
        hinge = GetComponent<HingeJoint>();
    }

    // Update is called once per frame
    void Update()
    {
        //transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, minY, maxY), transform.position.z);

        currentLevel = 1 - (((maxX - hinge.angle) / (maxX - minX)) * 2);


        if (Math.Abs(currentLevel) > deadZonePercentage)
        {
            // change the water level of the pipe
            pipeGroup.ChangeWaterForPipe(currentLevel * multiplier, pipe);
            //Debug.Log($"Going {currentLevel}");
        }
        

    }
}
