/* ontology.vala
 *
 * Copyright © 2011 Collabora Ltd.
 *             By Seif Lotfy <seif@lotfy.com>
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * Get information about interpretation/manifestation symbols
 */

namespace Zeitgeist
{

    namespace Symbol
    {
        private static HashTable<string, Info> all_symbols = null;
        private static bool initialized = false;

        public static unowned string get_display_name (string symbol_uri)
        {
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return symbol_uri;

            return symbol.display_name;
        }

        public static unowned string get_description (string symbol_uri)
        {
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return "";

            return symbol.description;
        }

        public static List<unowned string> get_all_parents (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.parents)
            {
                results.append (uri);
                // Recursively get the other parents
                foreach (unowned string parent_uri in get_all_parents (uri))
                    if (results.index (parent_uri) == -1)
                        results.append (parent_uri);
            }

            return results;
        }

        public static List<unowned string> get_all_children (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.all_children)
                results.append (uri);

            return results;
        }

        public static List<unowned string> get_children (string symbol_uri)
        {
            initialize_symbols ();
            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.children)
                results.append(uri);

            return results;
        }

        public static List<unowned string> get_parents (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string>();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.parents)
                results.append (uri);

            return results;
        }

        public static bool is_a (string? symbol_uri, string? parent_uri)
        {
            if (parent_uri == null || symbol_uri == null) return false;
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return false;
            if (parent_uri == symbol_uri) return true;

            foreach (unowned string uri in get_all_parents (symbol_uri))
                if (parent_uri == uri)
                    return true;
            return false;
        }

        private static void initialize_symbols ()
        {
            if (initialized) return;
            initialized = true;
            // *insert-auto-generated-code*
        }

    }

    private class Symbol.Info
    {
        public List<string> parents;
        public List<string> children;
        public List<string> all_children;
        public string uri;
        public string display_name;
        public string description;

        private Info (string uri, string display_name, string description,
            string[] parents, string[] children, string[] all_children)
        {
            this.uri = uri;
            this.display_name = display_name;
            this.description = description;
            this.parents = new List<string> ();
            for (int i = 0; i < parents.length; i++)
                this.parents.append (parents[i]);
            this.children = new List<string> ();
            for (int i = 0; i < children.length; i++)
                this.children.append (children[i]);
            this.all_children = new List<string> ();
            for (int i = 0; i < all_children.length; i++)
                this.all_children.append (all_children[i]);
        }

        internal static void register (string uri, string display_name,
            string description, string[] parents, string[] children,
            string[] all_children)
        {
            if (all_symbols == null)
                all_symbols = new HashTable<string, Info> (str_hash, str_equal);
            Info symbol = new Info (uri, display_name, description,
                parents, children, all_children);
            all_symbols.insert (uri, symbol);
        }

    }

}

// vim:expandtab:ts=4:sw=4
