using System.Text;
using Tiobon.Core.Common.DB.Dapper.Extensions;

namespace Tiobon.Core.Common.DB.Dapper.Utilities;
public class FileHelper : IDisposable
{
    public static string GetCurrentDownLoadPath()
    {
        return ("Download\\").MapPath();
    }

    #region 取得文件后缀名
    /// <summary>
    /// 
    /// </summary>
    /// <param name="path">路径 </param>
    /// <param name="fileName">文件名</param>
    /// <param name="content">写入的内容</param>
    /// <param name="appendToLast">是否将内容添加到未尾,默认不添加</param>
    public static void WriteFile(string path, string fileName, string content, bool appendToLast = false)
    {
        try
        {
            path = path.ReplacePath();
            fileName = fileName.ReplacePath();
            if (!Directory.Exists(path))//如果不存在就创建file文件夹
                Directory.CreateDirectory(path);

            using (FileStream stream = File.Open(path + fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] by = Encoding.Default.GetBytes($"-- {DateTime.Now:yyyy-MM-dd HH:mm:ss}" + "\r\n" + content);
                if (appendToLast)
                {
                    stream.Position = stream.Length;
                }
                else
                {
                    stream.SetLength(0);
                }
                stream.Write(by, 0, by.Length);
            }
        }
        catch (Exception Ex)
        {
            string error = Ex.Message;
        }
    }
    #endregion


    private bool _alreadyDispose = false;

    #region 构造函数
    public FileHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    ~FileHelper()
    {
        Dispose();
    }

    protected virtual void Dispose(bool isDisposing)
    {
        if (_alreadyDispose) return;
        _alreadyDispose = true;
    }
    #endregion

    #region IDisposable 成员

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    #endregion
}
