﻿using RemoteSensorSample.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Sensors;
using Windows.UI.Core;

namespace RemoteSensorSample
{
    class MainPageVM : ObservableObject
    {
        #region Instance Variables
        private string sensorXReading;
        private string sensorYReading;
        private string sensorZReading;
        private string borderXLocation;
        private string borderYLocation;
        private bool readSensor;

        private Accelerometer accelerometer;

        private readonly CoreDispatcher dispatcher;
        #endregion

        #region Properties
        public string SensorXReading
        {
            get
            {
                return sensorXReading;
            }

            set
            {
                if (sensorXReading != value)
                {
                    sensorXReading = value;
                    OnPropertyChanged();
                }
            }
        }

        public string SensorYReading
        {
            get
            {
                return sensorYReading;
            }

            set
            {
                if (sensorYReading != value)
                {
                    sensorYReading = value;
                    OnPropertyChanged();
                }
            }
        }

        public string SensorZReading
        {
            get
            {
                return sensorZReading;
            }

            set
            {
                if (sensorZReading != value)
                {
                    sensorZReading = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BorderXLocation
        {
            get
            {
                return borderXLocation;
            }

            set
            {
                if (borderXLocation != value)
                {
                    borderXLocation = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BorderYLocation
        {
            get
            {
                return borderYLocation;
            }

            set
            {
                if (borderYLocation != value)
                {
                    borderYLocation = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool ReadSensor
        {
            get
            {
                return readSensor;
            }

            set
            {
                if (readSensor != value)
                {
                    readSensor = value;
                    if (readSensor && accelerometer != null)
                    {
                        accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
                    }
                    else if (accelerometer != null)
                    {
                        accelerometer.ReadingChanged -= Accelerometer_ReadingChanged;
                    }
                    OnPropertyChanged();
                }
            }
        }

        private async void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
        {
            if (dispatcher.HasThreadAccess)
            {
                var xReading = args.Reading.AccelerationX * -100f;
                var yReading = args.Reading.AccelerationY * -100f;
                var zReading = args.Reading.AccelerationZ * -100f;
                SensorXReading = (args.Reading.AccelerationX * -100f).ToString("F1");
                SensorYReading = (args.Reading.AccelerationY * -100f).ToString("F1");
                SensorZReading = (args.Reading.AccelerationZ * -100f).ToString("F1");
                BorderXLocation = (yReading * 2 + 200).ToString("F1");
                BorderYLocation = (xReading * 2 + 200).ToString("F1");
            }
            else
            {
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
                {
                    var xReading = args.Reading.AccelerationX * -100f;
                    var yReading = args.Reading.AccelerationY * -100f;
                    var zReading = args.Reading.AccelerationZ * -100f;
                    SensorXReading = (args.Reading.AccelerationX * -100f).ToString("F1");
                    SensorYReading = (args.Reading.AccelerationY * -100f).ToString("F1");
                    SensorZReading = (args.Reading.AccelerationZ * -100f).ToString("F1");
                    BorderXLocation = (yReading * 2 + 200).ToString("F1");
                    BorderYLocation = (xReading * 2 + 200).ToString("F1");
                }));
            }

        }
    #endregion

    public MainPageVM()
        {
            accelerometer = Accelerometer.GetDefault();
            accelerometer.ReportInterval = 1000;
            if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            {
                this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
            }
        }
    }
}
