using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class DragController : BaseNote
{
    private SpriteRenderer m_DragSpriteRenderer;

    private void Awake()
    {
        //获取自身变换组件
        _transform = gameObject.GetComponent<Transform>();
        _anchorPointTransform = _transform.Find("DragAnchorPoint").GetComponent<Transform>();
        _rendererTransform = _transform.Find("DragRenderer").GetComponent<Transform>();
        //获取自身渲染器组件
        m_DragSpriteRenderer = _anchorPointTransform.GetComponent<SpriteRenderer>();
    }
    private void Update()
    {
        UpdateRenderer(_songInformation.SongCurrentTime, _BPMGroup.CurrentNoteSpeed);
        UpdateAnchorPoint(_songInformation.SongCurrentTime, _BPMGroup.CurrentNoteSpeed, _BPMGroup.Beat);
    }
    private Color A0 = new Color(255, 255, 255, 0);
    private Color A255 = new Color(255, 255, 255, 255);
    //用于控制物体颜色的参数t
    private float t = 0;
    /// <summary>
    /// 更新定位点
    /// </summary>
    protected override void UpdateAnchorPoint(float currentTime, float noteSpeed, float beatTime)
    {
        if (TargetTime - beatTime > currentTime) return;
        t += Time.deltaTime / noteSpeed;
        m_DragSpriteRenderer.color = Color.Lerp(A0, A255, t);
    }
    /// <summary>
    /// 用于被外部实例化的公开方法
    /// </summary>
    public void Init(Vector3 pos, float dragTime, SongInformationContainer container,BPMGroup timing)
    {
        Vector3 ViewPos = new Vector3(pos.x, pos.y, 0);
        //获得当前轨道的容器引用
        _songInformation = container;
        
        //初始化自身目标时间
        TargetTime = dragTime;
        //重置自身定位标记的位置
        _transform.position = ViewPos;
        //重置自身渲染层的位置
        _rendererTransform.position = pos;
        //重置自身位置，它用于计算偏移量
        //m_Pos = pos;
        //将失效标记重置
        _BPMGroup = timing;
        IsValid = false;
        m_DragSpriteRenderer.color = A0; t = 0;
        //将自身标记初始颜色设置为透明
    }
}
