using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using DG.Tweening;
using TMPro;
using UnityEditor.UIElements;
using UnityEngine;

public class Jetpack : MonoBehaviour
{
    [Serializable]
    private struct Settings
    {
        public Material trailMat;
        public int trimIdx;
        public float pulseTime;
        public float emissionScale;
        public float lightIntensity;
        public AnimationCurve trailTimeCurve;
    }

    [SerializeField] private Settings _settings;

    [SerializeField] private TrailRenderer[] _trails;
    private float[] _baseTrailTimes;

    [SerializeField] private Light _light;

    private const string EMISSION_COLOR = "_EmissionColor";

    private void Start()
    {
        _baseTrailTimes = new float[_trails.Length];
        for (int i = 0; i < _trails.Length; i++)
        {
            _baseTrailTimes[i] = _trails[i].time;
            _trails[i].time = 0;
        }

        DisableTrails();

        _light.intensity = 0;
    }

    private void DisableTrails() => SetTrails(false);
    private void EnableTrails() => SetTrails(true);

    private void SetTrails(bool state)
    {
        for (int i = 0; i < _trails.Length; i++)
        {
            _trails[i].Clear();
            _trails[i].enabled = state;
        }
    }

    public void Pulse()
    {
        EnableTrails();

        for (int i = 0; i < _trails.Length; i++)
        {
            _trails[i].time = _baseTrailTimes[i];
            _trails[i]
                .DOTime(0, _settings.pulseTime);
            // .SetEase(_settings.trailTimeCurve);
        }

        var t = 0f;
        DOTween.To(
                () => t,
                x => { },
                1.0f,
                _settings.pulseTime)
            .OnComplete(DisableTrails);

        _light.intensity = _settings.lightIntensity;
        _light.DOIntensity(0, _settings.pulseTime);

        // var baseEmission = _settings.trailMat.GetColor(EMISSION_COLOR);
        // var pulseEmission = baseEmission * _settings.emissionScale;
    }
}