using System.Windows;

namespace dealer
{
    public partial class AddOwnerWindow : Window
    {
        private DataManager dataManager;

        /// <summary>
        /// Gets the owner that was created when the user saved, or null if cancelled.
        /// </summary>
        public Owner? CreatedOwner { get; private set; }

        public AddOwnerWindow(DataManager dataManager)
        {
            InitializeComponent();
            this.dataManager = dataManager;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(JmenoTextBox.Text) && string.IsNullOrWhiteSpace(PrijmeniTextBox.Text))
            {
                System.Windows.MessageBox.Show("Vyplnte alespon jmeno nebo prijmeni.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var owner = new Owner
            {
                Jmeno = JmenoTextBox.Text.Trim(),
                Prijmeni = PrijmeniTextBox.Text.Trim(),
                Telefon = TelefonTextBox.Text.Trim(),
                Email = EmailTextBox.Text.Trim(),
                Adresa = AdresaTextBox.Text.Trim()
            };

            dataManager.AddOwner(owner);
            dataManager.Save();
            CreatedOwner = owner;
            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
