using FairyGUI;
using UnityEngine;

namespace ET
{
    public class CheckResUpdate : MonoBehaviour
    {
        void Start()
        {
            var view = UIPackage.CreateObject("CheckForResUpdate", "CheckForResUpdate").asCom;
            view.name = "CheckForResUpdate";
            GRoot.inst.AddChild(view);

            var progress = view.GetChild("processbar").asProgress;
            var txtProgress = view.GetChild("updateInfo");
            
            YooAssetProxy.InitHostCallbacks((onStateChanged) =>
            {
                txtProgress.text = onStateChanged.CurrentStates.ToString();

                if (onStateChanged.CurrentStates == EPatchStates.PatchDone)
                {
                    progress.value = 1f;
                    txtProgress.text = "加载游戏中……";
                    GameObject.Destroy(this.gameObject);
                }
            }, (onProcessUpdated) =>
            {
                string currentSizeMB = (onProcessUpdated.CurrentDownloadSizeBytes / 1048576f).ToString("f1");
                string totalSizeMB = (onProcessUpdated.TotalDownloadSizeBytes / 1048576f).ToString("f1");
                string text =
                    $"更新资源中:{onProcessUpdated.CurrentDownloadCount}/{onProcessUpdated.TotalDownloadCount} {currentSizeMB}MB/{totalSizeMB}MB";

                txtProgress.text = text;
                progress.value = onProcessUpdated.CurrentDownloadSizeBytes * 1.0f /
                    onProcessUpdated.TotalDownloadSizeBytes;
            });
        }
    }
}