using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.ComponentModel;
using GalaSoft.MvvmLight.Command;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// Window_Tip.xaml 的交互逻辑
    /// </summary>
    public partial class Window_Tip : Window
    {
        WdTipVm viewModel;
        public Window_Tip()
        {
            InitializeComponent();
        }
        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);
        }
    }
    public class WdTipVm:INotifyPropertyChanged
    {
        private static Window_Tip Current=null;

        public TimeSpan Duration { get; set; }
        public string Message { get; set; }
        public string Title { get; set; }

        public RelayCommand LoadedCmd { get; private set; }
        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);
        }
    }
}
