namespace QuickSetup {
    public class QsConfig {
        private Json.Node root;

        public QsConfig(string path) {
            var parser = new Json.Parser();

            try {
                parser.load_from_file(path);
            } catch (Error e) {
                print("Failed to load config: %s", path);
            }

            this.root = parser.get_root();
        }

        public QsConfig.from_json_string(string json_string) {
            var parser = new Json.Parser();

            try {
                parser.load_from_data(json_string);
            } catch (Error e) {
                print("Failed to load config from JSON string\n");
            }

            this.root = parser.get_root();
        }

        public string get_page_string(string page_id, string key, string default_value = "") {
            string translated_default = tr(default_value);
            if (this.root == null) {
                error("Config not loaded");
            }

            var root_object = this.root.get_object();
            if (root_object == null) {
                return translated_default;
            }

            if (!root_object.has_member(page_id)) {
                return translated_default;
            }

            var page_object = root_object.get_object_member(page_id);

            if (page_object == null || !(page_object.has_member(key))) {
                return translated_default;
            }

            if (page_object.has_member(key)) {
                return tr(page_object.get_string_member(key));
            }

            return translated_default;
        }

        public bool get_page_bool(string page_id, string key, bool default_value = false) {
            if (this.root == null) {
                error("Config not loaded");
            }

            var root_object = this.root.get_object();
            if (root_object == null) {
                return default_value;
            }

            if (!root_object.has_member(page_id)) {
                return default_value;
            }

            var page_object = root_object.get_object_member(page_id);

            if (page_object == null) {
                return default_value;
            }

            if (page_object.has_member(key)) {
                return page_object.get_boolean_member(key);
            }

            return default_value;
        }

        public List<string> get_page_string_array(string page_id, string key) {
            if (this.root == null) {
                error("Config not loaded");
            }

            var root_object = this.root.get_object();
            if (root_object == null) {
                return new List<string>();
            }
            
            if (!root_object.has_member(page_id)) {
                return new List<string>();
            }

            var page_object = root_object.get_object_member(page_id);

            if (page_object == null) {
                return new List<string>();
            }

            var array = page_object.get_array_member(key);

            if (array == null || !(page_object.has_member(key)) || !(array.get_length() > 0)) {
                return new List<string>();
            }

            // Iterate through the array to build a string list
            var string_list = new List<string>();

            for (var i = 0; i < array.get_length(); i++) {
                var element = array.get_string_element(i);
                string_list.append(element);
            }

            return string_list;
        }

        public Json.Object? get_page_object(string page_id) {
            if (this.root == null) {
                error("Config not loaded");
            }

            var root_object = this.root.get_object();
            if (root_object == null) {
                return null;
            }
            
            if (!root_object.has_member(page_id)) {
                return null;
            }

            var page_object = root_object.get_object_member(page_id);

            if (page_object == null) {
                return null;
            }

            return page_object;
        }
    }
}
