using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using System.Collections.Generic;
using System;

namespace AquaCare.View
{
    public partial class TutorialsWindow : Window
    {
        private List<string> _images;
        private TextBlock? _imageIndexTextBlock;
        private TextBlock? _initialText;
        private int _currentImageIndex;

        private readonly Dictionary<string, int> _imageCountPerFolder = new()
        {
            {"Fische füttern", 3},
            {"Wasser wechseln", 7},
            {"Filter reinigen", 8}
        };

        public TutorialsWindow()
        {
            InitializeComponent();
            _images = new List<string>();
            
            _imageIndexTextBlock = this.Find<TextBlock>("imageIndexTextBlock") 
                ?? throw new InvalidOperationException("imageIndexTextBlock not found");
            _initialText = this.Find<TextBlock>("initialText")
                ?? throw new InvalidOperationException("initialText not found");
        }

        private void LoadTutorialImages(string folderName)
        {
            if (_initialText == null || tutorialImage == null) return;
            if (!_imageCountPerFolder.ContainsKey(folderName)) return;

            int imageCount = _imageCountPerFolder[folderName];
            _images = new List<string>();
            
            // Füge dynamisch die richtige Anzahl Bilder hinzu
            for (int i = 1; i <= imageCount; i++)
            {
                _images.Add($"Ressourcen/BilderTutorials/{folderName}/{i}.jpg");
            }

            _currentImageIndex = 0;
            _initialText.IsVisible = false;
            tutorialImage.IsVisible = true;
            UpdateTutorialContent();
        }

        private void UpdateTutorialContent()
        {
            try 
            {
                if (_imageIndexTextBlock == null || _initialText == null || tutorialImage == null) return;

                if (_images.Count > 0 && _currentImageIndex >= 0 && _currentImageIndex < _images.Count)
                {
                    string fullPath = $"avares://AquaCare.View/{_images[_currentImageIndex]}";
                    System.Diagnostics.Debug.WriteLine($"Lade Bild: {fullPath}");  // Debug-Ausgabe
                    
                    var uri = new Uri(fullPath);
                    var asset = Avalonia.Platform.AssetLoader.Open(uri);
                    tutorialImage.Source = new Bitmap(asset);
                    
                    _imageIndexTextBlock.Text = $"Bild {_currentImageIndex + 1} von {_images.Count}";
                    _initialText.IsVisible = false;
                    tutorialImage.IsVisible = true;
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Fehler beim Laden des Bildes: {ex.Message}");
            }
        }

        private void NavigateImages(bool forward)
        {
            if (forward)
            {
                _currentImageIndex = _currentImageIndex < _images.Count - 1 ? _currentImageIndex + 1 : 0;
            }
            else
            {
                _currentImageIndex = _currentImageIndex > 0 ? _currentImageIndex - 1 : _images.Count - 1;
            }
            UpdateTutorialContent();
        }

        // Event Handler
        private void FischeFütternClick(object? sender, RoutedEventArgs e) 
            => LoadTutorialImages("Fische füttern");

        private void WasserWechselnClick(object? sender, RoutedEventArgs e) 
            => LoadTutorialImages("Wasser wechseln");

        private void FilterReinigenClick(object? sender, RoutedEventArgs e) 
            => LoadTutorialImages("Filter reinigen");

        private void NextImageClick(object? sender, RoutedEventArgs e) 
            => NavigateImages(true);

        private void PreviousImageClick(object? sender, RoutedEventArgs e) 
            => NavigateImages(false);

        private void CloseClick(object? sender, RoutedEventArgs e) 
            => Close();
    }
}
