using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace BPASmartClient.CustomResource.UserControls
{
    /// <summary>
    /// Notifiaction.xaml 的交互逻辑
    /// </summary>
    public partial class Notifiaction :Window
    {
        private const byte MAX_NOTIFICATIONS = 3;
        private readonly ObservableCollection<NotifiactionModel> buffer = new ObservableCollection<NotifiactionModel>();
        private ObservableCollection<NotifiactionModel> NotifiactionList = new ObservableCollection<NotifiactionModel>();
        private const double topOffset = 40;
        private const double leftOffset = 350;
        public DispatcherTimer uptimer = null;
        public Notifiaction()
        {
            InitializeComponent();
            this.NotificationsControl.DataContext = this.NotifiactionList;
            this.Top = 130;
            this.Left = SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width - this.Width;
            this.Height = SystemParameters.WorkArea.Height - 460;
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
            if (uptimer == null)
                uptimer = new DispatcherTimer();
            uptimer.Tick += new EventHandler(_timer_Tick);
            uptimer.Interval = TimeSpan.FromSeconds(1);
            uptimer.Start();
        }

        #region 显示控制
        /// <summary>
        /// 线程消失函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _timer_Tick(object sender,EventArgs e)
        {
            NotifiactionList.ToList().ForEach(par =>
            {
                if (par.time > 0)
                    par.time = par.time - 1;
            });
            List<NotifiactionModel> Notifia = NotifiactionList.ToList().FindAll(x => x.time <= 0);
            foreach (var item in Notifia)
            {
                RemoveNotification(item);
            }
        }
        /// <summary>
        /// 清除窗体显示函数
        /// </summary>
        /// <param name="type"></param>
        public void Clear(EnumPromptType type)
        {
            List<NotifiactionModel> buff = buffer.ToList().FindAll(x => x.NotifiactionType == type);
            foreach (var item in buff)
            {
                buffer.Remove(item);
            }
            List<NotifiactionModel> Notifia = NotifiactionList.ToList().FindAll(x => x.NotifiactionType == type);
            foreach (var item in Notifia)
            {
                NotifiactionList.Remove(item);
            }
        }
        /// <summary>
        /// 增加一个消息弹框
        /// </summary>
        /// <param name="notification"></param>
        public void AddNotifiaction(NotifiactionModel notification)
        {
            try
            {
                var window = Window.GetWindow(notification.window);
                var intPtr = new WindowInteropHelper(window).Handle;
                var screen = System.Windows.Forms.Screen.FromHandle(intPtr);
                this.Left = screen.WorkingArea.Left + screen.WorkingArea.Width / 2 - this.Width / 2;// screen.WorkingArea.Left + screen.WorkingArea.Width - this.Width;

                WindowInteropHelper helper = new WindowInteropHelper(this);
                helper.Owner = new WindowInteropHelper(notification.window).Handle;
                this.Topmost = false;
                if (notification.window != null)
                {
                    if (notification.window.WindowState == WindowState.Minimized)
                    {
                        this.WindowState = WindowState.Minimized;
                    }
                    else
                    {
                        this.WindowState = WindowState.Normal;
                    }
                }

                if (NotifiactionList.Count + 1 > MAX_NOTIFICATIONS)//且数量超过了限制
                {
                    buffer.Add(notification);
                }
                else//已经显示的没找到，数量没有超过限制
                {
                    NotifiactionList.Add(notification);
                }

                if (NotifiactionList.Count > 0 && !IsActive)
                    this.Show();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 将显示窗体移除
        /// </summary>
        /// <param name="notification"></param>
        public void RemoveNotification(NotifiactionModel notification)
        {
            if (notification == null)
                return;

            if (NotifiactionList.Contains(notification))
                NotifiactionList.Remove(notification);

            if (buffer.Count > 0)
            {
                if (notification.NotifiactionType == EnumPromptType.Warn)
                {
                    NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info);
                    if (mode == null)
                    {
                        NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success);
                        if (mode1 == null)
                        {
                            NotifiactionList.Add(buffer[0]);
                            buffer.RemoveAt(0);
                        }
                        else
                        {
                            NotifiactionList.Add(mode1);
                            buffer.Remove(mode);
                        }
                    }
                    else
                    {
                        NotifiactionList.Add(mode);
                        buffer.Remove(mode);
                    }
                }
                else if (notification.NotifiactionType == EnumPromptType.Info)
                {
                    NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success);
                    if (mode == null)
                    {
                        NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn);
                        if (mode1 == null)
                        {
                            NotifiactionList.Add(buffer[0]);
                            buffer.RemoveAt(0);
                        }
                        else
                        {
                            NotifiactionList.Add(mode1);
                            buffer.Remove(mode);
                        }
                    }
                    else
                    {
                        NotifiactionList.Add(mode);
                        buffer.Remove(mode);
                    }
                }
                else if (notification.NotifiactionType == EnumPromptType.Success)
                {
                    NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn);
                    if (mode == null)
                    {
                        NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info);
                        if (mode1 == null)
                        {
                            NotifiactionList.Add(buffer[0]);
                            buffer.RemoveAt(0);
                        }
                        else
                        {
                            NotifiactionList.Add(mode1);
                            buffer.Remove(mode);
                        }
                    }
                    else
                    {
                        NotifiactionList.Add(mode);
                        buffer.Remove(mode);
                    }
                }
                else
                {
                    NotifiactionList.Add(buffer[0]);
                    buffer.RemoveAt(0);
                }
            }

            //Close window if there's nothing to show
            if (NotifiactionList.Count < 1)
                Hide();
        }

        private void NoticeGrid_SizeChanged(object sender,SizeChangedEventArgs e)
        {
           
        }
        #endregion

        #region 窗体事件

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PART_CloseButton_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
        {
            Image border = (Image)sender;
            NotifiactionModel mode = border.DataContext as NotifiactionModel;
            RemoveNotification(mode);
        }
        #endregion
    }

    public class NotifiactionModel :INotifyPropertyChanged
    {
        private int _count;
        private string _content;
        private int _time;
        /// <summary>
        /// Id不需要赋值
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 通知标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 通知内容
        /// </summary>
        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this,new PropertyChangedEventArgs("Content"));
                }
            }
        }
        /// <summary>
        /// 通知内容-显示全体
        /// </summary>
        public string ContentToolTip { get; set; }
        /// <summary>
        /// 通知类型
        /// </summary>
        private EnumPromptType enumPromptType;
        public EnumPromptType NotifiactionType 
        {
            get { return enumPromptType; }
            set
            {
                enumPromptType = value;
                switch (enumPromptType)
                {
                    case EnumPromptType.Info:
                        color = Color.FromRgb(35,132,190);
                        break;
                    case EnumPromptType.Warn:
                        color = Color.FromRgb(255,170,22);
                        break;
                    case EnumPromptType.Error:
                        color = Color.FromRgb(245,49,49);
                        break;
                    case EnumPromptType.Success:
                        color = Color.FromRgb(28,194,59);
                        break;
                }
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this,new PropertyChangedEventArgs("enumPromptType"));
            }
        }
        /// <summary>
        /// 背景颜色主题
        /// </summary>
        public Color color { get; set; }
        /// <summary>
        /// 文本颜色
        /// </summary>
        public Color textColor { get; set; }
        /// <summary>
        /// 外部windows
        /// </summary>
        public System.Windows.Window window { get; set; }
        /// <summary>
        /// 弹窗显示时长
        /// </summary>
        public int time
        {
            get { return _time; }
            set
            {
                _time = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this,new PropertyChangedEventArgs("time"));
            }
        }
        public int count
        {
            get { return _count; }
            set
            {
                _count = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this,new PropertyChangedEventArgs("count"));
            }
        }//超过1显示数量
        /// <summary>
        /// 设置默认之
        /// </summary>
        public NotifiactionModel()
        {
            Id=Guid.NewGuid().ToString();
            time = 5;
            count = 1;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
