using System.Collections.ObjectModel;
using System.Windows;

namespace eDosStation
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public class beepclass
    {
        public string Text { get; set; }
        public byte Value { get; set; }
        public beepclass(string txt, byte val)
        {
            Text = txt; Value = val; 
        }
    }

    


    public class readerclass
    {
        public string Text { get; set; }
        public readerclass(string txt)
        {
            Text = txt; 
        }
    }

    public partial class Settings : Window
    {
        Station thisStation;
        ObservableCollection<beepclass> beepValues = new ObservableCollection<beepclass>();
        ObservableCollection<readerclass> readerValues = new ObservableCollection<readerclass>();

        public Settings( Station tStation)
        {
            thisStation = tStation;
            InitializeComponent();
        }

        private void BOK_Click(object sender, RoutedEventArgs e)
        {
            string thiscom = cComPort.SelectedItem.ToString().Replace("COM","");
            thisStation.ComPort = int.Parse(thiscom);

            DataSet1.InstallationRow ir = CIns1.SelectedItem as DataSet1.InstallationRow;
            thisStation.Inst_CODE1 = ir.Inst_CODE;
            thisStation.Inst_Name1 = ir.InstallationName;

            ir = CIns2.SelectedItem as DataSet1.InstallationRow;
            thisStation.Inst_CODE2 = ir.Inst_CODE;
            thisStation.Inst_Name2 = ir.InstallationName;

            thisStation.useBeep = (byte)tBeep.SelectedValue;

            if (cbValidation.IsChecked == null) cbValidation.IsChecked = false;
            thisStation.useTestButtons = cbValidation.IsChecked.Value;
            thisStation.Reader = (string)cbReaders.SelectedValue;
            thisStation.checkUser = cbCheckUsers.IsChecked != null ? cbCheckUsers.IsChecked.Value : false;

            thisStation.EPDTimeOut = int.Parse(tEPDTimeout.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)
        {
          
            using (var a = new LSWDesfirePublic.CCard())
            {
                a.GetReaderList();
                foreach (string s in a.readersList) { readerValues.Add(new readerclass(s)); }
            }
            cbReaders.ItemsSource = readerValues;
            cbReaders.SelectedValue = thisStation.Reader;
            

            foreach (string cn in System.IO.Ports.SerialPort.GetPortNames()) 
            {
                cComPort.Items.Add(cn);
            }

            beepValues.Add(new beepclass("Loud High freq Beep"  , 0b00010111    ));
            beepValues.Add(new beepclass("Loud Low freq Beep"   , 0b00011111    ));
            beepValues.Add(new beepclass("Normal High freq Beep", 0b00010110    ));
            beepValues.Add(new beepclass("Normal Low freq Beep" , 0b00011110    ));
            beepValues.Add(new beepclass("No Beep (only led)"   , 0b00011010    ));

            
           


            //                  7 6 5 4 3210
            //  1 0 0 1 1 1 0 0 1 1 1 0 0001
            //  1 0 0 1 1 1 0 0 1 1 0 0 0001
            //  1 0 0 1 1 1 0 0 1 0 1 0 0001
            //  1 0 0 1 1 1 0 0 1 0 0 0 0001
            //  1 0 0 1 1 1 0 0 0 0 0 0 0000

            //MK3
            //Bits 0-3 = Graphic Wearer Name, displayed in EPD Assignment menu
            //0000 : Off(no sounder)
            //0001 : Continuous Single Tone
            //0010 : Continuous Dual Tone Slow
            //0011 : Continuous Dual Tone Fast
            //0100 : Intermittent Single Slow
            //0101 : Intermittent Single Fast
            //0110 : Intermittent Double Slow
            //0111 : Intermittent Double Fast
            //1110 : Mirion Style Dose
            //1111 : Mirion Style Rate
            //Bit 4 = Not used
            //Bit 5 = Tone 2kHz = 0, 4kHz = 1
            //Bit 6 = Volume Loud = 1, Normal = 0
            //Bit 7 = Sound Enabled = 1
            //Bit 8, 9 = Not used
            //Bit 10 = LED Brightness Bright = 1, Normal = 0
            //Bit 11 = LED Enabled = 1
            //Bit 12 = Mutable User can mute = 1
            //Bit 13, 14 = Not Used
            //Bit 15 = Vibe Enabled = 1

            //beepValues.Add(new beepclass("Beep", 22));
            //beepValues.Add(new beepclass("No Beep", 18));
            tBeep.ItemsSource = beepValues;

            string thisComport = $"COM{thisStation.ComPort}";
            if (!cComPort.Items.Contains(thisComport)) cComPort.Items.Add(thisComport);
            cComPort.SelectedItem = thisComport;

            DataSet1.InstallationDataTable tbInstallations;
            using (var ta = new DataSet1TableAdapters.InstallationTableAdapter())
                tbInstallations = ta.GetData();
            tbInstallations.AddInstallationRow(0, "-");
            
            foreach (DataSet1.InstallationRow r  in tbInstallations.Rows)
            {
                CIns1.Items.Add(r);
                CIns2.Items.Add(r);
            }
            CIns1.SelectedValue = thisStation.Inst_CODE1;
            CIns2.SelectedValue = thisStation.Inst_CODE2;

            tBeep.SelectedValue = thisStation.useBeep;
            cbValidation.IsChecked = thisStation.useTestButtons;
            cbCheckUsers.IsChecked= thisStation.checkUser;

            tEPDTimeout.Text = thisStation.EPDTimeOut.ToString();

            bool rv = thisStation.dataInterface.checkCon(false);
            
        }
    }
}
