namespace XGame.Framework.FileSystem
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public static partial class File
    {
        private static readonly IFileOperator _fileOperator = CreateFileOperator();
        private static readonly IDirectoryOperator _directoryOperator = CreateDirectoryOperator();

        #region File Operator

        /// <summary>
        /// 获取文件名
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="withoutExt"></param>
        /// <returns></returns>
        public static string GetFileName(string fullName, bool withoutExt = true)
        {
            return _fileOperator.GetFileName(fullName, withoutExt);
        }

        /// <summary>
        /// 拓展名
        /// </summary>
        /// <param name="fullName">文件全路径(含拓展名)</param>
        /// <returns>拓展名（不包含“.”）</returns>
        public static string Extension(string fullName)
        {
            return _fileOperator.Extension(fullName);
        }

        /// <summary>
        /// 文件是否存在(包外全路径)
        /// </summary>
        /// <param name="fullName">文件全路径(含拓展名)</param>
        /// <returns>True: 存在;  False: 不存在; </returns>
        public static bool ExistFile(string fullName)
        {
            return _fileOperator.Exist(fullName);
        }

        /// <summary>
        /// 删除文件(包外全路径)
        /// </summary>
        /// <param name="fullName">文件全路径(含拓展名)</param>
        /// <returns>True: 删除成功;  False: 删除失败;</returns>
        public static bool DeleteFile(string fullName)
        {
            return _fileOperator.Delete(fullName);
        }

        /// <summary>
        /// 获取文件所在文件夹路径(包外全路径)
        /// </summary>
        /// <param name="fullName">文件全路径(含拓展名)</param>
        /// <returns>文件夹全路径</returns>
        public static string GetDirectory(string fullName)
        {
            return _fileOperator.GetDirectory(fullName);
        }

        /// <summary>
        /// 复制文件(包外全路径)
        /// </summary>
        /// <param name="source">源文件</param>
        /// <param name="destination">目标文件路径</param>
        /// <returns>True: 复制成功; False: 复制失败;</returns>
        public static bool CopyFile(string source, string destination)
        {
            return _fileOperator.Copy(source, destination, true);
        }

        /// <summary>
        /// 异步复制文件
        /// </summary>
        /// <param name="source">源文件</param>
        /// <param name="destination">包外文件全路径</param>
        /// <param name="overwrite">覆盖</param>
        /// <returns>复制文件异步对象</returns>
        public static ICopyFileAsync CopyFileAsync(string source, string destination, bool overwrite = true)
        {
            return _fileOperator.CopyFileAsync(source, destination, overwrite);
        }

        /// <summary>
        /// 移动文件(包外全路径)
        /// </summary>
        /// <param name="source">源文件</param>
        /// <param name="destination">目标文件路径</param>
        public static void MoveFile(string source, string destination)
        {
            _fileOperator.Move(source, destination);
        }

        /// <summary>
        /// 写入字节数组(包外全路径)
        /// </summary>
        /// <param name="filePath">文件全路径(包外)</param>
        /// <param name="bytes">内容</param>
        public static void WriteAllBytes(string filePath, byte[] bytes)
        {
            _fileOperator.WriteAllBytes(filePath, bytes);
        }

        /// <summary>
        /// 写入文本(包外全路径)
        /// </summary>
        /// <param name="filePath">文件全路径(包外)</param>
        /// <param name="contents"></param>
        public static void WriteAllText(string filePath, string contents)
        {
            _fileOperator.WriteAllText(filePath, contents);
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path">文件全路径</param>
        /// <returns>字节流</returns>
        public static byte[] ReadAllBytes(string path)
        {
            return _fileOperator.ReadAllBytes(path);
        }

        /// <summary>
        /// 读取文本(UTF-8)
        /// </summary>
        /// <param name="path">文件全路径</param>
        /// <returns>文本</returns>
        public static string ReadAllText(string path)
        {
            return _fileOperator.ReadAllText(path);
        }

        /// <summary>
        /// 异步读取文件
        /// </summary>
        /// <param name="filePath">文件全路径(包内包外都可以)</param>
        /// <returns>文件内容</returns>
        public static IReadFileAsync ReadFileAsync(string filePath)
        {
            return _fileOperator.ReadFileAsync(filePath);
        }

        /// <summary>
        /// 重命名文件
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static bool RenameFile(string from, string to)
        {
            return _fileOperator.Rename(from, to);
        }

        #endregion

        #region Directory Operator

        /// <summary>
        /// 新建文件夹(逐级新建)
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <returns>True: 新建成功; False: 新建失败</returns>
        public static bool Mkdir(string fullDirectory)
        {
            return _directoryOperator.Mkdir(fullDirectory);
        }

        /// <summary>
        /// 删除文件夹(包外全路径)
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <param name="includeSelf">是否删除自身文件夹</param>
        /// <returns>True: 删除成功;  False: 删除失败;</returns>
        public static bool DeleteDirectory(string fullDirectory, bool includeSelf = true)
        {
            return _directoryOperator.Delete(fullDirectory, includeSelf);
        }

        /// <summary>
        /// 文件夹是否存在(包外全路径)
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <returns>True: 存在;  False: 不存在;</returns>
        public static bool ExistDirectory(string fullDirectory)
        {
            return _directoryOperator.Exist(fullDirectory);
        }

        /// <summary>
        /// 是否是文件夹(包外全路径)
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <returns>True: 是;  False: 不是;</returns>
        public static bool IsDirectory(string fullDirectory)
        {
            return _directoryOperator.Exist(fullDirectory);
        }

        /// <summary>
        /// 复制(含子文件夹和子文件)
        /// </summary>
        /// <param name="source">源文件夹</param>
        /// <param name="destination">目标文件夹(清空)</param>
        /// <returns>True: 复制成功; False: 复制失败</returns>
        public static bool CopyDirectory(string source, string destination)
        {
            return _directoryOperator.Copy(source, destination, true);
        }

        /// <summary>
        /// 获取文件夹名
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <returns>文件夹名(最后一个"/")</returns>
        public static string GetDirectoryName(string fullDirectory)
        {
            return _directoryOperator.GetDirectoryName(fullDirectory);
        }

        /// <summary>
        /// 获取文件夹下的所有文件和文件夹路径
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <returns></returns>
        public static string[] GetFileSystemEntries(string fullDirectory)
        {
            return _directoryOperator.GetFileSystemEntries(fullDirectory);
        }

        /// <summary>
        /// 异步删除文件夹
        /// </summary>
        /// <param name="fullDirectory">文件夹全路径</param>
        /// <param name="includeSelf">是否删除自身文件夹</param>
        /// <returns>True: 删除成功;  False: 删除失败;</returns>
        public static IDeleteDirectoryAsync DeleteAsync(string fullDirectory, bool includeSelf = true)
        {
            return _directoryOperator.DeleteAsync(fullDirectory, includeSelf);
        }

        /// <summary>
        /// 重命名文件夹
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static bool RenameDirectory(string from, string to)
        {
            Assert.IsTrue(ExistDirectory(from), $"File.RenameDirectory: {from} 文件夹不存在！！");
            Assert.IsTrue(!ExistDirectory(to), $"File.RenameDirectory: {to} 文件夹已存在！！");

            return _directoryOperator.Rename(from, to);
        }

        /// <summary>
        /// 获取文件夹下所有文件
        /// </summary>
        /// <param name="fullDirectory">根文件夹全路径</param>
        /// <param name="recursion">递归</param>
        /// <returns>文件全路径</returns>
        public static string[] GetFiles(string fullDirectory, bool recursion = true)
        {
            return _directoryOperator.GetFiles(fullDirectory, null, recursion);
        }

        /// <summary>
        /// 获取文件夹下指定拓展名文件
        /// </summary>
        /// <param name="fullDirectory">根文件夹全路径</param>
        /// <param name="extension">拓展名(".xxx")</param>
        /// <param name="recursion">递归</param>
        /// <returns>文件全路径</returns>
        public static string[] GetFiles(string fullDirectory, string extension, bool recursion = true)
        {
            return _directoryOperator.GetFiles(fullDirectory, extension, recursion);
        }

        #endregion

        #region Factory
        static IFileOperator CreateFileOperator()
        {
            return new CSharpFileOperator();
        }

        static IDirectoryOperator CreateDirectoryOperator()
        {
            return new CSharpDirectoryOperator();
        }
        #endregion

        #region Path

        /// <summary>
        /// 路径拼接
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public static string Combine(params string[] paths)
        {
            return System.IO.Path.Combine(paths).Replace('\\', '/');
        }

        #endregion
    }
}
