using UnityEngine;
using UnityEngine.Rendering.Universal;

public class Lichtsteuerung : MonoBehaviour
{
    private const uint INTENSITY = 0;
    private const uint INNER_RADIUS = 1;
    private const uint OUTER_RADIUS = 2;
    private static readonly uint[] VALUE_TYPE_LIST = {INTENSITY, INNER_RADIUS, OUTER_RADIUS};
    private float[] startTime;
    private float[] endTime;
    private float[] startValue;
    private float[] endValue;
    private bool[] fadingInProgress;
    private Light2D lightComponent;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        lightComponent = GetComponent<Light2D>();
        int numberOfValueTypes = VALUE_TYPE_LIST.Length;
        startTime = new float[numberOfValueTypes];
        endTime = new float[numberOfValueTypes];
        startValue = new float[numberOfValueTypes];
        endValue = new float[numberOfValueTypes];
        fadingInProgress = new bool[numberOfValueTypes];
        foreach (uint valueType in VALUE_TYPE_LIST){
            startTime[valueType] = 0;
            endTime[valueType] = 0;
            fadingInProgress[valueType] = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        foreach (uint valueType in VALUE_TYPE_LIST){
            if (fadingInProgress[valueType]){
                float newValue;
                if (Time.time < endTime[valueType]){
                    float factor = (Time.time - startTime[valueType]) / (endTime[valueType] - startTime[valueType]);
                    newValue = factor * endValue[valueType] + (1f - factor) * startValue[valueType];
                } else {
                    fadingInProgress[valueType] = false;
                    newValue = endValue[valueType];
                }
                switch (valueType){
                    case INTENSITY:
                    lightComponent.intensity = newValue;
                    break;
                    case INNER_RADIUS:
                    lightComponent.pointLightInnerRadius = newValue;
                    break;
                    case OUTER_RADIUS:
                    lightComponent.pointLightOuterRadius = newValue;
                    break;
                }
            }
        }
    }

    public void FadeToIntensity(float intensity, float duration) {
        startValue[INTENSITY] = lightComponent.intensity;
        FadeCommon(INTENSITY, intensity, duration);
    }

    public void FadeToInnerRadius(float radius, float duration){
        startValue[INNER_RADIUS] = lightComponent.pointLightInnerRadius;
        FadeCommon(INNER_RADIUS, radius, duration);
    }

    public void FadeToOuterRadius(float radius, float duration){
        startValue[OUTER_RADIUS] = lightComponent.pointLightOuterRadius;
        FadeCommon(OUTER_RADIUS, radius, duration);
    }

    private void FadeCommon(uint valueType, float value, float duration){
        startTime[valueType] = Time.time;
        endTime[valueType] = startTime[valueType] + duration;
        fadingInProgress[valueType] = true;
        endValue[valueType] = value;
    }
}
