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

namespace SimpleStm.ViewModels
{
    class SettingsPageViewModel : BindableBase, INavigationAware
    {
        private string _comport;
        public string ComPort
        {
            get { return _comport; }
            set { SetProperty(ref _comport, value); }
        }

        private int _baudrate;
        public int BaudRate
        {
            get { return _baudrate; }
            set { SetProperty(ref _baudrate, value); }
        }

        private int _databits;
        public int DataBits
        {
            get { return _databits; }
            set { SetProperty(ref _databits, value); }
        }

        private StopBits _stopbits;
        public StopBits StopBitss
        {
            get { return _stopbits; }
            set { SetProperty(ref _stopbits, value); }
        }

        private Parity _parity;
        public Parity Parityy
        {
            get { return _parity; }
            set { SetProperty(ref _parity, value); }
        }

        private readonly ComService.StmService _stmService;

        public SettingsPageViewModel(ComService.StmService stmService)
        {
            _stmService = stmService;
            ConnectCommand = new DelegateCommand(Connect);
            DisconnectCommand = new DelegateCommand(Disconnect);

            this.PropertyChanged += OnPropertyChanged;
         
            Comports = new ObservableCollection<string> (SerialPort.GetPortNames());
            
            Baudrates = new ObservableCollection<int> {
                9600, 19200, 38400, 57600, 115200, 230400, 460800
            };

            Databits = new ObservableCollection<int> {
                8,9
            };

            Stopbits = new ObservableCollection<StopBits> {
                StopBits.None, StopBits.One, StopBits.OnePointFive, StopBits.Two
            };

            Paritys = new ObservableCollection<Parity> {
                Parity.None, Parity.Mark, Parity.Even,Parity.Odd, Parity.Space
            };

        }

        private async void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(BaudRate))
            {
                await _stmService.WriteBaudRate(BaudRate);
            }
            else if (e.PropertyName == nameof(DataBits))
            {
                await _stmService.WriteDataBits(DataBits);
            }
            else if (e.PropertyName == nameof(Parityy))
            {
                await _stmService.WriteParity(Parityy);
            }
            else if (e.PropertyName == nameof(StopBitss))
            {
                await _stmService.WriteStopBits(StopBitss);
            }
            else if (e.PropertyName == nameof(ComPort))
            {
                await _stmService.WriteComPort(ComPort);
            }
        }

        public ICommand ConnectCommand { get; private set; }

        public ICommand DisconnectCommand { get; private set; }

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

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

        /* Navigation */
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            BaudRate = Convert.ToInt32(_stmService.BaudRate);
            DataBits = _stmService.DataBits;
            StopBitss = _stmService.StopBitss;
            Parityy = _stmService.Parityy;
            ComPort = _stmService.ComPort;
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        
        /* ComboBox properties */

        private ObservableCollection<string> _comports;

        public ObservableCollection<string> Comports
        {
            get { return _comports; }
            set { _comports = value; }
        }

        private ObservableCollection<int> _baudrates;

        public ObservableCollection<int> Baudrates
        {
            get { return _baudrates; }
            set { _baudrates = value; }
        }

        private ObservableCollection<int> _databitss;

        public ObservableCollection<int> Databits
        {
            get { return _databitss; }
            set { _databitss = value; }
        }

        private ObservableCollection<StopBits> _stopbitss;

        public ObservableCollection<StopBits> Stopbits
        {
            get { return _stopbitss; }
            set { _stopbitss = value; }
        }

        private ObservableCollection<Parity> _paritys;

        public ObservableCollection<Parity> Paritys
        {
            get { return _paritys; }
            set { _paritys = value; }
        }










    }
}
