/**
 *
 * Authors:	Klaus Kämpf <kkaempf@suse.de>
 *		Stefan Hundhammer <sh@suse.de>
 *		Arvin Schnell <arvin@suse.de>
 *
 * Purpose:	Asks user to really do the installation/update.
 *
 * $Id: inst_doit.ycp 21862 2005-02-23 12:00:40Z jsrain $
 */
{
    textdomain "installation";

    import "Wizard";
    import "Mode";
    import "AutoinstConfig";

    import "Label";


    if (Mode::autoinst () && !AutoinstConfig::Confirm)
        return `next;


    define void ConfirmLicenses () {
	if (Mode::autoinst ())
	{
	    return;
	}
	list<string> to_install = Pkg::GetPackages (`selected, true);
	map<string,string> licenses = Pkg::PkgGetLicensesToConfirm (to_install);
	boolean deny_all = false;
	foreach (string package,  string license, licenses, {
	    if (deny_all)
	    {
		Pkg::PkgTaboo (package);
	    }
	    else
	    {
		term popup = `VBox (
		    `HSpacing (80),
		    // dialog heading, %1 is package name
		    `Heading (sformat (_("Confirm Package License: %1"),
			package)),
		    `HBox (
			`VSpacing (20),
			`RichText (`id (`lic), license)
		    ),
		    `HBox (
			`PushButton (`id (`help), Label::HelpButton ()),
			`HStretch (),
			// push button
			`PushButton (`id (`accept), _("I &Agree")),
			// push button
			`PushButton (`id (`deny), _("I &Disagree")),
			// push button
			`PushButton (`id (`deny_all), _("I D&isagree with All"))
		    )
		);
		UI::OpenDialog (popup);
		symbol ret = nil;
		while (ret == nil)
		{
		    ret = (symbol)UI::UserInput ();
		    if (ret == `help)
		    {
			ret = nil;
			// help text
			string help = _("<p><b><big>License Confirmation</big></b><br>
The package in the headline of the dialog requires an explicit confirmation
of acceptance of its license.
If you reject the license of the package, the package will not be installed.
<br>
To accept the license of the package, click <b>I Agree</b>.
To reject the license of the package, click <b>I Disagree</b>.
<br>
To reject installation of all packages requiring confirmation of
their licenses, click <b>I Disagree with All</b>.</p>");
			UI::OpenDialog (`HBox (
			    `VSpacing (18),
			    `VBox (
				`HSpacing (70),
				`RichText (help),
				`HBox (
				    `HStretch (),
				    // push button
				    `PushButton (`id (`close), Label::CloseButton ()),
				    `HStretch ()
				)
			    )
			));
			UI::UserInput ();
			UI::CloseDialog ();
		    }
		}
		UI::CloseDialog ();
		if (ret != `accept)
		{
		    if (ret == `deny_all)
		    {
			deny_all = true;
		    }
		    Pkg::PkgTaboo (package);
		}
		else
		{
		    Pkg::PkgMarkLicenseConfirmed (package);
		}
	    }
	});
    }


    /**
     * Confirm installation or update.
     * Returns 'true' if the user confirms, 'false' otherwise.
     **/
    boolean confirmInstallation()
    {
	// Heading for confirmation popup before the installation really starts
	string heading = "<h3>" + _("Confirm Installation") + "</h3>";

	string body =
	// Text for confirmation popup before the installation really starts 1/3
	    _("<p>All information required for the base installation is now complete.</p>")
	// Text for confirmation popup before the installation really starts 2/3
	    + _("
<p>If you continue now, partitions on your hard disk will be <b>formatted</b>
(erasing any existing data in those partitions) 
and <b>data will be overwritten</b>
according to the installation settings in the previous dialogs.</p>")
	// Text for confirmation popup before the installation really starts 3/3
	    + _("<p>Go back and check the settings if you are unsure.</p>")
	    ;

	string confirm_button_label = Label::InstallButton ();

	if ( Mode::update () )
	{
	    // Heading for confirmation popup before the update really starts
	    heading = "<h3>" + _("Confirm Update") + "</h3>";

	    body =
	    // Text for confirmation popup before the update really starts 1/3
		_("<p>All information required to perform an update is now complete.</p>")
	    // Text for confirmation popup before the update really starts 2/3
		+ _("
<p>If you continue now, data on your hard disk will be overwritten 
according to the settings in the previous dialogs.</p>")
	    // Text for confirmation popup before the update really starts 3/3
		+ _("<p>Go back and check the settings if you are unsure.</p>")
		;

	    // Label for the button that confirms startint the installation
	    confirm_button_label = _("Start &Update");
	}

	string heading_bg_color = "#A9CEDD";

	map display_info = UI::GetDisplayInfo();
	string text = display_info[ "RichTextSupportsTable" ]:false ?
	    sformat( "<table bgcolor=\"%1\"><tr><td>%2</td></tr></table>%3",
		     heading_bg_color, heading, body )
	    : ( heading + body );



	UI::OpenDialog(
		       `VBox(
			     `VSpacing( 0.4 ),
			     `HSpacing( 70 ),		// force width
			     `HBox(
				   `HSpacing( 0.7 ),
				   `VSpacing( 18 ),	// force height
				   `RichText( text ),
				   `HSpacing( 0.7 )
				   ),
			     `HBox(
				   `HStretch(),
				   `PushButton(`id(`back), `opt(`default), Label::BackButton() ),
				   `HStretch(),
				   `PushButton(`id(`confirm), confirm_button_label ),
				   `HStretch()
				   )
			     )
		       );

	symbol button = (symbol) UI::UserInput();
	UI::CloseDialog();

	return ( button == `confirm );
    }



    //
    // main()
    //

    ConfirmLicenses ();
    boolean confirmed = confirmInstallation();

    if ( confirmed )
	y2milestone( "User confirmed %1", Mode::update () ? "update" : "installation" );

    return confirmed ? `next : `back;
}
