﻿using JLGames.RocketDriver.Actions.Loaderx;
using JLGames.RocketDriver.Actions.Utils;
using UnityEngine;
using UnityEngine.UI;

namespace JLGames.RocketDriver.Samples.LoaderDemo
{
    public class LoaderDemo : MonoBehaviour
    {
        [SerializeField] private GameObject m_Root;
        [SerializeField] private InputField m_InputAbName;
        [SerializeField] private InputField m_InputPath;
        [SerializeField] private InputField m_InputSubPath;
        [SerializeField] private Image m_DisplayImage;

        private void Start()
        {
            InitLoader();
        }

        private void InitLoader()
        {
            Loader.InitLoader("TestLoaderSettings");
            Loader.InitVersion(OnVersionInited);
        }

        private void OnVersionInited(AssetBundleManifest asset, bool suc)
        {
            DebugUtil.Log("OnVersionInited:", suc);
            if (suc)
            {
                ShowRootView();
            }
        }

        private void ShowRootView()
        {
            m_Root.SetActive(true);
        }

        public void OnClickRefresh()
        {
            RefreshAsset();
        }

        private void RefreshAsset()
        {
            var abName = m_InputAbName.text.Trim();
            if (string.IsNullOrEmpty(abName))
            {
                Debug.LogWarning($"Need Bundle Name!");
                return;
            }

            var assetPath = m_InputPath.text.Trim();
            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogWarning($"Need Asset Path!");
                return;
            }

            var subPath = m_InputSubPath.text.Trim();
            Loader.LoadBundleAsync(abName, (bRef, suc) =>
            {
                if (!suc)
                {
                    Debug.LogWarning($"Load Bundle({abName}) Fail!");
                    return;
                }

                PrintBundleInfo(bRef);

                if (string.IsNullOrEmpty(subPath))
                {
                    var img = Loader.LoadAssetSync<Sprite>(assetPath, bRef.Bundle);
                    var img2 = Loader.LoadAssetSync<Texture2D>(assetPath, bRef.Bundle);
                    DebugUtil.Log("看看：", img == null, img2 == null);
                    if (img == null)
                    {
                        Debug.LogWarning($"Load Asset({assetPath}) Fail!");
                        return;
                    }

                    m_DisplayImage.sprite = img;
                }
                else
                {
                    var img = Loader.LoadSubAssetSync<Sprite>(assetPath, subPath, bRef.Bundle);
                    if (img == null)
                    {
                        Debug.LogWarning($"Load Sub Asset({assetPath})({subPath}) Fail!");
                        return;
                    }

                    m_DisplayImage.sprite = img;
                }
            });
        }

        private void PrintBundleInfo(BundleRef bRef)
        {
            if (null == bRef) return;
            if (null == bRef.Bundle)
            {
                DebugUtil.LogWarning($"Bundle({bRef.BundleName}) is null.");
                return;
            }

            var assetNames = bRef.Bundle.GetAllAssetNames();
            foreach (var aName in assetNames)
            {
                DebugUtil.Log("AssetName:", aName);
            }

            var assets = bRef.Bundle.LoadAllAssets();
            foreach (var asset in assets)
            {
                DebugUtil.Log($"资源类型：{asset.GetType()}, 数源名：{asset.name}");
            }
        }
    }
}