using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class gameInLoading : MonoBehaviour,IEventHandler
{
    private float nowValue = 0;
    private float tarvalue = 0;
    private float stepvalue = 0;
    private float maxvalue = 100f;
    private Slider slider = null;
    public void onEventHandler(string eventName, List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if (eventName.Equals("inGameLoadingValue"))
        {
            float addValue = (float)objects[0];
            bool isEnd = (bool)objects[1];
            if(isEnd == false && tarvalue < maxvalue)
            {
                tarvalue += addValue;
                if(tarvalue >= (maxvalue - 1f))
                {
                    tarvalue = maxvalue - 2f;//不要超出去
                }
            }
            else
            {
                tarvalue = maxvalue;
                nowValue = maxvalue - 0.03f;
            }
            Debug.Log("max = " + tarvalue.ToSafeString());
            float stepvalue1 = (tarvalue - nowValue) / 20f;
            if(stepvalue1 < 0.015f)
            {
                stepvalue1 = 0.015f;
            }
            stepvalue = stepvalue1;
            if (isEnd)
            {
                utilsTools.sendEvent("inGameLoadingBarEnd");
            }
        }
        else if(eventName.Equals("initViewShowData"))
        {
            gameObject.SetActive(true);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        
        slider = this.GetComponent<Slider>();
        slider.value = 0;
        slider.maxValue = maxvalue;
        slider.minValue = 0;
        utilsTools.onEvent(this, "inGameLoadingValue");
        utilsTools.onEvent(this, "initViewShowData");
    }
    // Update is called once per frame
    void Update()
    {
        if (utilsTools.isEqualByValue(stepvalue, 0.0f, 0.00000003f))
        {
            return;
        }
        nowValue = nowValue + stepvalue;
        if(nowValue >= maxvalue)
        {
            nowValue = maxvalue;
            slider.value = nowValue;
            stepvalue = 0.0f;
            //需要通知出去满了
            Debug.Log("end ------------");
            utilsTools.sendEvent("inGameLoadingBarEnd");
            return;
        }
        else
        {
            if(nowValue > tarvalue)
            {
                nowValue = tarvalue;
                stepvalue = 0.0f;
            }
            slider.value = nowValue;
        }        
    }
    public void OnDestroy()
    {
        IRunEventManages.unOn(this, null);
    }
}
