using InABox.Wpf;
using System;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace InABox.WPF
{
    /// <summary>
    ///     Interaction logic for DateEdit.xaml
    /// </summary>
    public partial class DateEdit : ThemableWindow
    {
        public DateEdit(string title, DateTime value)
        {
            InitializeComponent();
            Title = title;
            Value = value;
        }

        public DateTime Value
        {
            get => Calendar.SelectedDate ?? DateTime.MinValue;
            set => Calendar.SelectedDate = value;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        public static bool Execute(string title, ref DateTime value)
        {
            var edit = new DateEdit(title, value);
            if (edit.ShowDialog() == true)
            {
                value = edit.Value;
                return true;
            }

            return false;
        }

        private void Calendar_GotMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var originalElement = e.OriginalSource as UIElement;
            if(originalElement is CalendarDayButton || originalElement is CalendarItem)
            {
                originalElement.ReleaseMouseCapture();
            }
        }
    }
}