using 智慧物流软件系统.ViewModel;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Window = System.Windows.Window;

namespace 智慧物流软件系统
{
    /// <summary>
    /// 提示框
    /// </summary>
    public partial class TipInputView : HandyControl.Controls.Window
    {
        public TipInputView()
        {
            InitializeComponent();
        }

        private void closeClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 显示输入提示框并尝试转为指定类型
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="content">内容</param>
        /// <param name="verify">验证委托</param>
        /// <param name="isVisCloseBut">是否显示关闭按钮</param>
        /// <param name="owner">窗体所有者</param>
        /// <returns>输入的内容（关闭为null）</returns>
        public static T? Show<T>(string content, Func<T, string> verify = null, bool isVisCloseBut = true, Window owner = null) where T : struct
        {
            return Show<T>(content, string.Empty, "请输入值", verify, isVisCloseBut, owner);
        }

        /// <summary>
        /// 显示输入提示框并尝试转为指定类型
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="content">内容</param>
        /// <param name="title">标题</param>
        /// <param name="contentHint">内部内容</param>
        /// <param name="verify">验证委托</param>
        /// <param name="isVisCloseBut">是否显示关闭按钮</param>
        /// <param name="owner">窗体所有者</param>
        /// <returns>输入的内容（关闭为null）</returns>
        public static T? Show<T>(string content, string title, string contentHint = "请输入值", Func<T, string> verify = null, bool isVisCloseBut = true, Window owner = null) where T : struct
        {
            TipInputView? tipView;
            T? rval = null;
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                tipView = new TipInputView();
                tipView.tb1.KeyDown += (s, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        e.Handled = true;
                        tipView.qr.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    }
                };
                tipView.qr.Click += (sender, e) =>
                {
                    if (string.IsNullOrWhiteSpace(tipView.tb1.Text))
                    {
                        tipView.TsTxtx("请填写文本。");
                        return;
                    }

                    T val;
                    try
                    {
                        val = (T)Convert.ChangeType(tipView.tb1.Text, typeof(T));
                    }
                    catch (Exception)
                    {
                        tipView.TsTxtx($"无法将值{tipView.tb1.Text}换为{typeof(T).Name}。");
                        return;
                    }

                    var ts = verify?.Invoke(val);
                    if (!string.IsNullOrEmpty(ts))
                    {
                        tipView.TsTxtx(ts);
                        return;
                    }
                    rval = val;
                    tipView.Close();
                };
                tipView.DataContext = new { Title = title, Content = content, ContentHint = contentHint, IsClose = isVisCloseBut };

                tipView.Owner = owner == null ? System.Windows.Application.Current.MainWindow : owner;
                tipView.tb1.Focus();
                tipView.ShowDialog();
                tipView = null;
            }));

            return rval;
        }

        /// <summary>
        /// 显示输入提示框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="verify">验证委托</param>
        /// <param name="isVisCloseBut">是否显示关闭按钮</param>
        /// <param name="owner">窗体所有者</param>
        /// <returns>输入的文本（关闭为null）</returns>
        public static string Show(string content, Func<string, string> verify = null, bool isVisCloseBut = true, Window owner = null)
        {
            return Show(content, string.Empty, "请输入值", verify, isVisCloseBut, owner);
        }

        /// <summary>
        /// 显示输入提示框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="title">标题</param>
        /// <param name="contentHint">内部内容</param>
        /// <param name="verify">验证委托</param>
        /// <param name="isVisCloseBut">是否显示关闭按钮</param>
        /// <param name="owner">窗体所有者</param>
        /// <returns>输入的文本（关闭为null）</returns>
        public static string Show(string content, string title, string contentHint = "请输入值", Func<string, string> verify = null, bool isVisCloseBut = true, Window owner = null)
        {
            TipInputView? tipView;
            string? rval = null;
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                tipView = new TipInputView();
                tipView.tb1.KeyDown += (s, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        e.Handled = true;
                        tipView.qr.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    }
                };
                tipView.qr.Click += (sender, e) =>
                {
                    if (string.IsNullOrWhiteSpace(tipView.tb1.Text))
                    {
                        tipView.TsTxtx("请填写文本。");
                        return;
                    }

                    var ts = verify?.Invoke(tipView.tb1.Text);
                    if (!string.IsNullOrEmpty(ts))
                    {
                        tipView.TsTxtx(ts);
                        return;
                    }
                    rval = tipView.tb1.Text;
                    tipView.Close();
                };
                tipView.DataContext = new { Title = title, Content = content, ContentHint = contentHint, IsClose = isVisCloseBut };

                tipView.Owner = owner == null ? System.Windows.Application.Current.MainWindow : owner;
                tipView.tb1.Focus();
                tipView.ShowDialog();
                tipView = null;
            }));

            return rval!;
        }

        private void TsTxtx(string text)
        {
            transit.Visibility = Visibility.Hidden;
            textBlock.Text = text;
            transit.Visibility = Visibility.Visible;
        }

        private void previewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
