using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExtensionsDev
{
    public static class ChooseFileExtension
    {

        /// <summary>
        /// 使用选择Wave文件的功能
        /// </summary>
        /// <param name="ctrl">ButtonEdit控件</param>
        /// <param name="onFileChanged">文件改变事件,参数为选择的文件和文件的采样率Hz</param>
        /// <returns></returns>
        public static ButtonEdit UseChooseWaveFile(this ButtonEdit ctrl, Action<string, int> onFileChanged = null)
        {
            ctrl.AllowDrop = true;
            ctrl.DragEnter += ctrl_DragEnter;
            ctrl.DragDrop += ctrl_DragDrop;
            ctrl.ButtonClick += Ctrl_ButtonClick;
            ctrl.TextChanged += (sender, e) =>
            {
                string file = (sender as Control).Text;
                if (!File.Exists(file) || onFileChanged == null) return;
                //从wav文件中自动读取采样率
                FileInfo info = new FileInfo(file);
                if (info.Extension.ToLower() == ".wav")
                {
                    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        fs.Position = 24;
                        byte[] data = new byte[4];
                        fs.Read(data, 0, 4);
                        var fsHz = BitConverter.ToInt32(data, 0);
                        if (fsHz > 0)
                        {
                            onFileChanged(file, fsHz);
                        }

                    };
                }
                else
                {
                    onFileChanged(file, 0);
                }

            };
            ctrl.Tag = nameof(UseChooseFile);
            return ctrl;
        }

        /// <summary>
        /// 使用通用文件选择功能
        /// </summary>
        /// <param name="ctrl">ButtonEdit控件</param>
        /// <returns></returns>
        public static ButtonEdit UseChooseFile(this ButtonEdit ctrl)
        {
            ctrl.AllowDrop = true;
            ctrl.DragEnter += ctrl_DragEnter;
            ctrl.DragDrop += ctrl_DragDrop;
            ctrl.ButtonClick += Ctrl_ButtonClick;
            ctrl.Tag = nameof(UseChooseFile);
            return ctrl;
        }
        public static ButtonEdit UseChooseDir(this ButtonEdit ctrl)
        {
            ctrl.UseChooseFile();
            ctrl.Tag = nameof(UseChooseDir);
            return ctrl;
        }

        private static void Ctrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var ctrl = sender as Control;
            if (ctrl.Tag?.ToString() == nameof(UseChooseFile))
            {
                OpenFileDialog xtraOpenFileDialog1 = new OpenFileDialog();
                xtraOpenFileDialog1.Multiselect = false;
                var currentDir = Environment.CurrentDirectory;
                if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Environment.CurrentDirectory = currentDir;
                    ctrl.Text = xtraOpenFileDialog1.FileName;
                }
            }
            else
            {
                OpenFileDialog xtraOpenFileDialog1 = new OpenFileDialog();
                xtraOpenFileDialog1.Multiselect = false;
                var currentDir = Environment.CurrentDirectory;
                if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Environment.CurrentDirectory = currentDir;
                    ctrl.Text = Path.GetDirectoryName(xtraOpenFileDialog1.FileName);
                }
            }
        }

        private static void ctrl_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private static void ctrl_DragDrop(object sender, DragEventArgs e)
        {
            var file = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            var ctrl = sender as ButtonEdit;
            bool dragFile = ctrl.Tag?.ToString() == nameof(UseChooseFile);
            if (dragFile)
                (sender as Control).Text = file;
            else
            {
                if (File.Exists(file))
                    (sender as Control).Text = Path.GetDirectoryName(file);
                else
                    (sender as Control).Text = file;

            }
        }
    }
}
