/**
 * File:		language_proposal.ycp
 *
 * $Id: language_proposal.ycp 21221 2005-02-04 14:28:06Z jsuchome $
 *
 * Author:		Klaus Kaempf <kkaempf@suse.de>
 *
 * Purpose:		Proposal function dispatcher - language.
 *
 *			See also file proposal-API.txt for details.
 */
{
    textdomain "country";

    import "Language";
    import "Wizard";
    import "Encoding";
    import "Console";

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

    // Memorize current language to be able to detect a change.
    //
    string language_on_entry = Language::language;

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

	// Make proposal and fill return map
	list<string> prop =
	    Language::MakeProposal (force_reset, language_changed);

	y2milestone ("language_on_entry:%1 lang:%2, languages: %3",
	    language_on_entry, Language::language, Language::languages);

	if (force_reset && language_on_entry != Language::language)
	{
	    // Set it in YaST2
	    Language::WfmSetLanguage();
	}

	ret = $[
	      "raw_proposal"    : prop,
	      "language_changed": (language_on_entry != Language::language )
	];
    }
    else if ( func == "AskUser" )
    {
	boolean has_next = param["has_next"]:false;

	// Special case for language selection: Rebuild dialog completely upon
	// language change (to get even the wizard widgets re-translated), thus
	// the loop around that 'CallFunction()'.

	symbol result = nil;

	// THIS IS VERY VERY UGLY:
	// Because of the while-loop restoring in case of cancel must be
	// done here (outside the loop). :-((((
	// Remember the current language setting to be able to restore it.
	//
	string language_on_entry = Language::language;

	y2milestone("language_on_entry - 1: %1", language_on_entry );

	Wizard::OpenAcceptDialog();	// Reduce flicker (yes, it's a kludge)
	do
	{
	    Wizard::OpenAcceptDialog();	// This dialog will actually be used

            map args = $[];
            args["enable_back"] = true;
            args["enable_next"] = has_next;
	    result = (symbol)
		WFM::CallFunction ("inst_language", [args]);
	    Wizard::CloseDialog();

	} while (result == `again);

	Wizard::CloseDialog();		// Close superfluous extra dialog

	// SECOND PART OF UGLINESS :-(((
	// Back to original values.
	//
	if ( result == `back )
	{
	    import "Console";
	    import "Installation";

	    boolean use_utf8 = true;		// utf8 is default

	    map display_info    = UI::GetDisplayInfo ();
	    if (! display_info["HasFullUtf8Support"]:true)
	    {
		use_utf8 = false;		// fallback to ascii
	    }

	    y2milestone("language_on_entry - 2: %1", language_on_entry );

	    // Set it in the Language module.
	    //
	    Language::Set( language_on_entry );

	    // Set Console font
	    //
	    Console::SelectFont( language_on_entry );

	    string enc = (use_utf8) ? "UTF-8" : Encoding::console;

	    // Set it in YaST2
	    //
	    UI::SetLanguage (language_on_entry, enc);
	    WFM::SetLanguage(language_on_entry, "UTF-8");
	}

	// Fill return map

	ret =
	    $[
	      "workflow_sequence" : result,
	      "language_changed": (language_on_entry != Language::language)
	    ];

	y2debug( "Returning from proposal_language::AskUser() with: %1", ret );

    }
    else if ( func == "Description" )
    {
	// Fill return map.
	//
	// Static values do just nicely here, no need to call a function.

	ret =
	    $[
	      // label text
	      "rich_text_title"	:	_( "Language"  ),
	      // menue label text
	      "menu_title"	:	_( "&Language" ),
	      "id"		:	"language_stuff"
	    ];
    }

    return ret;
}
