using System.Windows;
using System.Windows.Input;

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

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

        private void RefreshOwnersList()
        {
            string searchText = SearchTextBox?.Text ?? "";
            OwnersListView.ItemsSource = null;
            OwnersListView.ItemsSource = dataManager.SearchOwners(searchText);
        }

        private void SearchTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            RefreshOwnersList();
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var addWindow = new AddOwnerWindow(dataManager);
            addWindow.Owner = this;
            addWindow.ShowDialog();
            RefreshOwnersList();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedOwner = OwnersListView.SelectedItem as Owner;
            if (selectedOwner == null)
            {
                System.Windows.MessageBox.Show("Vyberte vlastnika, ktereho chcete upravit.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var editWindow = new EditOwnerWindow(selectedOwner, dataManager);
            editWindow.Owner = this;
            editWindow.ShowDialog();
            RefreshOwnersList();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedOwner = OwnersListView.SelectedItem as Owner;
            if (selectedOwner == null)
            {
                System.Windows.MessageBox.Show("Vyberte vlastnika, ktereho chcete smazat.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var result = System.Windows.MessageBox.Show(
                $"Opravdu chcete smazat vlastnika {selectedOwner.CeleJmeno}?\n\nVozidla tohoto vlastnika zustanou, ale nebudou mit prirazeneho vlastnika.",
                "Potvrzeni smazani",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                dataManager.RemoveOwner(selectedOwner);
                dataManager.Save();
                RefreshOwnersList();
            }
        }

        private void OwnersListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var selectedOwner = OwnersListView.SelectedItem as Owner;
            if (selectedOwner == null) return;

            var editWindow = new EditOwnerWindow(selectedOwner, dataManager);
            editWindow.Owner = this;
            editWindow.ShowDialog();
            RefreshOwnersList();
        }

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