using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dealer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DataManager dataManager = new DataManager();

        public MainWindow()
        {
            InitializeComponent();

            this.dataManager.Load();

            this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var window = new AddVehicleWindow(this.dataManager);
            window.ShowDialog();

            this.VehiclesListView.ItemsSource = null;
            this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedVehicle = this.VehiclesListView.SelectedItem as Vehicle;

            if (selectedVehicle == null)
            {
                System.Windows.MessageBox.Show("Vyberte vozidlo, ktere chcete upravit.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var editWindow = new EditVehicleWindow(selectedVehicle, this.dataManager);
            editWindow.ShowDialog();

            if (editWindow.WasEdited)
            {
                this.VehiclesListView.ItemsSource = null;
                this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
            }
        }

        private void VehiclesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var selectedVehicle = this.VehiclesListView.SelectedItem as Vehicle;

            if (selectedVehicle == null)
                return;

            var detailWindow = new VehicleDetailWindow(selectedVehicle, this.dataManager);
            detailWindow.ShowDialog();

            if (detailWindow.WasDeleted || detailWindow.WasEdited)
            {
                this.VehiclesListView.ItemsSource = null;
                this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
            }
        }

        private void DetailButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedVehicle = this.VehiclesListView.SelectedItem as Vehicle;

            if (selectedVehicle == null)
            {
                System.Windows.MessageBox.Show("Vyberte vozidlo, ktere chcete zobrazit.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var detailWindow = new VehicleDetailWindow(selectedVehicle, this.dataManager);
            detailWindow.ShowDialog();

            if (detailWindow.WasDeleted || detailWindow.WasEdited)
            {
                this.VehiclesListView.ItemsSource = null;
                this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedVehicle = this.VehiclesListView.SelectedItem as Vehicle;

            if (selectedVehicle == null)
            {
                System.Windows.MessageBox.Show("Vyberte vozidlo, ktere chcete smazat.", "Upozorneni", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var result = System.Windows.MessageBox.Show(
                $"Opravdu chcete smazat vozidlo {selectedVehicle.Model} (SPZ: {selectedVehicle.Spz})?",
                "Potvrzeni smazani",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                this.dataManager.RemoveVehicle(selectedVehicle);
                this.dataManager.Save();
                this.VehiclesListView.ItemsSource = null;
                this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
            }
        }

        private void OwnersButton_Click(object sender, RoutedEventArgs e)
        {
            var ownersWindow = new OwnersWindow(this.dataManager);
            ownersWindow.Owner = this;
            ownersWindow.ShowDialog();
            
            // Refresh vehicles list to update owner references
            this.VehiclesListView.ItemsSource = null;
            this.VehiclesListView.ItemsSource = this.dataManager.GetAllVehicles();
        }
    }
}