using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Flick类型的枚举
/// </summary>

public delegate void FlickEventHandler(float EventValue);
public class FlickController : BaseNote
{
    private FlickDirection flickDirection;
    private SpriteRenderer anchorPoint_SpriteRenderer;
    private SpriteRenderer flickRederer_SpriteRenderer;
    private float rotation, move;
    private bool isTriggers;
    /// <summary>
    /// 禁止往里面注册匿名函数!!!!!!
    /// </summary>
    public event FlickEventHandler CameraRotationEvent;
    /// <summary>
    /// 禁止往里面注册匿名函数!!!!!!
    /// </summary>
    public event FlickEventHandler CameraMoveEvent;
    void Awake()
    {
        _transform = gameObject.GetComponent<Transform>();
        _anchorPointTransform = _transform.Find("FlickAnchorPoint").GetComponent<Transform>();
        _rendererTransform = _transform.Find("FlickRenderer").GetComponent<Transform>();
        anchorPoint_SpriteRenderer = _anchorPointTransform.gameObject.GetComponent<SpriteRenderer>();
        flickRederer_SpriteRenderer = _rendererTransform.gameObject.GetComponent<SpriteRenderer>();
    }
    void Update()
    {
        UpdateRenderer(_songInformation.SongCurrentTime, _BPMGroup.CurrentNoteSpeed);
        UpdateAnchorPoint(_songInformation.SongCurrentTime, _BPMGroup.CurrentNoteSpeed, _BPMGroup.Beat);
    }

    private void OperationCamera()
    {
        if (!isTriggers) return;
        CameraRotationEvent?.Invoke(rotation);
        CameraMoveEvent?.Invoke(move);
        CameraRotationEvent = null;
        CameraMoveEvent = null;
    }
    /// <summary>
    /// 初始化一个任意方向的Flick
    /// </summary>
    public void Init(Vector3 pos, float flickTime, SongInformationContainer container, Dictionary<FlickDirection, Sprite> FlickSpritePairs, BPMGroup timing)
    {
        Vector3 ViewPos = new Vector3(pos.x, pos.y, 0);
        //获得当前轨道的容器引用
        _songInformation = container;
        //初始化自身目标时间
        TargetTime = flickTime;
        //重置自身定位标记的位置
        _transform.position = ViewPos;
        //重置自身渲染层的位置
        _rendererTransform.position = pos;
        //将失效标记重置
        IsValid = false;
        //将自身标记初始颜色设置为透明
        //m_flickSpriteRenderer.color = new Color(255, 255, 255, 0); t = 0;
        flickDirection = FlickDirection.Any;
        anchorPoint_SpriteRenderer.sprite = FlickSpritePairs[FlickDirection.Any];
        flickRederer_SpriteRenderer.sprite = FlickSpritePairs[FlickDirection.Any];
        _BPMGroup = timing;
    }
    /// <summary>
    /// 初始化一个指定方向的Flick
    /// </summary>
    public void Init(Vector3 pos, float flickTime, SongInformationContainer container, FlickDirection Direction, Dictionary<FlickDirection, Sprite> FlickSpritePairs, BPMGroup timing)
    {
        Vector3 ViewPos = new Vector3(pos.x, pos.y, 0);
        //获得当前轨道的容器引用
        _songInformation = container;
        //初始化自身目标时间
        TargetTime = flickTime;
        //重置自身定位标记的位置
        _transform.position = ViewPos;
        //重置自身渲染层的位置
        _rendererTransform.position = pos;
        //将失效标记重置
        IsValid = false;
        //将自身标记初始颜色设置为透明
        //m_flickSpriteRenderer.color = new Color(255, 255, 255, 0); t = 0;
        //初始化Flick自身方向
        flickDirection = Direction;
        //依据自身方向初始化自身渲染
        anchorPoint_SpriteRenderer.sprite = FlickSpritePairs[Direction];
        flickRederer_SpriteRenderer.sprite = FlickSpritePairs[Direction];
        _BPMGroup = timing;

    }
    /*
    策划案补充:
    Flick判定
    >任意方向Flick:只有Prefect判定,在其抵达目标时间时,在5个单位长度内手指处于移动状态即可完成判定,早于目标时间不判定,超过g判区间触发miss事件
    >指定方向note:有Great.Prefect判定,在3个单位长度内初次被击中时,变为激发态,启动[滑动检查],超过g判区间触发miss事件
    ->滑动检查:在每一帧检查手指ID,手指位移,手指位移方向是否正确,当三者均满足时,与标准时间进行比较,触发对应事件
    ->条件:初始点为第一次击中点,,每一帧更新目标手指ID的手指目前位置,当向量模长为2单位长度,且方向与标准向量夹角不超过π/2,开始结算
    */
    
}
