using Gee;

namespace StillExplore {
    public string _(string text) {
        return GLib.dgettext(GETTEXT_PACKAGE, text);
    }

    
    public class ActionConfig : GLib.Object {
        public string action_type { get; set; }
        public string target { get; set; }
        public string? button_name { get; set; }
        public string? button_style_class { get; set; }
        
        public ActionConfig() {}
        
        public ActionConfig.from_json(Json.Object obj) {
            this.action_type = obj.get_string_member("type");
            this.target = obj.get_string_member("target");
            
            if (obj.has_member("button_name")) {
                this.button_name = _(obj.get_string_member("button_name"));
            }
            
            if (obj.has_member("button_style_class")) {
                this.button_style_class = obj.get_string_member("button_style_class");
            }
        }
    }
    
    public class ActionItemConfig : GLib.Object {
        public string title { get; set; }
        public string? description { get; set; }
        public string? icon { get; set; }
        public string? svg { get; set; }
        public ActionConfig action { get; set; }
        
        public ActionItemConfig() {}
        
        public ActionItemConfig.from_json(Json.Object obj) {
            this.title = _(obj.get_string_member("title"));
            
            if (obj.has_member("description")) {
                this.description = _(obj.get_string_member("description"));
            }
            
            if (obj.has_member("icon")) {
                this.icon = obj.get_string_member("icon");
            }
            
            if (obj.has_member("svg")) {
                this.svg = obj.get_string_member("svg");
            }
            
            var action_obj = obj.get_object_member("action");
            this.action = new ActionConfig.from_json(action_obj);
        }
    }
    
    public class GroupConfig : GLib.Object {
        public string title { get; set; }
        public string? description { get; set; }
        public Gee.List<ActionItemConfig> actions { get; set; }
        
        public GroupConfig() {
            this.actions = new Gee.ArrayList<ActionItemConfig>();
        }
        
        public GroupConfig.from_json(Json.Object obj) {
            this();
            this.title = _(obj.get_string_member("title"));
            
            if (obj.has_member("description")) {
                this.description = _(obj.get_string_member("description"));
            }
            
            var actions_array = obj.get_array_member("actions");
            for (uint i = 0; i < actions_array.get_length(); i++) {
                var action_obj = actions_array.get_object_element(i);
                this.actions.add(new ActionItemConfig.from_json(action_obj));
            }
        }
    }
    
    public class PageConfig : GLib.Object {
        public string id { get; set; }
        public string title { get; set; }
        public string? titlebar { get; set; }
        public string? icon { get; set; }
        public Gee.List<GroupConfig> groups { get; set; }
        
        public PageConfig() {
            this.groups = new Gee.ArrayList<GroupConfig>();
        }
        
        public PageConfig.from_json(Json.Object obj) {
            this();
            this.id = obj.get_string_member("id");
            this.title = _(obj.get_string_member("title"));
            
            if (obj.has_member("titlebar")) {
                this.titlebar = _(obj.get_string_member("titlebar"));
            }
            
            if (obj.has_member("icon")) {
                this.icon = obj.get_string_member("icon");
            }
            
            var groups_array = obj.get_array_member("groups");
            for (uint i = 0; i < groups_array.get_length(); i++) {
                var group_obj = groups_array.get_object_element(i);
                this.groups.add(new GroupConfig.from_json(group_obj));
            }
        }
    }
    
    public class AppConfig : GLib.Object {
        public string app_title { get; set; }
        
        public AppConfig() {}
        
        public AppConfig.from_json(Json.Object obj) {
            this.app_title = _(obj.get_string_member("app_title"));
        }
    }
    
    public class Config : GLib.Object {
        public AppConfig app { get; set; }
        public Gee.List<PageConfig> pages { get; set; }
        
        public Config() {
            this.pages = new Gee.ArrayList<PageConfig>();
        }
        
        public Config.from_json(Json.Object obj) {
            this();
            
            var app_obj = obj.get_object_member("app");
            this.app = new AppConfig.from_json(app_obj);
            
            var pages_array = obj.get_array_member("pages");
            for (uint i = 0; i < pages_array.get_length(); i++) {
                var page_obj = pages_array.get_object_element(i);
                this.pages.add(new PageConfig.from_json(page_obj));
            }
        }
        
        public static Config? load_from_file(string filename) {
            try {
                var parser = new Json.Parser();
                parser.load_from_file(filename);
                
                var root = parser.get_root();
                if (root == null || root.get_node_type() != Json.NodeType.OBJECT) {
                    warning("Invalid JSON structure in config file: %s", filename);
                    return null;
                }
                
                var root_obj = root.get_object();
                return new Config.from_json(root_obj);
                
            } catch (Error e) {
                warning("Failed to load config from %s: %s", filename, e.message);
                return null;
            }
        }
        
        public PageConfig? get_page_by_id(string page_id) {
            foreach (var page in this.pages) {
                if (page.id == page_id) {
                    return page;
                }
            }
            return null;
        }
    }
}
