using System.Diagnostics;
using System.Windows;

namespace Jeti_Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Specbos? mySpeckBos;
        Stopwatch myStopWatch;
        Specbos.Errorcode err;
        Polygon_vm polygon;

        public MainWindow()
        {
            InitializeComponent();
            myStopWatch = new Stopwatch();
            mySpeckBos = new ();
            err = Specbos.Errorcode.SUCCESS;
            polygon    = new(); // = (Polygon_vm)this.TryFindResource("pol"); //   
            canvas.DataContext = polygon;
        }

        private void mnuExitClick(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void tbxIntTimeValidate(object sender, RoutedEventArgs e)
        {
            float value;
            if (float.TryParse(tbxIntTime.Text, out value)) mySpeckBos.fTint = value;
            else
            {
                MessageBox.Show("Ungültige Eingabe!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                tbxIntTime.Text = "0";
            }
        }

        private void btnStartClick(object sender, RoutedEventArgs e)
        {
            myStopWatch.Start();
            float.TryParse(tbxIntTime.Text, out mySpeckBos.fTint);
            ushort.TryParse(tbxAvgCount.Text, out mySpeckBos.ushAverage);
            uint.TryParse(tbxNmStep.Text, out mySpeckBos.uiStep);
            err = mySpeckBos.MeasureEx();
                myStopWatch.Stop();
            tbxResult.Text = Enum.GetName(typeof(Specbos.Errorcode), err);
                tbxStopwatch.Text = myStopWatch.Elapsed.ToString();
            if (err == Specbos.Errorcode.SUCCESS)
            {
                mySpeckBos.GetRadiance();
                tbxRadiance.Text = mySpeckBos.FRadio.ToString();
                mySpeckBos.GetPhoto();
                tbxPkWavelength.Text = mySpeckBos.FPhoto.ToString();
            }
        }

        private void lblConnStatus_DoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                if (mySpeckBos.FindDevices() == Specbos.Errorcode.SUCCESS)
                {
                    if (mySpeckBos.OpenDevice() == Specbos.Errorcode.SUCCESS) lblConnStatus.Content = "connected";
                }

            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }    
        }

        private void btnPolygon_Click(object sender, RoutedEventArgs e)
        {
            double xmaxcoord = canvas.ActualWidth - 1;
            double ymaxcoord = canvas.ActualHeight - 1;
            uint.TryParse(tbxNmStart.Text, out mySpeckBos.nmStart);
            uint.TryParse(tbxNmEnd.Text, out mySpeckBos.nmEnd);

            if (mySpeckBos.GetRadiances() == Specbos.Errorcode.SUCCESS)
            {
                polygon.BuildPolygon(mySpeckBos.FSPRad, (int)canvas.ActualWidth, (int)canvas.ActualHeight);
            }
        }
    }
}