using System.CodeDom;
using System.Collections.Generic;
using UnityEngine;

/*
逻辑说明:
1.依据头部,尾部目标时间计算出自身头部,尾部相对于0平面的位移
2.依据头部,尾部的位移,更新自身的mesh,通过动态修改mesh
3.当头部到达0平面时,若是被点击的状态,则将_currentHead作为更新mesh的依据
4.期间每一个8分音符检查一次自身的判定状态
5.若是未被点击状态,改变自身颜色,返回miss事件
6.当自身尾部小于等于0时,返回prefect事件
*/

/// <summary>
/// DataFlow的控制脚本,为了保持封装性它甚至有属于自己的对象池
/// </summary>
public class DataFlowController : BaseNote
{
    //首尾位置
    [SerializeField] private float _startPointXPos;
    [SerializeField] private float _startPointYPos;
    [SerializeField] private float _endPointXPos;
    [SerializeField] private float _endPointYPos;
    //起始时间
    [SerializeField] private float _endTime;
    [SerializeField] private float _width;

    public DataFlowUnit dataFlowUnitPrefab;
    Stack<DataFlowUnit> dataFlowUnitPool = new Stack<DataFlowUnit>();
    void Awake()
    {
        _transform = gameObject.GetComponent<Transform>();
    }
    /// <summary>
    /// 弹栈
    /// </summary>
    private DataFlowUnit GetView()
    {
        DataFlowUnit retObj;

        if (dataFlowUnitPool.Count > 0)
        {
            retObj = dataFlowUnitPool.Pop();
        }
        else
        {
            retObj = Instantiate<DataFlowUnit>(dataFlowUnitPrefab,_transform);
        }
        retObj.gameObject.SetActive(true);
        retObj.enabled = true;

        return retObj;
    }
    /// <summary>
    /// 压栈
    /// </summary>
    private void ReturnToPool(DataFlowUnit obj)
    {
        if (obj != null)
        {
            obj.gameObject.SetActive(false);
            obj.enabled = false;
            dataFlowUnitPool.Push(obj);   
        }
    }

    private void CreateDataFlow( float valueInterval,ReferencePointLocation referencePoint,float currentTime)
    {

        //指定起始坐标
        Vector2 startPoint = new Vector2(_startPointXPos, _startPointYPos);
        Vector2 endPoint = new Vector2(_endPointXPos, _endPointYPos);

        Vector2 tempStartPoint = startPoint;

        while (currentTime < _endTime)
        {
            if (currentTime + valueInterval > _endTime) break;

            //计算出点的位置
            var tempEndPoint = BezierFunctions.GetBezierPoint(
                startPoint, 
                endPoint, 
                referencePoint, 
                (currentTime + valueInterval) / _endTime
                );

            GetView().Init(
                tempStartPoint, 
                tempEndPoint, 
                currentTime, 
                currentTime + valueInterval, 
                0.4f, 
                _songInformation, 
                _BPMGroup
                );

            //自增时间
            currentTime += valueInterval;
            //移动尾部
            tempStartPoint = tempEndPoint;
        }
        //这里生成最后一段蛇
        GetView().Init(
            tempStartPoint, 
            endPoint, 
            currentTime, 
            _endTime, 
            0.4f, 
            _songInformation, 
            _BPMGroup
            );

    }
    public void Init(Vector2 startPoint, Vector2 endPoint, float startTime, float endTime, float width, SongInformationContainer container, BPMGroup timing, ReferencePointLocation referencePoint)
    {
        //赋值坐标
        _startPointXPos = startPoint.x;
        _startPointYPos = startPoint.y;
        _endPointXPos = endPoint.x;
        _endPointYPos = endPoint.y;

        //给属性赋值
        _width = width;
        _songInformation = container;

        //获得时间
        TargetTime = startTime;
        _endTime = endTime;

        _BPMGroup = timing;

        CreateDataFlow(_BPMGroup.Beat / 4, referencePoint, startTime);
    }

}
