using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Avalonia;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Ursa.Controls;
using 常用工具集.Base;

namespace 常用工具集.ViewModel._03图片相关
{
    public class GIF分割ViewModel : ViewModelBase
    {

        public List<string> Paths { get; set; } = new List<string>();
        public string ButtonName { get; set; } = "导出文件";
        public string Path1 { get; set; } = "";
        public string Path2 { get; set; } = "";
        public DelegateCommand SelectGifCmd { get; set; }
        public DelegateCommand SelectExportPathCmd { get; set; }

        public DelegateCommand ExportCmd { get; set; }

        public GIF分割ViewModel()
        {
            SelectGifCmd = new DelegateCommand(SelectGifCmdFunc);
            SelectExportPathCmd = new DelegateCommand(SelectExportPathCmdFunc);
            ExportCmd = new DelegateCommand(ExportCmdFunc);
        }


        private async void SelectGifCmdFunc(object obj)
        {
            var sp = GlobalValues.StorageProvider;
            if (sp is null) return;
            FilePickerOpenOptions options = new FilePickerOpenOptions();
            options.Title = "选择GIF文件";
            options.AllowMultiple = false;
            options.FileTypeFilter = [
                new FilePickerFileType("GIF文件")
                {
                    Patterns = new[] { "*.gif" },
                    AppleUniformTypeIdentifiers = new[] { "public.gif" },
                    MimeTypes = new[] { "image/gif" }
                }];
            var result = await sp.OpenFilePickerAsync(options);
            if (result == null || result.Count == 0) return;
            Path1 = result.FirstOrDefault()?.Path.LocalPath;
        }

        private async void SelectExportPathCmdFunc(object obj)
        {
            var sp = GlobalValues.StorageProvider;
            if (sp is null) return;
            var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
            {
                Title = "Select Folder",
                AllowMultiple = false,
            });
            if (result == null || result.Count == 0) return;
            Path2 = result.FirstOrDefault()?.Path.LocalPath;
        }

        private void ExportCmdFunc(object obj)
        {
            string gifPath = Path1.Trim();
            string folderPaht = Path2.Trim();
            if (string.IsNullOrEmpty(gifPath) || string.IsNullOrEmpty(folderPaht))
            {
                MessageBox.ShowAsync("请选择gif路径或者导出路径!");
                return;
            }

            if (ButtonName == "导出文件")
            {
                ButtonName = "导出中...";
                //开启线程导出图片
                Thread thread = new Thread(Export);
                thread.Start();
            }
            else
            {
                MessageBox.ShowAsync("正在导出中!");
                return;
            }
        }

        /// <summary>
        /// 导出jpg图片
        /// </summary>
        private void Export()
        {
            string gifPath = Path1.Trim();
            string folderPaht = Path2.Trim();
            Image img = Image.FromFile(gifPath);
            FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]);
            //获取gif帧的数量
            int count = img.GetFrameCount(fd);
            //遍历保存图片
            for (int i = 0; i < count; i++)
            {
                img.SelectActiveFrame(fd, i);
                string imgPath = folderPaht + "\\frame" + (i + 1) + ".png";
                //判断同名文件是否存在
                if (File.Exists(imgPath))
                {
                    File.Delete(imgPath);
                }
                //保存图片 一定要设置格式 否则保存出来的图片都是一张图片
                img.Save(imgPath, ImageFormat.Png);
            }
            Dispatcher.UIThread.Invoke(() =>
            {
                GlobalValues.Success("文件导出成功!");
                ButtonName = "导出文件";
            });

        }
    }
}
