using System;
using System.Threading;
using Plugin.Settings;
using Plugin.Settings.Abstractions;

namespace Aurora.Services
{
    public class SettingsService : BaseService<SettingsService>
    {
        private Lazy<ISettings> _appSettings;
        private string _usernameKey = "username";
        private string _defaultPortKey = "port";

        public SettingsService()
        {
        }

        public ISettings AppSettings
        {
            get
            {
                if (_appSettings == null)
                {
                    _appSettings = new Lazy<ISettings>(() => CrossSettings.Current, LazyThreadSafetyMode.PublicationOnly);
                }

                return _appSettings.Value;
            }
            set
            {
                _appSettings = new Lazy<ISettings>(() => value, LazyThreadSafetyMode.PublicationOnly);
            }
        }

        /// <summary>
        /// The user's username. This is persisted.
        /// </summary>
        /// <value></value>
        public string Username
        {
            get { return AppSettings.GetValueOrDefault(_usernameKey, ""); }
            set
            {
                AppSettings.AddOrUpdateValue(_usernameKey, value);
            }
        }

        /// <summary>
        /// The default port to use. This is persisted.
        /// </summary>
        /// <value></value>
        public int DefaultPort
        {
            get { return AppSettings.GetValueOrDefault(_defaultPortKey, 4005); }
            set { AppSettings.AddOrUpdateValue(_defaultPortKey, value); }
        }

        /// <summary>
        /// The current sessions clientId. This is assigned by the server. This is not persisted.
        /// </summary>
        /// <value></value>
        public string ClientId
        {
            get; set;
        }

    }
}