using InABox.Wpf;
using System.Windows;

namespace InABox.WPF
{
    /// <summary>
    ///     Interaction logic for NumberEdit.xaml
    /// </summary>
    public partial class NumberEdit : ThemableWindow
    {
        public NumberEdit(string title, int min, int max, int value)
        {
            InitializeComponent();
            Title = title;
            Value = value;
            Editor.Minimum = min;
            Editor.Maximum = max;
        }

        public int Value
        {
            get => Editor.Value.HasValue ? Editor.Value.Value : 0;
            set => Editor.Value = value;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if(Editor.Value >= Editor.Minimum && Editor.Value <= Editor.Maximum)
            {
                DialogResult = true;
                Close();
            }
            else
            {
                MessageBox.Show($"Value must be in range [{Editor.Minimum}, {Editor.Maximum}]");
            }
        }

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

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

            return false;
        }
    }
}