using System.Windows;

namespace dealer
{
    public partial class EditOwnerWindow : Window
    {
        private Owner owner;
        private DataManager dataManager;

        public EditOwnerWindow(Owner owner, DataManager dataManager)
        {
            InitializeComponent();
            this.owner = owner;
            this.dataManager = dataManager;
            LoadOwnerData();
        }

        private void LoadOwnerData()
        {
            JmenoTextBox.Text = owner.Jmeno ?? "";
            PrijmeniTextBox.Text = owner.Prijmeni ?? "";
            TelefonTextBox.Text = owner.Telefon ?? "";
            EmailTextBox.Text = owner.Email ?? "";
            AdresaTextBox.Text = owner.Adresa ?? "";
        }

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

            if (string.IsNullOrWhiteSpace(PrijmeniTextBox.Text))
            {
                System.Windows.MessageBox.Show("Vyplnte prijmeni.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (string.IsNullOrWhiteSpace(TelefonTextBox.Text))
            {
                System.Windows.MessageBox.Show("Vyplnte telefon.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (string.IsNullOrWhiteSpace(EmailTextBox.Text))
            {
                System.Windows.MessageBox.Show("Vyplnte email.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (string.IsNullOrWhiteSpace(AdresaTextBox.Text))
            {
                System.Windows.MessageBox.Show("Vyplnte adresu.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            owner.Jmeno = JmenoTextBox.Text.Trim();
            owner.Prijmeni = PrijmeniTextBox.Text.Trim();
            owner.Telefon = TelefonTextBox.Text.Trim();
            owner.Email = EmailTextBox.Text.Trim();
            owner.Adresa = AdresaTextBox.Text.Trim();

            dataManager.Save();
            this.Close();
        }

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