/*
 * pxio.c
 *
 * Copyright (C) 2007  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * 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 General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVB_CONFIG_H
#include <config.h>
#endif

#include <pslib.h>

#include "pxtools.h"
#include "pxtree.h"

static void pxNodeFree(pxNode *node)
{
    psFree(node->name);
    psFree(node->parent);
    psFree(node->children);
}

pxNode *pxNodeAlloc(const char *name, pxNode *parent)
{
    PS_ASSERT_PTR_NON_NULL(name, NULL);

    pxNode *node = psAlloc(sizeof(pxNode));

    node->name = psStringCopy(name);

    // parent == NULL basically means that this is the root node
    if (!parent) {
        node->parent = NULL;
    } else {
        // add this node to it's parent's children list
        psListAdd(parent->children, PS_LIST_TAIL, node);
        node->parent = psMemIncrRefCounter(parent);
    }

    node->children = psListAlloc(NULL);

    psMemSetDeallocator(node, (psFreeFunc) pxNodeFree);

    return node;
}


void pxNodePrint(FILE *stream, pxNode *node)
{
    fprintf(stream, "node name: %s\n", node->name);
    fprintf(stream, "node parent's name: %s\n", node->parent
                                              ?  node->parent->name
                                              : NULL);
    psListIterator *iter = psListIteratorAlloc(node->children, 0, false);                                          
    pxNode *child = NULL;
    while ((child = psListGetAndIncrement(iter))) {
        fprintf(stream, "node child's name: %s\n", child->name);
    }
    psFree(iter);
}

// func() returning false means decend no futher along this branch

bool pxTreeCrawl(pxNode *node, pxNodeFunc func, void *arg)
{
    // if func() returns false, stop
    if (!func(arg, node)) {
        return false;
    }

    psListIterator *iter = psListIteratorAlloc(node->children, 0, false);                                          
    pxNode *child = NULL;
    while ((child = psListGetAndIncrement(iter))) {
        pxTreeCrawl(child, func, arg);
    }
    psFree(iter);

    return true;
}

bool pxNodeFuncCountDependants(void *arg, pxNode *node)
{
    (*(int *)arg)++;
    return true;
}

bool pxNodeHasChildren(pxNode *node)
{
    int children = psListLength(node->children);

    return children ? true : false;
}

bool pxNodeHasGrandChildren(pxNode *node)
{
    // find out how many nodes there are lower in the tree
    // subtract the parent node from the count so we have just a tally of
    // decendants
    int nodes = -1;
    pxTreeCrawl(node, pxNodeFuncCountDependants, &nodes);
    psTrace("pxtree", PS_LOG_INFO, "node %s has %d dependants", node->name, nodes);

    // find out how many children this node has
    int children = psListLength(node->children);
    psTrace("pxtree", PS_LOG_INFO, "node %s has %d children", node->name, children);

    if (!children) {
        // no children
        return false;
    }

    if (nodes < children) {
        psAbort("you can't have fewer children than decendants!");
    }

    if (nodes == children) {
        // no grandchildren
        return false;
    }

    return true;
}

pxNode *pxTreeFromMetadata(psMetadata *md)
{
    psHash *nodeNames = psHashAlloc(10);

    psMetadataIterator *iter = psMetadataIteratorAlloc(md, 0, NULL);
    psMetadataItem *item = NULL;
    while ((item = psMetadataGetAndIncrement(iter))) {
        if (!(item->type == PS_DATA_STRING)) {
            continue;
        }
        pxNode *parent = psHashLookup(nodeNames, item->data.str);
        pxNode *node = pxNodeAlloc(item->name, parent);
        psHashAdd(nodeNames, item->name, node);
        psFree(node);
    }
    psFree(iter);

    pxNode *root = psHashLookup(nodeNames, "root");
    psFree(nodeNames);

    return root;
}
