using PCUT.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace PCUT.ViewModels
{
    public class PortSettingViewModel : NotificationBase
    {
        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 int _parity;
        public int SelectedParity
        {
            get { return _parity; }
            set { SetProperty(ref _parity, value); }
        }

        private int _stopbits;
        public int SelectedStopbits
        {
            get { return _stopbits; }
            set { SetProperty(ref _stopbits, value); }
        }

        private int _speed;
        public int Speed
        {
            get { return _speed; }
            set { SetProperty(ref _speed, value); }
        }

        private int _pen;
        public int SelectedPen
        {
            get { return _pen; }
            set { SetProperty(ref _pen, value); }
        }

        public ObservableCollection<Parity> ParityList = new ObservableCollection<Parity>()
        {
            Parity.None,
            Parity.Odd,
            Parity.Even,
            Parity.Mark,
            Parity.Space
        };

        public ObservableCollection<StopBits> StopbitsList = new ObservableCollection<StopBits>()
        {
            StopBits.None,
            StopBits.One,
            StopBits.Two,
            StopBits.OnePointFive,
        };

        public ObservableCollection<int> PenList = new ObservableCollection<int>()
        {
            0,
            1,
            2,
            3
        };

        public void LoadSetting(string portName)
        {
            var settings = ApplicationData.Current.LocalSettings;
            if (settings.Values.TryGetValue($"{portName}_PortBaudrate", out var baudrate))
                BaudRate = (int)baudrate;
            else
                BaudRate = 9600;

            if (settings.Values.TryGetValue($"{portName}_PortDatabits", out var databits))
                Databits = (int)databits;
            else
                Databits = 8;

            if (settings.Values.TryGetValue($"{portName}_PortParity", out var parity))
                SelectedParity = (int)parity;
            else
                SelectedParity = 0;

            if (settings.Values.TryGetValue($"{portName}_PortStopbits", out var stopbits))
                SelectedStopbits = (int)stopbits;
            else
                SelectedStopbits = 1;

            if (settings.Values.TryGetValue($"{portName}_Speed", out var speed))
                Speed = (int)speed;
            else
                Speed = 82;

            if (settings.Values.TryGetValue($"{portName}_PortPen", out var pen))
                SelectedPen = (int)pen;
            else
                SelectedPen = 0;

        }

        public void SaveSetting(string portName)
        {
            var settings = ApplicationData.Current.LocalSettings;
            settings.Values[$"{portName}_PortBaudrate"] = BaudRate;
            settings.Values[$"{portName}_PortDatabits"] = Databits;
            settings.Values[$"{portName}_PortParity"] = SelectedParity;
            settings.Values[$"{portName}_PortStopbits"] = SelectedStopbits;
            settings.Values[$"{portName}_Speed"] = Speed;
            settings.Values[$"{portName}_PortPen"] = SelectedPen;
        }
    }
}
