/**
 * Module:	inst_rootpart.ycp
 *
 * Authors:	Stefan Schubert <schubi@suse.de>
 *		Arvin Schnell <arvin@suse.de>
 *
 * Purpose:	Select root partition for update or booting.
 *		RootPart::rootPartitions must be filled before
 *		calling this module.
 */
{
    textdomain "update";

    import "Wizard";
    import "Popup";
    import "Label";
    import "RootPart";


    // This dialog comes in three different flavors: `update_dialog,
    // `update_popup and `boot_popup
    symbol flavor = (symbol) WFM::Args (2);


    list partition_list = [];

    define void make_partition_list (boolean withall)
    {
	partition_list = [];
	foreach (string p, map i, RootPart::rootPartitions, {
	    if (i[`valid]:false || withall)
	    {
		string s = i[`name]:"error";
		if (s == "unknown") s = _("Unknown");

		string a = i[`arch]:"error";
		if (a == "unknown") a = _("Unknown");

		partition_list = add (partition_list, `item (`id (p), s, p, a));
	    }
	});
    }


    make_partition_list (RootPart::showAllPartitions);


    string title = "";
    string label = "";
    string help_text = "";

    if (flavor == `boot_popup)
    {
	// label for selection of root partition (for boot)
	label = _("Partition or System to Boot:");

	// help text for root partition dialog (for boot)
	help_text = _("<p>
Select the partition or system to boot.
</p>
");
    }
    else
    {
	// label for selection of root partition (for update)
	label = _("Partition or System to Update:");

	// help text for root partition dialog (for update)
	help_text = _("<p>
Select the partition or system to update.
</p>
");

	if (flavor == `update_dialog)
	{
	    // headline for dialog "Select for update"
	    title = _("Select for Update");
	}
    }

    // help text for root partition dialog (general part)
    help_text = help_text + _("<p>
<b>Show All Partitions</b> expands the list to a
general overview of your system's partitions.
</p>
");

    term contents =
	`VBox (
	       // heading for 'select root partiton' dialogue
	       `HCenter (
			 `HSquash (
				   `VBox (
					  `HSpacing( 40 ),	// force minimum width
					  `Left (`Label (label)),
					  `Table (`id (`partition),
								// table header
						  `header (_("System"),
								// table header
							   _("Partition"),
								// table header
							   _("Architecture")),
						  partition_list),
					  `Left(`CheckBox(`id(`showall), `opt(`notify),
							// check box
							  _("Show All Partitions"), false))
					  )
				   )
			 ),
	       `VSpacing (2)
	       );


    if (flavor == `update_dialog)
    {
	Wizard::OpenAcceptDialog ();
	Wizard::SetContents (title, contents, help_text, (boolean) WFM::Args(0), (boolean) WFM::Args(1));
    }
    else
    {
	term buttons = `PushButton (`id(`next), `opt(`default), Label::OKButton());

	if (flavor == `boot_popup)
	{
	    buttons = `HBox (
			     // pushbutton to (rightaway) boot the system selected above
			     `HWeight( 1, `PushButton(`id(`next), `opt(`default), _("Boot")) ),
			     `HSpacing( 1 ),
			     `HWeight( 1, `PushButton(`id(`cancel), Label::CancelButton() ) )
			     );
	}

	term full = `HBox (
			   `VSpacing( 16 ),		// force dialog height
			   `VBox(
				 `HSpacing( 30 ),	// force help text width
				 `RichText( help_text )
				 ),
			   `HSpacing( 3 ),
			   `VBox(
				 `VSpacing( 1 ),
				 contents,
				 buttons
				 ),
			   `HSpacing( 3 )
			   );

	UI::OpenDialog (full);
    }


    if (size (RootPart::selectedRootPartition) > 0)
	UI::ChangeWidget (`id(`partition), `CurrentItem, RootPart::selectedRootPartition);

    UI::ChangeWidget(`id(`showall), `Value, RootPart::showAllPartitions);


    any ret = nil;

    while (true)
    {
	if (flavor == `update_dialog)
	    ret = Wizard::UserInput ();
	else
	    ret = UI::UserInput ();

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

	if (ret == `showall)
	{
	    string tmp = (string) UI::QueryWidget (`id(`partition), `CurrentItem);
	    make_partition_list ((boolean) UI::QueryWidget (`id(`showall), `Value));
	    UI::ChangeWidget (`id(`partition), `Items, partition_list);
	    if (tmp != nil)
		UI::ChangeWidget (`id(`partition), `CurrentItem, tmp);
	    continue;
	}
	if ((flavor == `update_dialog || flavor == `update_popup)
	    && ret == `next)
	{
	    string selected = (string) UI::QueryWidget (`id(`partition), `CurrentItem);
	    map freshman = RootPart::rootPartitions[selected]:$[];
	    boolean cont = true;
	    y2milestone ("Selected root partition: %1 %2",
		selected, freshman);
	    if (freshman[`name]:"unknown" == "unknown")
	    {
		cont = Popup::ContinueCancel (
// continue-cancel popup
_("No installed system that can be upgraded with this product was found
on the selected partition."));
	    }
	    else if (freshman[`arch]:"" != RootPart::GetDistroArch ())
	    {
		cont = Popup::ContinueCancel (
// continue-cancel popup
_("The architecture of the system installed in the selected partition
is different than the one of this product."));

	    }
	    if (! cont)
		ret = nil;
	}

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

    if (ret == `next)
    {
	RootPart::selectedRootPartition = (string) UI::QueryWidget (`id(`partition), `CurrentItem);
	RootPart::showAllPartitions = (boolean) UI::QueryWidget (`id(`showall), `Value);
    }

    if (flavor == `update_dialog)
	Wizard::CloseDialog ();
    else
	UI::CloseDialog ();

    return ret;
}
