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.Shapes;

namespace eDosStation
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        Station thisStation;
        public Settings( Station tStation)
        {
            thisStation = tStation;
            InitializeComponent();
        }

        private void BOK_Click(object sender, RoutedEventArgs e)
        {
                thisStation.ComPort = int.Parse(tComPort.Text);
                thisStation.Inst_CODE1 = int.Parse(tInsCode1.Text);
                thisStation.Inst_CODE2 = int.Parse(tInsCode2.Text);
                thisStation.Inst_Name1= tInsName1.Text;
                thisStation.Inst_Name2 = tInsName2.Text;
                thisStation.TestCSN = tTestCSN.Text;
                thisStation.Reader = cbReaders.Text;
                thisStation.Update();
                this.Close();
            if (thisStation.Error>0)
            {
                MessageBox.Show($"Error {thisStation.ErrorMessage}");
            }
        }

        private void BCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tComPort.Text = thisStation.ComPort.ToString();
            tInsCode1.Text = thisStation.Inst_CODE1.ToString();
            tInsCode2.Text = thisStation.Inst_CODE2.ToString();
            tInsName1.Text = thisStation.Inst_Name1.ToString();
            tInsName2.Text = thisStation.Inst_Name2.ToString();
            tTestCSN.Text = thisStation.TestCSN;

            bool rv = thisStation.dataInterface.checkCon(false);
            var a = new LSWDesfirePublic.CCard();
            a.GetReaderList();
            cbReaders.Items.Clear();
            foreach (string s in a.readersList )
            {
                ComboBoxItem b = new ComboBoxItem();
                b.Content = s;
                if (s.Equals(thisStation.Reader))
                {
                    b.IsSelected = true;
                    b.Background = Brushes.Green;
                }
                cbReaders.Items.Add(b);
            }
            
            a.Dispose();
        }
    }
}
