/**
 * Module:	rootpart_proposal.ycp
 *
 * Author:	Arvin Schnell <arvin@suse.de>
 *
 * Purpose:	Let user choose root partition during update.
 */
{
    textdomain "update";

    import "HTML";
    import "Mode";
    import "Linuxrc";
    import "Update";
    import "PackageCallbacks";
    import "RootPart";

    string func  = (string) WFM::Args(0);
    map    param = (map) WFM::Args(1);
    map    ret   = $[];


    define boolean mount_target ()
    {
	UI::OpenDialog (`opt(`decorated ),
			// intermediate popup while mounting partitions
			`Label(_("Mounting partitions. One moment please...")));

	boolean tmp = RootPart::MountPartitions (RootPart::selectedRootPartition);
	sleep (500);

	UI::CloseDialog ();

	return tmp;
    }


    if ( func == "MakeProposal" )
    {
	boolean force_reset      = param["force_reset"     ]:false;
	boolean language_changed = param["language_changed"]:false;

	PackageCallbacks::SetRebuildDBCallbacks ();

	// call some function that makes a proposal here:
	//
	// DummyMod::MakeProposal( force_reset );

	// Fill return map

	if (!RootPart::didSearchForRootPartitions)
	{
	    UI::OpenDialog (`opt(`decorated ),
			    // label
			    `Label(_("Evaluating root partition. One moment please...")));

	    RootPart::FindRootPartitions ();

	    UI::CloseDialog ();

	    RootPart::selectedRootPartition = "";
	}

	if (force_reset)
	{
	    RootPart::selectedRootPartition = "";
	}

	if (RootPart::numberOfValidRootPartitions == 0
	    && RootPart::selectedRootPartition == "")
	{
	    RootPart::targetOk = false;

	    ret = $[ "warning" :
		     // Proposal for system to update
		     _("No root partition found"),
		     "warning_level" : `fatal,
		     "raw_proposal" : []
	    ];
	}
	else
	{
	    if (RootPart::selectedRootPartition == "")
	    {
		if (RootPart::numberOfValidRootPartitions == 1 && !Linuxrc::manual ())
		{
		    RootPart::SetSelectedToValid ();
		}
		else
		{
		    symbol result = (symbol) WFM::CallFunction ("inst_rootpart", [true, true, `update_popup]);
		}

		RootPart::targetOk = mount_target ();
	    }

	    if (RootPart::numberOfValidRootPartitions == 1)
	    {
		ret = $[ "raw_proposal" :
			 [ RootPart::GetInfoOfSelected (`name) ]
		];
	    }
	    else
	    {
		ret = $[ "raw_proposal" :
			 // Proposal for system to update
			 [ sformat (_("%1 on root partition %2"),
				    RootPart::GetInfoOfSelected (`name),
				    RootPart::selectedRootPartition) ]
		];
	    }

	    if (!RootPart::targetOk)
	    {
		// inform user in the proposal about the failed mount
		ret = add (ret, "warning", _("Failed to mount target system"));
		ret = add (ret, "warning_level", `fatal);
		ret = add (ret, "raw_proposal", []);
	    }
	}
    }
    else if ( func == "AskUser" )
    {
	boolean has_next = param["has_next"]:false;

	// call some function that displays a user dialog
	// or a sequence of dialogs here:
	//
	// sequence = DummyMod::AskUser( has_next );

	string tmp = RootPart::selectedRootPartition;

	symbol result = (symbol) WFM::CallFunction ("inst_rootpart", [true, has_next, `update_dialog]);

	if (result == `next)
	{
	    Update::Detach ();
	    RootPart::UnmountPartitions (false);

	    RootPart::targetOk = mount_target ();
	}

	// Fill return map

	ret = $[ "workflow_sequence" : result,
		 "rootpart_changed" : RootPart::selectedRootPartition != tmp ];
    }
    else if ( func == "Description" )
    {
	// Fill return map.

	if (Mode::normal ())
	{
	    ret = $[];
	}
	else
	{
	    ret = $[
		    // this is a heading
		    "rich_text_title"	:	_("Selected for Update"),
		    // this is a menu entry
		    "menu_title"	:	_("&Selected for Update"),
		    "id"		:	"rootpart_stuff"
	    ];
	}
    }

    return ret;
}
