using _2025._01._30;
using System.IO;
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;
using static System.Reflection.Metadata.BlobBuilder;

namespace _2025._02._12;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<Book> books = Konyvek();

        foreach (Book book in books)
        {
            KonyvekListView.Items.Add(book.Cim);
        }
    }

    public List<Book> Konyvek()
    {
        List<Book> books = new List<Book>();
        StreamReader sr = new StreamReader("konyvek.txt");
        string line = sr.ReadLine();
        while (line != null)
        {
            string[] line_splitted = line.Split(";");
            Book book = new Book(line_splitted[0], line_splitted[1], int.Parse(line_splitted[2]), int.Parse(line_splitted[3]));
            books.Add(book);
            line = sr.ReadLine();
        }
        sr.Close();

        return books;
    }

    private void TorlesButton_Click(object sender, RoutedEventArgs e)
    {
        List<Book> books = Konyvek();

        foreach (Book book in books)
        {
            if (book.Cim == KonyvekListView.SelectedValue.ToString())
            {
                books.Remove(book);
                KonyvekListView.Items.RemoveAt(KonyvekListView.SelectedIndex);
                break;
            }
        }

        FileStream fs = new FileStream("konyvek.txt", FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        foreach (Book book in books)
        {
            sw.WriteLine($"{book.Cim};{book.Szerzo};{book.Kiadas_eve};{book.Darabszam}");
        }

        sw.Close();
        fs.Close();
    }

    private void ModositasButton_Click(object sender, RoutedEventArgs e)
    {

    }

    private void KonyvekListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        List<Book> books = Konyvek();

        foreach (Book book in books)
        {
            return;
        }
    }
}