using CommunityToolkit.Mvvm.Input;
using System;
using System.ComponentModel;
using System.Windows;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// Window_Tip.xaml 的交互逻辑
    /// </summary>
    public partial class Window_Tip : Window
    {
        WdTipVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        public Window_Tip()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="duration"></param>
        public void Init(string title, string message, TimeSpan duration)
        {
            if (viewModel == null)
            {
                viewModel = new WdTipVm();
                DataContext = viewModel;
            }
            viewModel.Init(title, message, duration);

        }
        /// <summary>
        /// 右下角 提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">持续时间</param>
        public static void Show(string title, string message, TimeSpan duration)
        {
            WdTipVm.Show(title, message, duration);
        }
        /// <summary>
        /// 右下角 短时间提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        public static void ShowShortTime(string title, string message)
        {
            Show(title, message, TimeSpan.FromSeconds(2));
        }
        /// <summary>
        /// 右下角 永远显示提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        public static void Show(string title, string message)
        {
            Show(title, message, TimeSpan.MaxValue);

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.LoadedCmd.Execute(null);
        }
    }
    class WdTipVm : INotifyPropertyChanged
    {
        private static Window_Tip Current = null;

        /// <summary>
        /// 持续时间
        /// </summary>
        public TimeSpan Duration { get; set; }
        /// <summary>
        /// 提示内容
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 加载事件
        /// </summary>

        public RelayCommand LoadedCmd { get; private set; }
        /// <summary>
        /// 关闭事件
        /// </summary>
        public RelayCommand CloseCmd { get; private set; }
        public event PropertyChangedEventHandler PropertyChanged;


        System.Windows.Threading.DispatcherTimer timer;

        public WdTipVm()
        {
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            InitCmd();
        }
        public void Init(string title, string message, TimeSpan duration)
        {
            Title = title;
            Message = message;
            Duration = duration;

            if (!Current.IsVisible)
                Current.Show();

            if (duration < TimeSpan.MaxValue && duration > TimeSpan.MinValue)
            {
                timer.Interval = duration;
                timer.Start();
            }
            else
            {
                if (timer.IsEnabled)
                    timer.Stop();
            }

        }
        void InitCmd()
        {
            LoadedCmd = new RelayCommand(() =>
            {
                if (Current == null)
                    return;//不正常
                Current.Left = SystemParameters.PrimaryScreenWidth - Current.Width;
                Current.Top = SystemParameters.PrimaryScreenHeight - Current.Height - 100;
                if (Duration < TimeSpan.MaxValue && Duration > TimeSpan.MinValue)
                {
                    timer.Interval = Duration;
                    timer.Start();
                }
            });

            CloseCmd = new RelayCommand(() =>
            {
                if (Current == null)
                    return;//不正常
                timer_Tick(null, null);
            });
        }
        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            Current.Close();
            Current = null;
        }

        /// <summary>
        /// 右下角 提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">持续时间</param>
        public static void Show(string title, string message, TimeSpan duration)
        {
            if (Current == null)
                Current = new Window_Tip();

            Current.Init(title, message, duration);
        }
    }
}
