using System;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;

namespace GTPCorrgir
{
    public class UserSettings
    {
        private static readonly string SettingsPath =
            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "usersettings.json");

        private static UserSettings instance;
        private static readonly object lockObject = new object();

        public class WindowSettings
        {
            public double Left { get; set; }
            public double Top { get; set; }
            public double Width { get; set; }
            public double Height { get; set; }
            public double Opacity { get; set; }
        }

        public class AppearanceSettings
        {
            public string Theme { get; set; } = "Light";
            public int FontSize { get; set; } = 14;
            public string FontFamily { get; set; } = "Segoe UI";
            public int OpacityDelay { get; set; } = 10000; // milisegundos
        }

        public class BehaviorSettings
        {
            public bool AutoSaveHistory { get; set; } = true;
            public bool ShowNotifications { get; set; } = true;
            public int NotificationDuration { get; set; } = 5000; // milisegundos
            public bool AutoCopyToClipboard { get; set; } = true;
        }

        public WindowSettings Window { get; set; }
        public AppearanceSettings Appearance { get; set; }
        public BehaviorSettings Behavior { get; set; }

        // Constructor público con valores por defecto
        public UserSettings()
        {
            Window = new WindowSettings();
            Appearance = new AppearanceSettings();
            Behavior = new BehaviorSettings();
        }

        public static UserSettings Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObject)
                    {
                        instance ??= Load();
                    }
                }
                return instance;
            }
        }

        private static UserSettings Load()
        {
            try
            {
                if (File.Exists(SettingsPath))
                {
                    string json = File.ReadAllText(SettingsPath);
                    var settings = JsonConvert.DeserializeObject<UserSettings>(json);
                    if (settings != null)
                    {
                        return settings;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error loading settings: {ex.Message}");
            }
            return new UserSettings();
        }

        public void Save()
        {
            try
            {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText(SettingsPath, json);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error saving settings: {ex.Message}");
            }
        }

        public void ResetToDefaults()
        {
            Window = new WindowSettings();
            Appearance = new AppearanceSettings();
            Behavior = new BehaviorSettings();
        }
    }
}