using Elwig.Helpers;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace Elwig.Dialogs {
    public partial class ManualWeighingDialog : Window {

        public int Weight = 0;
        public string? Reason = null;

        public ManualWeighingDialog() {
            InitializeComponent();
        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs evt) {
            DialogResult = true;
            Weight = int.Parse(WeightInput.Text);
            Reason = Regex.Replace(ReasonInput.Text, @"\s+", "").Trim();
            if (Reason == "") {
                Reason = null;
            } else if (!Reason.EndsWith(".") || !Reason.EndsWith("!") || !Reason.EndsWith("?")) {
                Reason += ".";
            }
            Close();
        }

        private void UpdateButtons() {
            ConfirmButton.IsEnabled = WeightInput.Text.Length > 0 && ReasonInput.Text.Trim().Length > 0;
        }

        private void WeightInput_TextChanged(object sender, TextChangedEventArgs evt) {
            Validator.CheckInteger(WeightInput, true, 5);
            UpdateButtons();
        }

        private void ReasonInput_TextChanged(object sender, TextChangedEventArgs evt) {
            UpdateButtons();
        }
    }
}