using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using NaughtyAttributes.Test;
using TMPro;
using UnityEngine;

public class Glove : MonoBehaviour
{
    [SerializeField] private GameObject[] _cylinders;
    [SerializeField] private Transform _graphics;

    private TrailRenderer[] _trails;
    private Renderer[] _renderers;

    // TODO: refactor to settings object
    [SerializeField] private float _baseTrailTime;
    [SerializeField] private float _blinkTrailTime;
    [SerializeField] private float _pulseTime;
    [SerializeField] private Material _trailMat;
    [SerializeField] private float _emissionScale;
    [SerializeField] private float _rotationSpeed;
    [SerializeField] private float _scaleFactor;

    private const string EMISSION_COLOR = "_EmissionColor";
    
    private void Start()
    {
        _trails = GetComponentsInChildren<TrailRenderer>();
        _renderers = GetComponentsInChildren<Renderer>();

        foreach (var trail in _trails)
        {
            trail.time = _baseTrailTime;
        }
    }

    private void LateUpdate()
    {
        _graphics.Rotate(Vector3.up * (Time.deltaTime * _rotationSpeed));
    }

    public void Pulse()
    {
        _graphics.transform.localScale = Vector3.one * _scaleFactor;
        _graphics.DOScale(Vector3.one, _pulseTime);

        foreach (var trail in _trails)
        {
            trail.time = _blinkTrailTime;
            trail.DOTime(_baseTrailTime, _pulseTime);
        }

        var baseEmission = _trailMat.GetColor(EMISSION_COLOR);
        var pulseEmission = baseEmission * _emissionScale;
        foreach (var renderer in _renderers)
        {
            renderer.material.SetColor(EMISSION_COLOR, pulseEmission);
            renderer.material.DOColor(baseEmission, _pulseTime);
        }
    }
}
