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 WpfClasseFile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GestoreStudenti gestore;

        public MainWindow()
        {
            gestore = new GestoreStudenti();

            gestore.CaricaStudentiDaFile("../../../alunni.txt");

            InitializeComponent();

            listIndirizzi.ItemsSource = gestore.Indirizzi();
        }

        private void listIndirizzi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listIndirizzi.SelectedItem == null)
                return;
            listClassi.ItemsSource = gestore.Classi(listIndirizzi.SelectedItem.ToString());
            listAlunni.ItemsSource = null;

            ResetInfo();
        }

        private void listClassi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listClassi.SelectedItem == null)
                return;

            listAlunni.ItemsSource = gestore.Studenti(listClassi.SelectedItem.ToString());

            ResetInfo();
        }

        private void listAlunni_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listAlunni.SelectedItem == null)
                return;

            Studente s = gestore.Studente(listAlunni.SelectedItem.ToString(), listIndirizzi.SelectedItem.ToString(), listClassi.SelectedItem.ToString());

            if (s == null)
                return;

            txtNomeCognome.Text = string.Join(" ", s.Nome, s.Cognome);
            txtClasse.Text = listClassi.SelectedItem.ToString();
            txtCittà.Text = s.Città;
            txtNascita.Text = s.DataNascita;
        }

        private void ResetInfo()
        {
            txtNomeCognome.Text = String.Empty;
            txtClasse.Text = String.Empty;
            txtCittà.Text = String.Empty;
            txtNascita.Text = String.Empty;
        }
    }
}