using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SFramework
{
    public class SToolFunction
    {
        public static int GetFileSize(string fileFullPath)
        {
            System.IO.FileInfo oneFileInfo = new System.IO.FileInfo(fileFullPath);
            return (int)oneFileInfo.Length;
        }

        public static string GetAssetBundleFullPath(string abFullNameWithPath)
        {
            string[] nameInfo = abFullNameWithPath.Split('/');
            string realABName = nameInfo[nameInfo.Length - 1];
            string fullPath = abFullNameWithPath.Substring(0, abFullNameWithPath.Length - realABName.Length);
            return fullPath;
        }

        public static string GetDownloadSpeed(float alreadyDownloadBytes, float downloadTime)
        {
            float downloadSpeed = alreadyDownloadBytes / downloadTime; // Bytes Per Second.
            string extName = "B/s";

            if (System.Math.Floor(downloadSpeed / 1024) > 0)
            {
                downloadSpeed = downloadSpeed / 1024; // KB/s
                extName = "KB/s";
            }

            if (System.Math.Floor(downloadSpeed / 1024) > 0)
            {
                downloadSpeed = downloadSpeed / 1024; //MB/s
                extName = "MB/s";
            }

            downloadSpeed = (float)System.Math.Round(downloadSpeed, 2);
            string finalSpeed = downloadSpeed.ToString() + " " + extName;
            return finalSpeed;
        }

        public static void MakeSurePathExisted(string path)
        {
            if (System.IO.Directory.Exists(path))
                return;

            System.IO.Directory.CreateDirectory(path);
        }

        public static SFileInfo CreateFileInfo(string fileName, string fileFullPathWithName)
        {
            SFileInfo fileInfo = new SFileInfo();
            fileInfo.fileName = fileName;
            fileInfo.hashInfo = fileName.GetHashCode().ToString();
            fileInfo.fileSize = SToolFunction.GetFileSize(fileFullPathWithName);
            return fileInfo;
        }
    }
}
