using Mall.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Mall.Common.Plugin
{
    /// <summary>
    /// 文件操作帮助类
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 网站文件夹虚拟目录
        /// </summary>
        public static string VirtualDirectory = Config.VirtualDirectory;

        /// <summary>
        /// 行程特色删除指定文件夹文件
        /// </summary>
        /// <param name="filePath">文件夹路径</param>
        public static void DeleteFile(string filePath)
        {
            string newPath = AppContext.BaseDirectory+ "\\" + filePath;
            DirectoryInfo theFolder = new DirectoryInfo(newPath);
            if (theFolder.Exists)
            {
                FileSystemInfo[] files = theFolder.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件
                    if (file != null)
                    {
                        file.Delete();
                    }
                }
            }
        }

        /// <summary>
        /// 删除文件扩展方法
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteFileExt(string filePath)
        {
            string newPath = AppContext.BaseDirectory + "\\" + VirtualDirectory + filePath;
            File.Delete(newPath);
        }

        /// <summary>
        /// 根据TCID获取行程特色图片
        /// </summary>
        /// <param name="TCID"></param>
        /// <returns></returns>
        public static List<string> GetTripFileList(int TCID)
        {
            string filepath = VirtualDirectory + "/Feature/" + TCID + "/";

            List<string> fileList = new List<string>();
            DirectoryInfo theFolder = new DirectoryInfo(AppContext.BaseDirectory + "\\" + filepath);
            if (!theFolder.Exists)
            {
                theFolder.Create();
            }
            FileSystemInfo[] files = theFolder.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                //是文件
                if (file != null)
                {
                    fileList.Add(Config.ViewFileSiteUrl + "/Feature/" + TCID + "/" + file.Name);
                }
            }
            return fileList;
        }

        /// <summary>
        /// 批量删除文件
        /// </summary>
        /// <param name="fileList"></param>
        public static void DeleteFileList(List<string> fileList)
        {
            foreach (var item in fileList)
            {
                string newPath = AppContext.BaseDirectory + "\\" + VirtualDirectory + item;
                File.Delete(newPath);
            }
        }


        /// <summary>
        /// 文件移动
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="newFilePath"></param>
        public static void FileCopy(string filePath, string newFilePath)
        {
            string newTempFilePath = filePath.Substring(filePath.IndexOf("/")); ;
            var strPath = AppContext.BaseDirectory + "\\" + VirtualDirectory + newTempFilePath; //System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newTempFilePath);
            string newfilePath = AppContext.BaseDirectory + "\\" + VirtualDirectory  +newFilePath.Substring(0, strPath.LastIndexOf('\\') - 1); //System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory) + newFilePath.Substring(0, strPath.LastIndexOf('\\') - 1);
            if (!Directory.Exists(newfilePath))
            {
                Directory.CreateDirectory(newfilePath);
            }
            string sourceFile = AppContext.BaseDirectory + "\\" + VirtualDirectory + newTempFilePath;// System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newTempFilePath);
            string targetFile = AppContext.BaseDirectory + "\\" + VirtualDirectory + newFilePath;//System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newFilePath);
            File.Move(sourceFile, targetFile);
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool FileExists(string filePath)
        {
            return File.Exists(AppContext.BaseDirectory + "\\" + VirtualDirectory + filePath);
            //return File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + filePath));
        }

        /// <summary>
        /// 写byte[]到fileName
        /// </summary>
        /// <param name="fileName">保存至硬盘路径</param>
        /// <param name="pReadByte">byte[]</param>
        /// <returns></returns>
        public static bool WriteByteToFile(string fileName, byte[] pReadByte)
        {
            FileStream pFileStream = null;
            try
            {
                pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
                pFileStream.Write(pReadByte, 0, pReadByte.Length);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
            return true;
        }
    }
}
