﻿using System;
using JLGames.RocketDriver.CSharp.Utils;
using JLGames.RocketDriver.Actions.Utils;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Loaderx
{
    internal static class BundleDefaultValues
    {
        public const bool WebglDynamicUrlOn = false;

        public const string WebUrl = "http://project.example.com";
        public static readonly string FileUrl = $"{Application.streamingAssetsPath}"; //StreamingAssets
        public const string UrlPattern = "/Bundles";
        public const string ProjectBasePath = "Assets/GameAssets";
        public const string CacheName = "BundleCache";
        public const int VersionSize = 1;
        public const int HistorySize = 3;
        public const int ReleaseCounterInterval = 16;
        public const int ReleaseTimerInterval = 30;
        public const bool EnableDebug = false;
        public const bool EnableCache = false;
    }

    [Serializable]
    public class BundleProperties
    {
        [Tooltip("Type of data source used\n使用的数据源类型")] [SerializeField]
        private BundleSource m_BundleSource = BundleSource.Web;

        [Tooltip("Web Settings\nWeb设置")] [SerializeField]
        private BundleSourceWeb m_BundleSourceWeb;

        [Tooltip("Streaming Settings\nStreaming设置")] [SerializeField]
        private BundleSourceStreaming m_BundleSourceStreaming;

        [Tooltip("FileDefug Settings\nFileDebug设置")] [SerializeField]
        private BundleSourceFileDebug m_BundleSourceFileDebug;

        [Tooltip("Project Base Path\n项目基础目录")] [SerializeField]
        private string m_ProjectBasePath = BundleDefaultValues.ProjectBasePath;

        [Tooltip("Bundle Cache Settings\nAssetBundle缓存设置")] [SerializeField]
        private BundleCache m_BundleCache;

        [Tooltip("Bundle Release Strategy\n资源释放策略")] [SerializeField]
        private BundleReleaseStrategy m_BundleReleaseStrategy;

        [Tooltip("Enable Debug?\n是否使用调试设置")] [SerializeField]
        private BundleDebug m_BundleDebug;

        public string RequestBaseUrl
        {
            get
            {
                if (m_BundleSource == BundleSource.Web)
                {
                    var webSitUrl = m_BundleSourceWeb.WebSiteUrl;
                    if (Application.platform == RuntimePlatform.WebGLPlayer && m_BundleSourceWeb.WebglDynamicUrl)
                    {
                        webSitUrl = AbPaths.GetDynamicWebglUrl();
                    }

//                    DebugUtil.Log("Web:", webSitUrl, m_WebSitePattern);
                    return UnityPathUtil.CombineUnityPath(webSitUrl, m_BundleSourceWeb.WebSitePattern);
                }

                if (m_BundleSource == BundleSource.StreamingAssets)
                {
//                    DebugUtil.Log("StreamingAssets:", Application.streamingAssetsPath, m_SteamingPattern);
                    return UnityPathUtil.CombineUnityPath(Application.streamingAssetsPath, m_BundleSourceStreaming.SteamingPattern);
                }

                if (m_BundleSource == BundleSource.FileDebug)
                {
//                    DebugUtil.Log("FileDebug:", m_FileUrl, m_FilePattern);
                    return UnityPathUtil.CombineUnityPath(m_BundleSourceFileDebug.FileUrl, m_BundleSourceFileDebug.FilePattern);
                }

                return "";
            }
        }

        /// <summary>
        /// Project Base Path
        /// 项目基础目录
        /// </summary>
        public string ProjectBasePath => m_ProjectBasePath;

        /// <summary>
        /// Bundle Cache Settings ： CacheName
        /// AssetBundle缓存设置: 缓存名称
        /// </summary>
        public string CacheName => m_BundleCache.CacheName;

        /// <summary>
        /// Bundle Cache Settings ： Version Size
        /// AssetBundle缓存设置: 版本容量
        /// </summary>
        public int VersionSize => m_BundleCache.VersionSize;

        /// <summary>
        /// Bundle Cache Settings ： History Size
        /// AssetBundle缓存设置: 记录容量
        /// </summary>
        public int HistorySize => m_BundleCache.HistorySize;

        /// <summary>
        /// Bundle Cache Settings ： Cache Path
        /// AssetBundle缓存设置: 缓存路径
        /// </summary>
        public string RunningCachePath => m_BundleDebug.RunningCachePath;

        /// <summary>
        /// Bundle Release Strategy Mode
        /// 资源释放策略模式
        /// </summary>
        public BundleReleaseMode AssetReleaseMode => m_BundleReleaseStrategy.AssetReleaseMode;

        /// <summary>
        /// Bundle Release Strategy : By Timer
        /// 资源释放策略：计时
        /// </summary>
        public int ReleaseTimeInterval => m_BundleReleaseStrategy.TimeInterval;

        /// <summary>
        /// Bundle Release Strategy : By Counter
        /// 资源释放策略: 计数
        /// </summary>
        public int ReleaseCounterInterval => m_BundleReleaseStrategy.CounterInterval;

        /// <summary>
        /// Enable Debug?
        /// 是否使用调试设置
        /// </summary>
        public bool CacheEnabled => m_BundleDebug.CacheEnabled;

        public override string ToString()
        {
            return $"BundleProperties{{BundleSource={m_BundleSource}, ProjectBasePath={m_ProjectBasePath}}}";
        }
    }
}