using Newtonsoft.Json;
using PlantBox.Client.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace PlantBox.Client.ViewModels
{
    public class SettingsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public const string FileName = "Settings.json";
        public string FilePath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FileName);

        private Settings _settings;

        // Settings
        public CultureInfo Language
        {
            get => _settings.Language;
            set
            {
                if (value != _settings.Language)
                {
                    _settings.Language = value;
                    OnPropertyChanged();
                }
            }
        }
        public bool NotificationsDisabled
        {
            get => _settings.NotificationsDisabled;
            set
            {
                if (value != _settings.NotificationsDisabled)
                {
                    _settings.NotificationsDisabled = value;
                    OnPropertyChanged();
                }
            }
        }
        public bool NotificationsMuted
        {
            get => _settings.NotificationsMuted;
            set
            {
                if (value != _settings.NotificationsMuted)
                {
                    _settings.NotificationsMuted = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BrokerIP
        {
            get => _settings.BrokerIP;
            set
            {
                if (value != _settings.BrokerIP)
                {
                    _settings.BrokerIP = value;
                    OnPropertyChanged();
                }
            }
        }

        public List<ulong> IDs
        {
            get => _settings.IDs;
            set
            {
                if (value != _settings.IDs)
                {
                    _settings.IDs = value;
                    OnPropertyChanged();
                }
            }
        }

        public SettingsViewModel()
        {
            if (File.Exists(FilePath))
            {
                _settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(FilePath));
            }
            else
            {
                _settings = new Settings();
            }
        }

        public void Save()
        {
            using (var file = new StreamWriter(FilePath, false))
            {
                file.WriteLine(JsonConvert.SerializeObject(_settings));
            }
        }

        public async Task SaveAsync()
        {
            using (var file = new StreamWriter(FilePath, false))
            {
                await file.WriteLineAsync(JsonConvert.SerializeObject(_settings));
            }
        }

        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
