using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PushPlatform.Common.Util
{
    public class FileUtil
    {
        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static bool Exists(string fullpath)
        {
            return System.IO.File.Exists(fullpath);
        }

        /// <summary>
        /// 检查文件夹是否存在
        /// </summary>
        /// <param name="folderPath"></param>
        public static bool ExistsPath(string folderPath, bool isCreate = false)
        {
            var exist = System.IO.Directory.Exists(folderPath);

            if (!exist && isCreate)
            {
                System.IO.Directory.CreateDirectory(folderPath);
            }

            return exist;
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="savePath"></param>
        public static void CreateDirectory(string savePath)
        {
            System.IO.Directory.CreateDirectory(savePath);
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteDirectory(string path)
        {
            if (path.IsNullOrEmpty())
                return;

            if (System.IO.Directory.Exists(path))
            {
                System.IO.Directory.Delete(path, true);
            }
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="content">写入的内容</param>
        public static void WriteFile(string path, string content)
        {
            FileInfo info = new FileInfo(path);

            if (!Directory.Exists(info.DirectoryName))
            {
                Directory.CreateDirectory(info.DirectoryName);
            }

            using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                {
                    writer.Write(content);

                    writer.Flush();
                }
            }
        }

        public static void WriteFile(string path, MemoryStream stream)
        {
            FileInfo info = new FileInfo(path);

            if (!Directory.Exists(info.DirectoryName))
            {
                Directory.CreateDirectory(info.DirectoryName);
            }

            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var buffer = stream.ToArray();
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadFile(string path)
        {
            string fileContent = string.Empty;

            StreamReader sr = new StreamReader(path, Encoding.UTF8);
            try
            {
                fileContent = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                return fileContent;
            }
            finally
            {
                sr.Close();
            }
            return fileContent;

        }

        public static void CopyDirectory(string srcDir, string tgtDir)
        {
            DirectoryInfo source = new DirectoryInfo(srcDir);
            DirectoryInfo target = new DirectoryInfo(tgtDir);

            if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("父目录不能拷贝到子目录！");
            }

            if (!source.Exists)
            {
                return;
            }

            if (!target.Exists)
            {
                target.Create();
            }

            FileInfo[] files = source.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                File.Copy(files[i].FullName, target.FullName + @"\" + files[i].Name, true);
            }

            DirectoryInfo[] dirs = source.GetDirectories();

            for (int j = 0; j < dirs.Length; j++)
            {
                CopyDirectory(dirs[j].FullName, target.FullName + @"\" + dirs[j].Name);
            }
        }

        /// <summary>  
        /// 从一个目录将其内容复制到另一目录  
        /// </summary>  
        /// <param name="directorySource">源目录</param>  
        /// <param name="directoryTarget">目的目录</param>  
        public static void CopyFolderTo(string directorySource, string directoryTarget)
        {
            //检查是否存在目的目录  
            if (!Directory.Exists(directoryTarget))
            {
                Directory.CreateDirectory(directoryTarget);
            }
            //先来复制文件  
            DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);
            FileInfo[] files = directoryInfo.GetFiles();
            //复制所有文件  
            foreach (FileInfo file in files)
            {
                if (!File.Exists(Path.Combine(directoryTarget, file.Name)))
                {
                    file.CopyTo(Path.Combine(directoryTarget, file.Name));
                }
            }
            //最后复制目录  
            DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();
            foreach (DirectoryInfo dir in directoryInfoArray)
            {
                CopyFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));
            }
        }

    }
}
