using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SimpleStm.ViewModels
{
    class MainPageViewModel : BindableBase, INavigationAware
    {
        private bool _measureActive;
        public bool MeasurementActive
        {
            get { return _measureActive; }
            set { SetProperty(ref _measureActive, value); }
        }

        private double _temperature;
        public double Temperature
        {
            get { return _temperature; }
            set { SetProperty(ref _temperature, value); }
        }

        private double _humidity;
        public double Humidity
        {
            get { return _humidity; }
            set { SetProperty(ref _humidity, value); }
        }

        private double _pressure;
        public double Pressure
        {
            get { return _pressure; }
            set { SetProperty(ref _pressure, value); }
        }

        private int _measPeriod;
        public int Meas_Period
        {
            get { return _measPeriod; }
            set { SetProperty(ref _measPeriod, value); }
        }

        private string _actualDateTime;
        public string ActualDateTime
        {
            get { return _actualDateTime; }
            set { SetProperty(ref _actualDateTime, value); }
        }

        private byte _date;
        public byte Date
        {
            get { return _date; }
            set { SetProperty(ref _date, value); }
        }
        private byte _month;
        public byte Month
        {
            get { return _month; }
            set { SetProperty(ref _month, value); }
        }
        private byte _year;
        public byte Year
        {
            get { return _year; }
            set { SetProperty(ref _year, value); }
        }
        private byte _hour;
        public byte Hour
        {
            get { return _hour; }
            set { SetProperty(ref _hour, value); }
        }
        private byte _minute;
        public byte Minute
        {
            get { return _minute; }
            set { SetProperty(ref _minute, value); }
        }
        private byte _sec;
        public byte Second
        {
            get { return _sec; }
            set { SetProperty(ref _sec, value); }
        }


        private readonly ComService.StmService _stmService;
        public MainPageViewModel(ComService.StmService stmService)
        {
            _stmService = stmService;
            StartMeasurementCommand = new DelegateCommand(StartMeasurement);
            StopMeasurementCommand = new DelegateCommand(StopMeasurement);
            SleepCommand = new DelegateCommand(Sleep);
            SetTimeCommand = new DelegateCommand(SetTime);

            _stmService.ValuesRefreshed += OnStmServiceValuesRefreshed;
            OnStmServiceValuesRefreshed(null, EventArgs.Empty);

            Dates = new ObservableCollection<byte>(GetDates());
            Months = new ObservableCollection<byte>(GetMonths());
            Years = new ObservableCollection<byte>(GetYears());
            Hours = new ObservableCollection<byte>(GetHours());
            Minutes = new ObservableCollection<byte>(GetMinutes());
            Seconds = new ObservableCollection<byte>(GetSeconds());
            //this.PropertyChanged += OnPropertyChanged;

        }

        private void OnStmServiceValuesRefreshed(object sender, EventArgs e)
        {
            MeasurementActive = _stmService.MeasureActive;
            Temperature = _stmService.Temperature;
            Humidity = _stmService.Humidity;
            Pressure = _stmService.Pressure;
            Meas_Period = _stmService.MeasurementPeriod;
            ActualDateTime = _stmService.ActualDateTime;

            _stmService.Date = Date;
            _stmService.Month = Month;
            _stmService.Year = Year;
            _stmService.Hours = Hour;
            _stmService.Minutes = Minute;
            _stmService.Seconds = Second;
        }

        private void StartMeasurement()
        {
            _stmService.StartMeasurement();
        }

        private void StopMeasurement()
        {
            _stmService.StopMeasurement();
        }

        private void Sleep()
        {
            _stmService.Sleep();
        }

        private void SetTime()
        {
            _stmService.SetDatetime();
        }


        public ICommand StartMeasurementCommand { get; private set; }

        public ICommand StopMeasurementCommand { get; private set; }

        public ICommand SleepCommand { get; private set; }

        public ICommand SetTimeCommand { get; private set; }

        /* Navigation */
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            MeasurementActive = _stmService.MeasureActive;
            Temperature = _stmService.Temperature;
            Humidity = _stmService.Humidity;
            Pressure = _stmService.Pressure;
            Meas_Period = _stmService.MeasurementPeriod;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        /* ComboBox properties */

        private ObservableCollection<byte> GetDates()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 1; i <= 31; ++i)
                {
                   dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _dates;
        public ObservableCollection<byte> Dates
        {
            get { return _dates; }
            set { _dates = value; }
        }

        private ObservableCollection<byte> GetMonths()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 1; i <= 12; ++i)
                {
                    dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _months;
        public ObservableCollection<byte> Months
        {
            get { return _months; }
            set { _months = value; }
        }

        private ObservableCollection<byte> GetYears()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 0; i <= 100; ++i)
                {
                    dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _years;
        public ObservableCollection<byte> Years
        {
            get { return _years; }
            set { _years = value; }
        }

        private ObservableCollection<byte> GetHours()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 0; i <= 24; ++i)
                {
                    dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _hours;
        public ObservableCollection<byte> Hours
        {
            get { return _hours; }
            set { _hours = value; }
        }

        private ObservableCollection<byte> GetMinutes()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 0; i <= 59; ++i)
                {
                    dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _minutes;
        public ObservableCollection<byte> Minutes
        {
            get { return _minutes; }
            set { _minutes = value; }
        }

        private ObservableCollection<byte> GetSeconds()
        {
            ObservableCollection<byte> dat = new ObservableCollection<byte>();
            {
                for (byte i = 0; i <= 59; ++i)
                {
                    dat.Add(i);
                }
            }
            return dat;
        }

        private ObservableCollection<byte> _seconds;
        public ObservableCollection<byte> Seconds
        {
            get { return _seconds; }
            set { _seconds = value; }
        }


    }
}
