/**
 * Module: 		inst_update.ycp
 *
 * Authors:		Stefan Schubert <schubi@suse.de>
 *			Arvin Schnell <arvin@suse.de>
 *
 * Purpose:
 * Displays software selection screen of previous installed software-groups.
 * Show checkboxes for software categories. Let the user select his software.
 * if he want to UPGRADE his system.
 */

{
    textdomain "update";

    import "Mode";
    import "Installation";
    import "Language";
    import "Wizard";
    import "Popup";
    import "RootPart";
    import "Update";
    import "Packages";


    // screen title for update options
    string title = _("Update Options");

    // get available base selection sorted by reverse order (highest ordered base selection first)
    list<string> available_base_selections = Update::GetBaseSelections ();
    y2milestone ("available_base_selections %1, current_selection %2", available_base_selections, Update::selected_selection);

    // create box with base selections, check if current selection really is available
    term baseconfsbox = `VBox ();

    // loop through available base selections, create radio button entry for every base selection
    // preselect current selection

    foreach (string selection, available_base_selections, {
	map selection_data = Pkg::SelectionData (selection);
	string selection_summary = selection_data["summary"]:"";
	boolean preselect = (selection == Update::selected_selection);
	baseconfsbox = add (baseconfsbox, `Left(`RadioButton(`id(selection),
							     `opt(`notify, `autoShortcut),
							     selection_summary,
							     preselect)));
    });

    // Checking: already selected addons or single selection?

    string wrn_msg = "";

    if (Pkg::RestoreState (true))		// check if state changed
    {
	// Display warning message
	wrn_msg = _("\
You have already chosen software from \"Detailed selection\".\n\
You will lose that selection if you change the basic selection.");
    }

    // Build and show dialog

    string from_version = RootPart::GetInfoOfSelected (`name);
    string to_version = Update::updateVersion["nameandversion"]:_("Unknown");

    term contents = `HVSquash(
	`VBox(
	      // label showing from what version to what version we are updating
	      `Left (`Label (sformat (_("Update from %1 to %2"), from_version, to_version))),
	      `VSpacing (1),
	      // frame title for update selection
	      `Frame( _("Update Mode"),
		      `VBox(
			    `VSpacing(1),
			    `RadioButtonGroup( `id(`bgoup), `opt(`notify),
					       `VBox(
						     `Left(`RadioButton(`id(`upgrade),`opt(`notify),
									// radio button label for update including new packages
									_("&Update with Installation of New Software and Features
Based on the Selection:
"), false)),
						     `VSpacing(0.5),
						     `HBox(
							   `HSpacing(4),
							   `RadioButtonGroup(`id(`baseconf), baseconfsbox)
							   ),
						     `VSpacing(1.5),
						     `Left(`RadioButton(`id(`notupgrade),`opt(`notify),
									// radio button label for update of already installed packages only
									_("Only U&pdate Installed Packages"), true)),
						     `VSpacing(1.0)
						     )
					       )
			    )
		      ),
	      `VSpacing (1),
	      `Left(`CheckBox(`id(`delete),
			      // check box label
			      // translator: add a & shortcut
			      _("&Delete Unmaintained Packages"), true)),
	      `VSpacing(1),
	      `Label( `id(`wrn_label), wrn_msg )
	      )
	);


    // help text for dialog "update options" 1/4
    string helptext = _("<p>The update option differs between two modes. In
either case, it is recommended to make a backup of your personal data.</p>
");

    // help text for dialog "update options" 2/4
    helptext = helptext + _("<p><b>With New Software:</b> This default setting
updates the existing software and installs all new features and benefits of
the new &product; version. The selection is based on the former predefined
software selection.</p>
");

    // help text for dialog "update options" 3/4
    helptext = helptext + _("<p><b>Only Installed Packages:</b> This selection
only updates the packages already installed on your system. <i>Note:</i>
New software in the predefined software selection, such as new YaST modules, is
not available after the update. You might miss advertised features.</p>
");

    // help text for dialog "update options" 4/4
    helptext = helptext + _("<p>After the update, some software might not
function anymore. Activate <b>Delete Unmaintained Packages</b> to delete those
packages during the update.</p>
");


    Wizard::OpenAcceptDialog ();

    Wizard::SetContents (title, contents, helptext, (boolean) WFM::Args(0), (boolean) WFM::Args(1));

    // preset update/upgrade radio buttons properly

    UI::ChangeWidget(`id(`upgrade), `Value, !Update::onlyUpdateInstalled);
    UI::ChangeWidget(`id(`notupgrade), `Value, Update::onlyUpdateInstalled);

    // grey out radio buttons if only already installed packages should be updated

    foreach (string sel, available_base_selections, {
	UI::ChangeWidget(`id(sel), `Enabled, !Update::onlyUpdateInstalled);
    });

    UI::ChangeWidget(`id(`delete), `Value, Update::deleteOldPackages);

    any ret = nil;

    while (true)
    {
	ret = Wizard::UserInput ();

	if (ret == `abort && Popup::ConfirmAbort (`painless))
	    break;

	if (ret == `upgrade || ret == `notupgrade)
	{
	    boolean tmp = (boolean) UI::QueryWidget (`id(`upgrade), `Value);
	    foreach (string sel, available_base_selections, {
		UI::ChangeWidget(`id(sel), `Enabled, tmp);
	    });

	    continue;
	}

	if (ret == `next && Pkg::RestoreState (true))
	{
	    boolean something_changed = false;

	    if (UI::QueryWidget (`id(`notupgrade), `Value) != Update::onlyUpdateInstalled)
		something_changed = true;

	    if ((boolean) UI::QueryWidget (`id(`upgrade), `Value) &&
		Update::selected_selection != UI::QueryWidget(`id(`baseconf), `CurrentButton))
		something_changed = true;

	    if (UI::QueryWidget (`id(`delete), `Value ) != Update::deleteOldPackages)
		something_changed = true;

	    if (something_changed)
	    {
		// yes/no question
		if (!Popup::YesNo (_("Do you really want\nto reset your detailed selection?")))
		    continue;
	    }
	}

	if (ret == `back || ret == `next)
	    break;
    }

    if (ret == `next)
    {
	Update::did_init1 = false;

	boolean b1 = Update::onlyUpdateInstalled;
	string s1 = Update::selected_selection;

	Update::onlyUpdateInstalled = (boolean) UI::QueryWidget (`id(`notupgrade), `Value);
	Update::deleteOldPackages = (boolean) UI::QueryWidget (`id(`delete), `Value);
	Update::selected_selection = (string) UI::QueryWidget(`id(`baseconf), `CurrentButton);

	if (b1 != Update::onlyUpdateInstalled || s1 != Update::selected_selection)
	{
	    Update::manual_interaction = true;
	}
    }

    Wizard::CloseDialog ();

    return ret;
}
