using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

using System.Windows;

namespace Wpf_TOMBOLA
{
    public partial class MainWindow : Window
    {
        private Tabellone tabellone;
        private List<TextBlock> celle = new List<TextBlock>();

        private void CreaTabellone()
        {
            gridTabellone.Children.Clear();
            celle.Clear();

            for (int i = 1; i <= 90; i++)
            {
                TextBlock tb = new TextBlock
                {
                    Text = i.ToString("D2"),
                    TextAlignment = TextAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Stretch,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Background = System.Windows.Media.Brushes.LightGray,
                    Margin = new Thickness(2)
                };

                celle.Add(tb);
                gridTabellone.Children.Add(tb);
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            tabellone = new Tabellone();
            CreaTabellone();
            AggiornaUI();
        }

        private void Estrai_Click(object sender, RoutedEventArgs e)
        {
            int numero = tabellone.Estrai();

            txtPrecedente1.Text = txtPrecedente.Text;
            txtPrecedente.Text = txtEstratto.Text;
            txtEstratto.Text = numero.ToString();

            AggiornaUI();

            if (tabellone.Completato())
            {
                txtPrecedente1.Text = "";
                txtPrecedente.Text = "";
                txtEstratto.Text = "";
                btnEstrai.IsEnabled = false;
                MessageBox.Show("Tutti i numeri sono stati estratti!");
                return;
            }
        }

        private void Nuovo_Click(object sender, RoutedEventArgs e)
        {
            tabellone.Nuovo();
            AggiornaUI();
        }

        private void AggiornaUI()
        {
            for (int i = 0; i < celle.Count; i++)
            {
                int numero = i + 1;

                if (tabellone.EsisteNumero(numero))
                {
                    celle[i].Background = System.Windows.Media.Brushes.LightGreen;
                }
                else
                {
                    celle[i].Background = System.Windows.Media.Brushes.LightGray;
                }
            }
        }
    }
}
