/*
 * NotificationPreferences.vala
 * 
 * Represents user notification preferences.
 * Following GNOME HIG naming conventions and Vala/GObject patterns.
 */

/**
 * NotificationPreferences - User notification settings
 */
public class RSSuper.NotificationPreferences : Object {
    public bool new_articles { get; set; }
    public bool episode_releases { get; set; }
    public bool custom_alerts { get; set; }
    public bool badge_count { get; set; }
    public bool sound { get; set; }
    public bool vibration { get; set; }
    
    /**
     * Default constructor (all enabled by default)
     */
    public NotificationPreferences() {
        this.new_articles = true;
        this.episode_releases = true;
        this.custom_alerts = true;
        this.badge_count = true;
        this.sound = true;
        this.vibration = true;
    }
    
    /**
     * Constructor with initial values
     */
    public NotificationPreferences.with_values(bool new_articles = true,
                                                bool episode_releases = true,
                                                bool custom_alerts = true,
                                                bool badge_count = true,
                                                bool sound = true,
                                                bool vibration = true) {
        this.new_articles = new_articles;
        this.episode_releases = episode_releases;
        this.custom_alerts = custom_alerts;
        this.badge_count = badge_count;
        this.sound = sound;
        this.vibration = vibration;
    }
    
    /**
     * Enable all notifications
     */
    public void enable_all() {
        this.new_articles = true;
        this.episode_releases = true;
        this.custom_alerts = true;
        this.badge_count = true;
        this.sound = true;
        this.vibration = true;
    }
    
    /**
     * Disable all notifications
     */
    public void disable_all() {
        this.new_articles = false;
        this.episode_releases = false;
        this.custom_alerts = false;
        this.badge_count = false;
        this.sound = false;
        this.vibration = false;
    }
    
    /**
     * Check if any notifications are enabled
     */
    public bool has_any_enabled() {
        return this.new_articles ||
               this.episode_releases ||
               this.custom_alerts ||
               this.badge_count ||
               this.sound ||
               this.vibration;
    }
    
    /**
     * Check if content notifications are enabled
     */
    public bool has_content_notifications() {
        return this.new_articles || this.episode_releases || this.custom_alerts;
    }
    
    /**
     * Serialize to JSON string
     */
    public string to_json_string() {
        var sb = new StringBuilder();
        sb.append("{");
        sb.append("\"newArticles\":");
        sb.append(this.new_articles ? "true" : "false");
        sb.append(",\"episodeReleases\":");
        sb.append(this.episode_releases ? "true" : "false");
        sb.append(",\"customAlerts\":");
        sb.append(this.custom_alerts ? "true" : "false");
        sb.append(",\"badgeCount\":");
        sb.append(this.badge_count ? "true" : "false");
        sb.append(",\"sound\":");
        sb.append(this.sound ? "true" : "false");
        sb.append(",\"vibration\":");
        sb.append(this.vibration ? "true" : "false");
        sb.append("}");
        return sb.str;
    }
    
    /**
     * Deserialize from JSON string
     */
    public static NotificationPreferences? from_json_string(string json_string) {
        var parser = new Json.Parser();
        try {
            if (!parser.load_from_data(json_string)) {
                return null;
            }
        } catch (Error e) {
            warning("Failed to parse JSON: %s", e.message);
            return null;
        }
        
        return from_json_node(parser.get_root());
    }
    
    /**
     * Deserialize from Json.Node
     */
    public static NotificationPreferences? from_json_node(Json.Node node) {
        if (node.get_node_type() != Json.NodeType.OBJECT) {
            return null;
        }
        
        var obj = node.get_object();
        var prefs = new NotificationPreferences();
        
        if (obj.has_member("newArticles")) {
            prefs.new_articles = obj.get_boolean_member("newArticles");
        }
        if (obj.has_member("episodeReleases")) {
            prefs.episode_releases = obj.get_boolean_member("episodeReleases");
        }
        if (obj.has_member("customAlerts")) {
            prefs.custom_alerts = obj.get_boolean_member("customAlerts");
        }
        if (obj.has_member("badgeCount")) {
            prefs.badge_count = obj.get_boolean_member("badgeCount");
        }
        if (obj.has_member("sound")) {
            prefs.sound = obj.get_boolean_member("sound");
        }
        if (obj.has_member("vibration")) {
            prefs.vibration = obj.get_boolean_member("vibration");
        }
        
        return prefs;
    }
    
    /**
     * Equality comparison
     */
    public bool equals(NotificationPreferences? other) {
        if (other == null) {
            return false;
        }
        
        return this.new_articles == other.new_articles &&
               this.episode_releases == other.episode_releases &&
               this.custom_alerts == other.custom_alerts &&
               this.badge_count == other.badge_count &&
               this.sound == other.sound &&
               this.vibration == other.vibration;
    }
    
    /**
     * Copy preferences from another instance
     */
    public void copy_from(NotificationPreferences other) {
        this.new_articles = other.new_articles;
        this.episode_releases = other.episode_releases;
        this.custom_alerts = other.custom_alerts;
        this.badge_count = other.badge_count;
        this.sound = other.sound;
        this.vibration = other.vibration;
    }
}
