Lottorivit.xaml.cs 5.98 KB
Newer Older
M1888's avatar
M1888 committed
1 2
using Lottokone.Model;
using System;
M1888's avatar
M1888 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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.Shapes;

namespace Lottokone
{
    /// <summary>
    /// Interaction logic for Lottorivit.xaml
    /// </summary>
    public partial class Lottorivit : Window
    {
M1888's avatar
M1888 committed
23 24 25 26

        private Button btnSave;
        private Label lblInfo;

M1888's avatar
M1888 committed
27 28 29 30 31 32 33 34
        // Tähän listaan tehdään uusi lista jokaista lottoriviä varten.
        // Siihen listaan sitten lisätään nuo kyseisen rivin TextBoxit, jotta ne saa
        // käytyä myöhemmin järjestyksessä läpi, kun rivit tallentaa.
        private List<List<TextBox>> numerot;

        // Tähän haetaan kannasta tallennetut rivit, joilla täytetään kentät valmiiksi
        private List<Lottorivi> rivit;

M1888's avatar
M1888 committed
35 36 37 38 39 40
        public Lottorivit()
        {
            InitializeComponent();
            NaytaKontrollit();
        }

M1888's avatar
M1888 committed
41
        private void NaytaKontrollit()
M1888's avatar
M1888 committed
42
        {
M1888's avatar
M1888 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            this.Height = 80 + Properties.Settings.Default.Vakiorivit * 40;
            numerot = new List<List<TextBox>>();
            grdRivit.RowDefinitions.Add(new RowDefinition());
            btnSave = new Button();
            btnSave.Content = "Tallenna";
            btnSave.Margin = new Thickness(5);
            btnSave.Click += btnSave_Click;
            Grid.SetRow(btnSave, Properties.Settings.Default.Vakiorivit);
            Grid.SetColumnSpan(btnSave, 2);
            grdRivit.Children.Add(btnSave);

            grdRivit.RowDefinitions.Add(new RowDefinition());
            lblInfo = new Label();
            lblInfo.Style = Resources["Isompi"] as Style;
            lblInfo.Margin = new Thickness(5);
            Grid.SetRow(lblInfo, Properties.Settings.Default.Vakiorivit + 1);
            Grid.SetColumnSpan(lblInfo, 8);
            grdRivit.Children.Add(lblInfo);

            try
            {
                rivit = ViewModel.Riviapuri.Lataa(Properties.Settings.Default.Vakiorivit);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Virhe: {ex.Message}");
            }

            for (int i = 0; i < 8; i++)
                grdRivit.ColumnDefinitions.Add(new ColumnDefinition());
M1888's avatar
M1888 committed
73
            
M1888's avatar
M1888 committed
74 75
            for (int i = 0; i < Properties.Settings.Default.Vakiorivit; i++)
            {
M1888's avatar
M1888 committed
76 77 78
                numerot.Add(new List<TextBox>());
                grdRivit.RowDefinitions.Add(new RowDefinition());
                
M1888's avatar
M1888 committed
79
                Label l = new Label();
M1888's avatar
M1888 committed
80 81 82 83
                l.Content = $"Rivi {i+1}";
                Grid.SetRow(l, i);
                Grid.SetColumn(l, 0);
                grdRivit.Children.Add(l);
M1888's avatar
M1888 committed
84

M1888's avatar
M1888 committed
85 86 87 88
                for (int j = 0; j < 7; j++)
                {
                    TextBox tb = new TextBox();
                    tb.Style = Resources["Lottonumero"] as Style;
M1888's avatar
M1888 committed
89 90 91 92 93
                    tb.Width = 25;
                    tb.Height = 25;
                    tb.Margin = new Thickness(3, 0, 3, 0);
                    tb.VerticalAlignment = VerticalAlignment.Top;
                    tb.GotFocus += txbFocus;
M1888's avatar
M1888 committed
94
                    tb.TextChanged += txbChanged;
M1888's avatar
M1888 committed
95 96 97 98 99
                    tb.Text = rivit[i].Numerot[j].ToString();
                    Grid.SetRow(tb, i);
                    Grid.SetColumn(tb, j+1);
                    grdRivit.Children.Add(tb);
                    numerot[i].Add(tb);
M1888's avatar
M1888 committed
100 101
                }
            }
M1888's avatar
M1888 committed
102
        }
M1888's avatar
M1888 committed
103

M1888's avatar
M1888 committed
104 105 106 107 108 109 110
        private void txbFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if(tb != null)
            {
                tb.SelectAll();
            }
M1888's avatar
M1888 committed
111 112 113 114
        }

        public void btnSave_Click(object sender, RoutedEventArgs e)
        {
M1888's avatar
M1888 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            List<Rivi> rivit = new List<Rivi>();
            foreach(List<TextBox> rivi in numerot)
            {
                short[] tmp = new short[7];
                // tarvitaan myös indeksi, joten foreach ei käy
                for(int i = 0; i < 7; i++)
                {
                    if(short.TryParse(rivi[i].Text, out short s))
                    {
                        // tehdään vielä validaatio ennen tallennusta
                        if(s < 1 || s > 40)
                        {
                            lblInfo.Content = "Käytä vain numeroita 1-40";
                            return;
                        }
                        tmp[i] = s;
                    }
                    else
                    {
                        lblInfo.Content = "Käytä vain numeroita 1-40";
                        return;
                    }
                }
                rivit.Add(new Rivi(tmp));
            }
M1888's avatar
M1888 committed
140

M1888's avatar
M1888 committed
141 142 143 144 145 146 147 148 149
            try
            {
                ViewModel.Riviapuri.Tallenna(rivit);
                this.Close();
            }
            catch (Exception ex)
            {
                lblInfo.Content = $"Virhe tallennettaessa rivejä: {ex.Message}";
            }
M1888's avatar
M1888 committed
150 151 152 153 154
        }

        public void txbChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
M1888's avatar
M1888 committed
155
            if (tb != null)
M1888's avatar
M1888 committed
156
            {
M1888's avatar
M1888 committed
157 158
                bool ok = true;
                if (int.TryParse(tb.Text, out int result))
M1888's avatar
M1888 committed
159
                {
M1888's avatar
M1888 committed
160
                    if (result < 1)
M1888's avatar
M1888 committed
161
                    {
M1888's avatar
M1888 committed
162
                        ok = false;
M1888's avatar
M1888 committed
163 164 165
                    }
                    else if (result > 40)
                    {
M1888's avatar
M1888 committed
166
                        ok = false;
M1888's avatar
M1888 committed
167 168 169 170
                    }
                }
                else
                {
M1888's avatar
M1888 committed
171 172 173 174 175 176 177 178 179 180 181
                    ok = false;
                }
                if (ok)
                {
                    lblInfo.Content = "";
                    btnSave.IsEnabled = true;
                }
                else
                {
                    lblInfo.Content = "Syötä vain numeroita 1-40";
                    btnSave.IsEnabled = false;
M1888's avatar
M1888 committed
182 183 184 185 186
                }
            }
        }
    }
}