/**
 * File:
 *   include/restore/summary_dialog.ycp
 *
 * Package:
 *   Restore module
 *
 * Summary:
 *   Display summary dialog.
 *
 * Authors:
 *   Ladislav Slezak <lslezak@suse.cz>
 *
 * $Id: summary_dialog.ycp,v 1.14 2004/03/09 18:09:08 sh Exp $
 *
 * Display summary dialog in wizard with optional details. Summary can be saved to file.
 *
 */

{

textdomain "restore";

import "Wizard";

import "Popup";
import "Label";

/**
 * This function removes HTML tags from input string
 * @param input Input string
 * @return string String without tags
 */

define string RemoveTags(string input) ``{
    string ret = input;

    map<string,string> tagmapping = $[ "BR" : "\n", "/P" : "\n", "P" : "", "B" : "", "/B" : "",
	"EM" : "", "/EM" : "", "I" : "", "/I" : "", "TT" : "", "/TT" : "",
	"/BIG" : "", "BIG" : "", "CODE" : "/CODE", "STRONG" : "", "/STRONG" : "", "PRE" : "", "/PRE" : "",
	 "LARGE" : "", "/LARGE" : "", "HR" : "", "H1" : "", "/H1" : "", "H2" : "", "/H2" : "",
	 "H3" : "", "/H3" : ""
    ];

    foreach(string t, string repl, tagmapping, ``{
	    string tag = "<" + t + ">";

	    while (issubstring(ret, tag))
	    {
		ret = regexpsub(ret, "(.*)" + tag + "(.*)", "\\1" + repl + "\\2");
	    }

	    while (issubstring(ret, tolower(tag)))
	    {
		ret = regexpsub(ret, "(.*)" + tag + "(.*)", "\\1" + repl + "\\2");
	    }
	}
    );

    return ret;
}

/**
 * Display summary dialog with optional details, it is possible to save dialog contents to file
 * @param text Summary text
 * @param detail_text Detailed summary text
 * @param helptext Help text for wizard
 * @param label Text in label
 * @param button Label for `next button, possible values are `next (label is "Next"), `ok ("Ok") or `finish ("Finish")
 * @return symbol Id of pressed button (`next, `back, `abort)
 */

define symbol DisplaySummaryDialog(string text, string detail_text, string helptext, string label, symbol button) ``{

    term contents = `VBox(
	    `VSpacing(0.5),
	    `RichText(`id(`rt), text),
	    `VSpacing(0.5),
	    // push button label
	    `HBox(`CheckBox(`id(`details), `opt(`notify, `key_F2), _("&Show details"), false), `HSpacing(3),
		// push button label
		`PushButton(`id(`save), _("Sa&ve to file..."))),
	    `VSpacing(1.0)
	);

    if (button == `finish)
    {
	Wizard::SetNextButton(`next, Label::FinishButton() );
    }
    else if (button == `ok)
    {
	Wizard::SetNextButton(`next, Label::OKButton() );
    }
    else if (button == `next)
    {
	Wizard::RestoreNextButton();
    }
    else
    {
	y2warning("Unknown button: %1", button);
    }

    Wizard::SetContents(label, contents, helptext, true, true);

    any ret = nil;

    do
    {
	ret = UI::UserInput();

	boolean details = (boolean) UI::QueryWidget(`id(`details), `Value);

	if (ret == `details)
	{
	    UI::ChangeWidget(`id(`rt), `Value, (details == true) ? detail_text : text);
	}
	else if (ret == `save)
	{
	    string savefile = UI::AskForSaveFileName("/", "*", _("Save Summary to File"));

	    if (savefile != "" && savefile != nil)
	    {
		SCR::Write(.target.string, savefile, RemoveTags(detail_text));
		y2milestone("Summary saved to file: %1", savefile);
	    }
	}
	else if (ret == `cancel)
	{
	    ret = `abort;
	}
    }
    while(ret != `next && ret != `abort && ret != `back);


    Wizard::RestoreNextButton();

    return (symbol) ret;
}

}

