/**
 * File:	clients/slp.ycp
 * Package:	SLP
 * Summary:	SLP Browser
 * Authors:	Anas Nashif <nashif@suse.de>
 *
 * $Id: slp.ycp,v 1.11 2004/06/14 12:32:04 nashif Exp $
 *
 * Browse SLP services
 */
{
    import "SLP";
    textdomain "slp";
    import "Wizard";
    import "Label";

    define term createTableItem(map srv) ``{

        term tabitem = `item();

        string srvurl = srv["srvurl"]:"";

        tabitem = `item(
                `id( srvurl),
                substring(srv["pcSrvType"]:"", 8 ),
                substring(srv["srvurl"]:"", 8 ),
                srv["lifetime"]:0
                );
        return tabitem;
    }

    /**
     * Process Tree Items
     */
    define list<map> createTreeItem(list<map> Tree, string srvType, list<string> Sub) ``{


        Tree = Wizard::AddTreeItem(Tree, "", srvType, "service:" + srvType );
        Sub = filter(string s, Sub, ``(s!=""));
        foreach(string s, Sub, ``{
                term si = `item();
                string Id = "service:" + srvType + ":" + s;
                Tree =Wizard::AddTreeItem(Tree, "service:" + srvType, s , Id);
                });

        return Tree;
    }

    define list fillTable(list<map> response) ``{
        list items = maplist(map srv, response, ``{
                return(createTableItem(srv));
                });

        return items;
    }

    define map<string, list<string> > processTree(list<string> typeResponse ) ``{
        map<string, list<string> > treeData = $[];
        foreach(string t, typeResponse, ``{
                list<string> tok = splitstring(t, ":");
                list<string> s = [];
                s=add(s, tok[2]:"" );
                string t1 = tok[1]:"";
                if (!haskey(treeData, t1) )
                {
                    treeData[t1] = s ;
                }
                else
                {
                    list old = treeData[t1]:[];
                    treeData[t1] = (list<string>)union(s,old) ;
                }
                });
        return treeData;
    }


    define list<map> fillTree(list<string> typeResponse) ``{
        list<map> Tree = [];
        map<string, list<string> > data = processTree(typeResponse);
        foreach(string type, list<string> sub,  data, ``{
                Tree = createTreeItem(Tree, type, sub);
                });

        return Tree;
    }


    /*
    list<map> response = SLP::FindSrvs("CIM-Object-Manager");
    */
    list tableItems = [];
    list treeItems = [];

    term contents =
        `Top(
                `VBox(
                    `Table( `id( `table ), `opt( `notify,  `immediate),
                        `header( _("Type"), _("URL"), _("Lifetime") ),
                        tableItems ),
                    `RichText(`id(`attr),  "" )
                    )
            );

    list<string> typeResponse = SLP::FindSrvTypes("*", "");

    string title = _("SLP Browser");
    Wizard::CreateTreeDialog();
    Wizard::SetDesktopIcon("slp");
    list<map> Tree = fillTree(typeResponse);
    y2debug("Tree=%1", Tree);
    Wizard::CreateTree(Tree, _("Service Types") );

    string help = dgettext("base", "No help available");
    Wizard::SetContentsButtons(title, contents, help, Label::BackButton(),
            Label::FinishButton());

    Wizard::HideAbortButton();
    Wizard::DisableBackButton();



    any input = nil;
    map cache = $[];
    map attrcache = $[];
    repeat {

        string srvtype = "";
        string srv = "";
        map event = UI::WaitForEvent();
        input = event["ID"]:nil;
        if (input == `wizardTree)
            input = UI::QueryWidget(`id(`wizardTree), `CurrentItem);

        y2debug("input: %1", input);
        if (is(input, string))
        {
            srvtype = Wizard::QueryTreeItem();
        }
        else if (input == `table )
        {
            srv = (string) UI::QueryWidget(`id(`table), `CurrentItem);
        }
        y2debug("srvtype: %1", srvtype );
        y2debug("srv: %1", srv );
        list<map> srvsResponse = [];
        if(haskey(cache, srvtype))
        {
            srvsResponse = cache[srvtype]:[];
        }
        else
        {
            srvsResponse = SLP::FindSrvs(srvtype , "");
            cache[srvtype] = srvsResponse;
        }
        tableItems = fillTable(srvsResponse);

        list<string> attr = [];
        string sum = "";

        foreach(map s , srvsResponse, ``{
                string srvurl = s["srvurl"]:"";
                if(haskey(attrcache, srvurl))
                {
                attr = attrcache[srvurl]:[];
                }
                else
                {
                y2debug("s: %1", s);
                attr = SLP::FindAttrs(srvurl);
                y2debug("attr: %1", attr);
                attrcache[srvurl] = attr;
                }
                });

        if (is(input, string))
        {
            UI::ChangeWidget(`id(`table), `Items, tableItems);
            srv = srvsResponse[0,"srvurl"]:"xxx";
            sum = SLP::AttrSummary(attrcache[srv]:[]);
            UI::ChangeWidget(`id(`attr), `Value, sum);
        }
        else if (input == `table )
        {
            sum = SLP::AttrSummary(attrcache[srv]:[]);
            UI::ChangeWidget(`id(`attr), `Value, sum);
        }
    } until (input == `next || input == `abort || input == `cancel);

    UI::CloseDialog();
    return input;
    // EOF
}
