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

namespace eDosStation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public DataInterface dataInterface;
        public Station thisStation;
        DispatcherTimer bgTimer;

#if SimulateEPD
        public SimulateData simulData = new SimulateData();
#endif
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var w = new ShowEPDDose( thisStation);
            w.ShowDialog();
        }

        private void BEntry_Click(object sender, RoutedEventArgs e)
        {
            var w = new EntryBadge(thisStation);
            w.ShowDialog();
#if SimulateEPD
            simulData = w.simulData;
#endif
            w = null;
        }

        private void BExit_Click(object sender, RoutedEventArgs e)
        {
            var w = new EpdExit(thisStation);
#if SimulateEPD
            w.simulData = simulData;
#endif
            w.ShowDialog();
            w = null;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Settings s = new Settings(thisStation);
            s.ShowDialog();
        }


        private void setLocalStatus()
        {
            if (!dataInterface.isOnline())
            {
                Status.Content = "***   Offline   ***";
            }
            else
            {
                Status.Content = "--- Online ---";
            }
        }

        private void setErrorStatus(string errMsg)
        {
            Status.Content = $"*** Error {errMsg} ***";
            Status.Foreground = Brushes.Yellow;
            Status.Background = Brushes.Red;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bool ok = true;
            string errMsg = string.Empty;
            tVersion.Text = "HPH eDos "+ System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            bgTimer = new DispatcherTimer(DispatcherPriority.Background);
            bgTimer.Interval = TimeSpan.FromSeconds(60);
            bgTimer.Tick += BgTimer_Tick;
            bgTimer.IsEnabled = true;

            try
            {
                dataInterface = new DataInterface();
                thisStation = new Station(dataInterface, string.Empty);
            }
            catch (System.Exception ex)
            {
                errMsg = $"Startup error {ex.Message} ";
                MessageBox.Show(errMsg);
                ok = false;
            }
            if (ok && thisStation.Error > 0)
            {
                errMsg = $"Startup error {thisStation.ErrorMessage} ";
                MessageBox.Show(errMsg);
                ok = false;
            }

            if (ok)
                try
                {
                    //var sp = new System.IO.Ports.SerialPort($"com{thisStation.ComPort}");
                    //sp.BaudRate = 9600;
                    //sp.Parity = System.IO.Ports.Parity.None;
                    //sp.StopBits = System.IO.Ports.StopBits.One;
                    //sp.Open();
                    //sp.DiscardInBuffer();
                    //sp.Close();
#if SimulateEPD
                    thisStation.ComPort = 0;
#endif
                    //ep = new EpdBaseClr.Epd2(thisStation.ComPort);
                    //ep.SetFeedbackSound(thisStation.useBeep);
                }
                catch (System.Exception ex)
                {
                    errMsg = $"Edp software error {ex.Message}";
                    MessageBox.Show(errMsg);
                    ok = false;
                }
            if (ok)
            {
                if (thisStation.insComments.Length > 2)
                {
                    InstMessage w = new InstMessage(thisStation.tbComments);
                    w.ShowDialog();
                    ibComment.Child = thisStation.tbComments;
                    ibComment.Visibility = Visibility.Visible;
                }
                else
                    ibComment.Visibility = Visibility.Collapsed;
            }
            else
                setErrorStatus(errMsg);

            BgTimer_Tick(null, null);
            bgTimer.Start();
        }

        private void BgTimer_Tick(object sender, EventArgs e)
        {
            setLocalStatus();
            tbTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        }

        private void MEnc_Click(object sender, RoutedEventArgs e)
        {
            //string cfg = System.IO.Path.Combine(Environment.CurrentDirectory, System.AppDomain.CurrentDomain.FriendlyName );
            //Config.ToggleConfigEncryption(cfg);
        }

        private void Window_GotFocus(object sender, RoutedEventArgs e)
        {
             bgTimer.Start();
        }

        private void Window_LostFocus(object sender, RoutedEventArgs e)
        {
            bgTimer.Stop(); 
        }

        private void mAbout_Click(object sender, RoutedEventArgs e)
        {
            About x = new About(thisStation);
            x.ShowDialog();
        }

        private void mExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
