using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Encoder = System.Drawing.Imaging.Encoder;

namespace SlnMesnac.Common
{
    public static class FileHelper
    {
        #region
        /// <summary>
        ///quality: 0-100 ,越小压缩程度也越高，但是图片质量越低
        /// </summary>
        /// <param name="imageData"></param>
        /// <param name="quality"></param>
        /// <returns></returns>
        public static byte[] CompressImageData(byte[] imageData, long quality)
        {
            try
            {
                using (MemoryStream memoryStream = new MemoryStream(imageData))
                {
                    using (Image image = Image.FromStream(memoryStream))
                    {
                        using (MemoryStream compressedStream = new MemoryStream())
                        {
                            ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

                            Encoder myEncoder = Encoder.Quality;
                            EncoderParameters myEncoderParameters = new EncoderParameters(1);
                            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
                            myEncoderParameters.Param[0] = myEncoderParameter;

                            image.Save(compressedStream, jpgEncoder, myEncoderParameters);
                            return compressedStream.ToArray();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"压缩图片出错：{ex.Message}");
                return null;
            }
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
        #endregion

        /// <summary>
        /// 查找指定目录下的文件
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="photoName"></param>
        /// <returns></returns>
        public static byte[] FindPhoto(string directoryPath)
        {
            try
            {
                // 获取目录中的所有文件
                string[] files = Directory.GetFiles(directoryPath);

                // 检查是否有文件
                if (files.Length == 0)
                {
                    Console.WriteLine("文件夹中没有照片文件");
                    return null;
                }

                // 假设这里简单地选择第一个文件作为示例
                string firstFile = files[0];
                Console.WriteLine($"找到文件：{firstFile}");

                // 读取第一个文件的字节数组
                byte[] bytes = File.ReadAllBytes(firstFile);
                Console.WriteLine($"文件大小：{bytes.Length} 字节");

                // 返回照片的字节数组
                return bytes;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件夹出错：{ex.Message}");
                return null;
            }
        }


        /// <summary>
        /// 删除文件夹所有照片
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteAllPictures(string path)
        {
            try
            {
                // 检查照片路径是否存在
                if (Directory.Exists(path))
                {
                    // 获取文件夹中的所有文件
                    string[] files = Directory.GetFiles(path);

                    // 遍历文件并删除
                    foreach (string file in files)
                    {
                        File.Delete(file);
                        Console.WriteLine($"Deleted file: {file}");
                    }

                    Console.WriteLine("All pictures deleted successfully.");
                }
                else
                {
                    Console.WriteLine("无路径");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"DeleteAllPictures异常: {ex.Message}");
            }
        }



        public static void  SaveImage(byte[] imageData, string imageName, string savePath)
        {
            // 根据当前日期创建目录路径
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
            string directoryPath = Path.Combine(savePath, currentDate);

            // 确保目录存在，如果不存在则创建
            Directory.CreateDirectory(directoryPath);

            // 将目录路径与照片名称组合
            string filePath = Path.Combine(directoryPath, imageName);

            try
            {
                // 将照片字节数组写入文件
                File.WriteAllBytes(filePath, imageData);
                Console.WriteLine($"图片 '{imageName}' 已成功保存至 '{filePath}'。");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存图片 '{imageName}' 到 '{filePath}' 时出错: {ex.Message}");
            }
        }



    }
}
