﻿﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using Hardcodet.Wpf.TaskbarNotification;
using NotifierReborn.Gui.Windows.Controls;

namespace NotifierReborn.Gui.Windows
{
    /// <summary>
    /// Provides a taskbar icon with support for tooltips, context menus, and toasts.
    /// </summary>
    public class NotifierTaskbarIcon : TaskbarIcon
    {
        #region Toast

        /// <summary>
        /// Identifies the <see cref="Toast" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty ToastProperty = DependencyProperty.Register(
            "Toast",
            typeof(object),
            typeof(NotifierTaskbarIcon),
            new PropertyMetadata(OnToastChanged));

        private static void OnToastChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = (NotifierTaskbarIcon)d;
            obj.OnToastChanged((object)e.OldValue, (object)e.NewValue);
        }

        private void OnToastChanged(object oldValue, object newValue)
        {
            if (oldValue == newValue)
                return;

            Dispatcher.Invoke((Action)delegate
            {
                var toast = new Toast(newValue, ToastDuration);
                ShowCustomBalloon(toast, PopupAnimation.None, null);
            });
        }

        /// <summary>
        /// The content to display in a toast.
        /// </summary>
        [Description("The content to display in a toast.")]
        [Category("Common")]
        public object Toast
        {
            get { return (object)GetValue(ToastProperty); }
            set { SetValue(ToastProperty, value); }
        }

        #endregion

        #region ToastDuration

        /// <summary>
        /// Identifies the <see cref="ToastDuration" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty ToastDurationProperty = DependencyProperty.Register(
            "ToastDuration",
            typeof(TimeSpan),
            typeof(NotifierTaskbarIcon));

        /// <summary>
        /// The duration that a toast is shown for.
        /// </summary>
        [Description("The duration that a toast is shown for.")]
        [Category("Common")]
        public TimeSpan ToastDuration
        {
            get { return (TimeSpan)GetValue(ToastDurationProperty); }
            set { SetValue(ToastDurationProperty, value); }
        }

        #endregion
    }
}
