using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;

namespace TodoList.Services
{
    public partial class AppSettings : ObservableObject
    {
        [ObservableProperty]
        private string shortcutModifiers = "Control,Alt"; // Comma separated

        [ObservableProperty]
        private string shortcutKey = "A";
    }

    public class SettingsService
    {
        private readonly string _filePath;
        public AppSettings Settings { get; private set; }

        public SettingsService()
        {
            var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var folder = Path.Combine(appData, "TodoListApp");
            Directory.CreateDirectory(folder);
            _filePath = Path.Combine(folder, "settings.json");
            Settings = LoadSettings();
        }

        private AppSettings LoadSettings()
        {
            if (!File.Exists(_filePath)) return new AppSettings();
            try
            {
                var json = File.ReadAllText(_filePath);
                return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
            }
            catch
            {
                return new AppSettings();
            }
        }

        public void SaveSettings()
        {
            try
            {
                var json = JsonSerializer.Serialize(Settings, new JsonSerializerOptions { WriteIndented = true });
                File.WriteAllText(_filePath, json);
            }
            catch { }
        }
    }
}
