using MahApps.Metro.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für BEImage.xaml
    /// </summary>
    public partial class BEImage : MetroWindow
    {
        ObservableCollection<ComboBoxItem> list = new ObservableCollection<ComboBoxItem>();
        public BEImage(int imageIndex = 99)
        {
            InitializeComponent();

            FillBEList();


            if (imageIndex != 99)
                cbBE.SelectedIndex = imageIndex;
        }
        private void FillBEList()
        {
            var desc = Helper.GetBEDescription();
            var numbers = Helper.GetBENumbers();
            var be = new List<string>();
            var beNameForPics = new List<string>();
            be.Add("");

            for (int i = 0; i < numbers.Count; i++)
            {
                be.Add($"BE-Fläche {numbers[i]} ({desc[i]})");
                beNameForPics.Add($"BE-Fläche {numbers[i]}\n({desc[i]})");
                var sp = new StackPanel();
                var img = new Image();
                //img.Width = 220;
                //img.Height = 150;
                img.Margin = new Thickness(5,10,5,10);
                img.Source = new BitmapImage(new Uri($"{Helper.GetBEFileNames(be.Last().Split(' ')[1])}", UriKind.Relative));
                img.MouseLeftButtonUp += Image_Click;
                img.ToolTip = "Klicken zum vergrößern";
                ToolTipService.SetInitialShowDelay(img, 200);
                sp.Children.Add(img);
                sp.Children.Add(new TextBlock() { Text = beNameForPics.Last(), TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Top });
                imgBE.Children.Add(sp);
            }

            cbBE.ItemsSource = be;
        }

        private void Image_Click(object sender, MouseButtonEventArgs e)
        {
            imgBESingle.Source = new BitmapImage(new Uri((sender as Image).Source.ToString()));
            imgBorder.Visibility = Visibility.Visible;
        }

        private void imgBESingle_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            imgBorder.Visibility = Visibility.Collapsed;
        }

        private void imgBESingle_LostMouseCapture(object sender, MouseEventArgs e)
        {
            imgBorder.Visibility = Visibility.Collapsed;
        }



        private void cbBE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var combo = (sender as ComboBox).SelectedItem.ToString();

            if (combo.Contains(' '))
            {
                if ((sender as ComboBox).SelectedIndex != 0 && (sender as ComboBox).SelectedIndex != -1)
                {
                    imgBESingle.Source = new BitmapImage(new Uri($"{Helper.GetBEFileNames(combo.Split(' ')[1])}", UriKind.Relative));
                    imgBorder.Visibility = Visibility.Visible;
                }
            }
        }

        public void changePicture(int selectedPicture)
        {
            cbBE.SelectedIndex = selectedPicture;
        }



        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }
    }
}
