using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace dealer
{
    public partial class VehicleDetailWindow : Window
    {
        private Vehicle vehicle;
        private DataManager dataManager;

        public bool WasDeleted { get; private set; } = false;
        public bool WasEdited { get; private set; } = false;

        public VehicleDetailWindow(Vehicle vehicle, DataManager dataManager)
        {
            InitializeComponent();

            this.vehicle = vehicle;
            this.dataManager = dataManager;

            LoadVehicleDetails();
        }

        private void LoadVehicleDetails()
        {
            // SPZ
            SpzTextBlock.Text = vehicle.Spz ?? "Nezadano";

            // Znacka
            ZnackaTextBlock.Text = vehicle.Znacka ?? "Nezadano";

            // Model
            ModelTextBlock.Text = vehicle.Model ?? "Nezadano";

            // Barva
            ColorTextBlock.Text = vehicle.Barva ?? "Nezadano";
            if (!string.IsNullOrEmpty(vehicle.Barva))
            {
                try
                {
                    var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(vehicle.Barva);
                    ColorPreviewBorder.Background = new SolidColorBrush(color);
                }
                catch
                {
                    ColorPreviewBorder.Background = System.Windows.Media.Brushes.White;
                }
            }

            // Rok vyroby
            YearTextBlock.Text = vehicle.RokVyroby?.ToString() ?? "Nezadano";

            // Palivo
            PalivoTextBlock.Text = vehicle.Palivo ?? "Nezadano";

            // Najete km
            if (vehicle.NajeteKm != null)
                MileageTextBlock.Text = vehicle.NajeteKm.Value.ToString("N0") + " km";
            else
                MileageTextBlock.Text = "Nezadano";

            // Cena
            if (vehicle.Cena != null)
                PriceTextBlock.Text = vehicle.Cena.Value.ToString("N0") + " Kc";
            else
                PriceTextBlock.Text = "Nezadano";

            // Vlastnik
            if (vehicle.Vlastnik != null)
            {
                OwnerNameTextBlock.Text = vehicle.Vlastnik.CeleJmeno;
                AddressTextBlock.Text = vehicle.Vlastnik.Adresa ?? "Nezadano";
                PhoneTextBlock.Text = vehicle.Vlastnik.Telefon ?? "Nezadano";
                EmailTextBlock.Text = vehicle.Vlastnik.Email ?? "Nezadano";
            }
            else
            {
                OwnerNameTextBlock.Text = "Nezadano";
                AddressTextBlock.Text = "Nezadano";
                PhoneTextBlock.Text = "Nezadano";
                EmailTextBlock.Text = "Nezadano";
            }

            // Fotografie (base64)
            if (vehicle.Fotografie != null && !string.IsNullOrEmpty(vehicle.Fotografie.Data))
            {
                try
                {
                    byte[] imageBytes = Convert.FromBase64String(vehicle.Fotografie.Data);
                    using (var ms = new MemoryStream(imageBytes))
                    {
                        var bitmap = new BitmapImage();
                        bitmap.BeginInit();
                        bitmap.CacheOption = BitmapCacheOption.OnLoad;
                        bitmap.StreamSource = ms;
                        bitmap.EndInit();
                        bitmap.Freeze();
                        VehicleImage.Source = bitmap;
                    }
                    VehicleImage.Visibility = Visibility.Visible;
                    NoPhotoTextBlock.Visibility = Visibility.Collapsed;
                }
                catch
                {
                    VehicleImage.Visibility = Visibility.Collapsed;
                    NoPhotoTextBlock.Visibility = Visibility.Visible;
                }
            }
            else
            {
                VehicleImage.Visibility = Visibility.Collapsed;
                NoPhotoTextBlock.Visibility = Visibility.Visible;
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var result = System.Windows.MessageBox.Show(
                $"Opravdu chcete smazat vozidlo {vehicle.Model} (SPZ: {vehicle.Spz})?",
                "Potvrzeni smazani",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                dataManager.RemoveVehicle(vehicle);
                dataManager.Save();
                WasDeleted = true;
                this.Close();
            }
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var editWindow = new EditVehicleWindow(vehicle, dataManager);
            editWindow.ShowDialog();

            if (editWindow.WasEdited)
            {
                WasEdited = true;
                LoadVehicleDetails();
            }
        }

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

