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)
                    GameManager.UIMgr.HidePanel<UI_LoadingPanel>();
            }
        }
    }

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

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init()
    {
        currentProgress = 0;
        targetProgress = 0;
        isUpdate = false;
        D_System system = GameManager.DataMgr.GetSystemInfo(GameManager.Instance.systemId);

        if (system == null)
            return;
        GameManager.ResourcesMgr.LoadAsync<Sprite>(Const.LoadBGPath + system.loadName, (sprite) =>
        {
            this.GetComponent<Image>().sprite = sprite;
        });


    }

}