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;
using System.Windows.Threading;

namespace eDosStation
{
    /// <summary>
    /// Interaction logic for ShowEPDDose.xaml
    /// </summary>
    public partial class ShowEPDDose : Window
    {
        Station thisStation;
        EpdBaseClr.Epd2 ep;
        bool EpdFound = false;
        DispatcherTimer dispatcherTimer;
        int Search;

        public ShowEPDDose(EpdBaseClr.Epd2 curEP, Station curStation)
        {
            InitializeComponent();
            ep = curEP;
            thisStation = curStation;
        }

        private void LoadDoses()
        {
            ep.GetStatus();

            isIssued.IsChecked = ep.Issued;
            Wearer.Text = ep.WearerName;
            EpdVisit.Text = $"Visit {ep.WearerID}";

            if ((ep.otherAlarms & 0x7f)!=0 || ep.alarmStatus>0)
            {
                cAlarm.IsChecked = true;
            } else
                cAlarm.IsChecked = false;

            Decoder.AlarmToTextBox(sAlarm, ep);

            ep.GetDoses();
            HpDose07l.Text = $"Hp07";
            HpDose10l.Text = $"Hp10";

            HpDose07.Text = $"{ep.Hp07:0.00}  µSv";
            HpDose10.Text = $"{ep.Hp10:0.00} µSv";

            ep.ReadTresholds();
            HP10AlarmDose1l.Text = "Alarm HP10(1)";
            HP10AlarmDose2l.Text = "Alarm HP10(2)";
            HP07AlarmDosel.Text = "Alarm HP07";

            HP10AlarmDose1.Text = $"{ep.Hp10THDose1:0.00} µSv";
            HP10AlarmDose2.Text = $"{ep.Hp10THDose2:0.00} µSv";
            HP07AlarmDose.Text = $"{ep.Hp07THDose:0.00} µSv";

            //ep.ReadExtraStatus();

            //BatVolt.Text = $" {ep.BatADC * 16.0} mV";
            //SupplyVolt.Text = $" {ep.SupplyADC* 16.0} mV";

            //int Hp10THDose2;
            //int Hp07THDose;
            ep.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int ConnectRV = 0;
            System.Diagnostics.Debug.WriteLine(thisStation.ComPort.ToString());
            try
            {
                //ep = new EpdBaseClr.Epd2(thisStation.ComPort);
                ConnectRV = ep.Connect(false);
            } catch (System.Exception ex)
            {
                MessageBox.Show($"Error loading dll's {ex.Message}");
            }
            if (ep.DeviceCount == 1 )
            {
                System.Diagnostics.Debug.WriteLine("found");
                EpdFound = true;
                statusEPD.Content = $"EPD:  {ep.b.CurrentDeviceID}";
                LoadDoses();
            }
            if (!EpdFound)
            {
                statusEPD.Content = $"EPD:  Wait ";
                Search = 100;
                dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += DispatcherTimer_Tick;
                dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
                dispatcherTimer.Start();
            }

        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Stop();
            ep.Connect(false);
            if (ep.DeviceCount == 1 )
            {
                EpdFound = true;
                statusEPD.Content = $"EPD:  {ep.b.CurrentDeviceID}";
                LoadDoses();
            }
            if (!EpdFound)
            {   Search--;
                if (Search > 0)
                {
                    statusEPD.Content = $"Search {Search}";
                    dispatcherTimer.Start();
                }  else
                {
                    statusEPD.Content = $"Search Timeout";
                }
            }
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimer?.Stop(); 
            ep.Close();
        }
    }
}
