using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_LoadingPanel : BasePanel
{
    /// <summary>
    /// 当前进度
    /// </summary>
    private float currentProgress = 0;

    /// <summary>
    /// 目标进度
    /// </summary>
    private float targetProgress = 0;

    /// <summary>
    /// 是否更新
    /// </summary>
    private bool isUpdate = false;

    /// <summary>
    /// 显示
    /// </summary>
    public override void ShowMe()
    {
        EventCenter.Instance.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
    }

    /// <summary>
    /// 隐藏面板
    /// </summary>
    public override void HideMe()
    {
        EventCenter.Instance.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
    }

    private void Update()
    {
        if (isUpdate)
        {
            if (currentProgress < targetProgress)
            {
                currentProgress += Time.deltaTime;
                if (currentProgress >= targetProgress)
                    currentProgress = targetProgress;
                GetControl<Slider>("Slider").value = currentProgress;
            }
            else
            {
                isUpdate = false;
                if (currentProgress == 1)
                    UIManager.Instance.HidePanel<UI_LoadingPanel>();
            }
        }
    }

    /// <summary>
    /// 更新进度
    /// </summary>
    /// <param name="progress"></param>
    private void UpdateProgress(float progress)
    {
        Debug.Log(progress);
        targetProgress += progress;
        isUpdate = true;
    }

    public void Init()
    {
        currentProgress = 0;
        targetProgress = 0;
        isUpdate = false;
    }

}
