/**
 * File:
 *   sound_complex
 *
 * Module:
 *   Sound
 *
 * Summary:
 *   sound complex dialog
 *
 * Authors:
 * Dan Vesely <dan@suse.cz>
 * Dan Meszaros <dmeszar@suse.cz>
 * Ladislav Slezak <lslezak@suse.cz>
 *
 * String corrections by Christian Steinruecken <cstein@suse.de>, 2001/08/01
 *
 *
 * $Id: complex.ycp 20357 2004-11-18 13:37:56Z lslezak $
 *
 * Params: 1st ... list of sound_cards
 *         2nd ... list with save_info
 *         3rd ... list with configured_unique
 */

{

    textdomain "sound";

    import "Wizard";
    import "Label";
    import "Popup";
    import "Mode";

    import "Sound";
    include "sound/mixer.ycp";
    include "sound/options.ycp"; // sound_options

    /**
     * creates summary table with card labels and thier states
     * (running/not running/dissappeared)
     * @return list table contents
     */
    define list createTable() ``{
	list runn   = get_running_cards();
	integer pos = 0;
	list ret    = maplist(map card, Sound::modules_conf,
	``{
	    string state = "";
	    if (Sound::use_alsa)
	    {
		if (size(filter(map f, (list<map>) runn, ``(f["number"]:-1 == pos))) !=0 )
		{
		    // to translators: "(The card is) running
		    state = _("Running");
		}
	    }
	    else
	    {
		string mname = card["module"]:"";
		if( haskey ((map)SCR::Read(.proc.modules), mname) )
		{
		    // to translators: "(The card is) running
		    state = _("Running");
		}
	    }

	    if (state == "")
	    {
		if (card["unique_key"]:"" == isa_uniq())
		{
		    // status of cound card
		    state = _("Not running");
		}
		else
		// card was autodetected
		{
		    list audio = Sound::detected_cards;
		    audio = filter(map g, (list<map>) audio,
			``(g["unique_key"]:"" == card["unique_key"]:""));
		    if (size(audio) != 0)
		    {
			// status of cound card
			state = _("Not running");
		    }
		    else
		    {
			// to translators: sound card has been removed from the machine
			state = _("Card removed");
		    }
		}
	    }

	    term r = `item(`id(pos), sformat("[%1]", pos), card["model"]:"",
		state);
	    pos = pos + 1;
	    return r;

	});
	return ret;
    }

    /**
     * function for enabling relevant controls in complex dialog
     * @param val boolean enable/disable
     */

    define void enableButtons(boolean val) ``{
	UI::ChangeWidget(`id(`b_delete),    `Enabled, val);
        UI::ChangeWidget(`id(`b_options),   `Enabled, val);
	UI::ChangeWidget(`id(`b_volume),    `Enabled, val);
    }

    /**
     * shows list of soundcards
     *
     * @return map result
     */

    define map ComplexDialog () ``{
	// help text
	symbol ui = nil;
        string helptext = Sound::STRINGS["ComplexDialog"]:"";

	y2debug(" save_info %1, agent %2", size(Sound::modules_conf), size(SCR::Dir(.audio.alsa.cards)));

	if (!Mode::config() && Sound::use_alsa &&
	    (size(Sound::modules_conf) != size(get_running_cards())))
	{
	    // marning popup message text
	    if (Popup::YesNo(_("The number of currently running cards doesn't match 
the number of configured cards in your configuration files. 
Try to restart sound system?")))

	    {
		sound_stop();
		sound_start_tmp(true);
	    }
	}

	list cc = createTable();

	boolean show_fonts = false;
	maplist(map card, Sound::modules_conf, ``{
	    if (HasFonts(card)) show_fonts = true;
	});

	term con = ComplexDlgCon(cc,
	    (Sound::rc_vars["LOAD_ALSA_SEQ"]:"no" == "yes"), show_fonts);

        // dialog title
	Wizard::SetContents (_("Sound configuration"), con, helptext,true,true);
	// Finish - button label
	Wizard::SetNextButton(`next, Label::FinishButton() );

	if (size (cc) == 0)
	{
	    enableButtons(false);
	}
	else
	{
	    UI::ChangeWidget(`id(`configured_table), `CurrentItem, 0);
	}

	do
	{
	    if (!Mode::config())
	    {
		SCR::Execute(.audio.alsa.restore, 0, 0);
	    }

	    ui = (symbol) UI::UserInput ();

	    if (ui == `b_delete)
	    {
		ui = `delete;
		Sound::card_id = (integer)
		    UI::QueryWidget (`id(`configured_table),`CurrentItem);
	    }
	    else if (ui == `b_options)
	    {
		Sound::card_id = (integer)
		    UI::QueryWidget (`id(`configured_table), `CurrentItem);

		map entry = Sound::modules_conf[Sound::card_id]:$[];

		map res = sound_options(entry);
		if (res["ui"]:nil == `next)
		{
		    map oldentry = entry;

		    entry = res["return"]:$[];
		    entry = add_common_options(entry, Sound::card_id);

		    integer pos = 0;
		    Sound::modules_conf = maplist(map card, Sound::modules_conf,
		    ``{
			if (pos != Sound::card_id)
			{
			    pos = pos + 1;
			    return card;
			}
			else
			{
			    pos = pos + 1;
			    return entry;
			}
		    });
		    if (oldentry != entry)
		    {
			// popup question text
			if (Popup::YesNo(_("To apply changes, the sound system
must be restarted.
Restart sound system now?
")))
			{
			    sound_stop();
			    sound_start_tmp(true);
			}
		    }
		}
		else if (res["ui"]:nil == `abort || res["ui"]:nil == `cancel)
		{
		    return $["ui" : `abort];
		}
		Wizard::SetNextButton(`next, Label::FinishButton() );

		// dialohg header
		Wizard::SetContents (_("Sound configuration"), con, helptext, true, true);
		cc = createTable();
		UI::ChangeWidget (`id(`configured_table), `Items, cc);
		UI::ChangeWidget(`id(`configured_table), `CurrentItem, 0);
	    }
	    else if (ui == `b_volume)
	    {
		Sound::card_id = (integer)
		    UI::QueryWidget (`id(`configured_table), `CurrentItem);
		map res = mixerDialog(Sound::card_id);
		if (res["ui"]:nil == `abort || res["ui"]:nil == `cancel)
                {
                    return $["ui": `abort];
                }

		Wizard::SetNextButton(`next, Label::FinishButton() );

		// dialog header
                Wizard::SetContents (_("Sound configuration"), con, helptext, true, true);
                cc = createTable();
                UI::ChangeWidget (`id(`configured_table), `Items, cc);
                UI::ChangeWidget(`id(`configured_table), `CurrentItem, 0);
	    }
	    else if (ui == `b_fonts)
	    {
		InstallFonts("", true);
	    }
	    else if (ui == `b_seq)
	    {
		string value = (boolean)
		    UI::QueryWidget(`id(`b_seq), `Value) ? "yes" : "no";
		Sound::rc_vars = add(Sound::rc_vars, "LOAD_ALSA_SEQ", value);
	    }
	    else if (ui == `b_conf2)
	    // add soundcard
	    {
		Sound::UpdateUnconfiguredCards();
		if (size(Sound::unconfigured_cards) == 0)
		{
		    ui = `add_manual;
		}
		else
		{
		    ui = `add_select;
		}
	    }
	    else if (ui == `cancel || ui == `abort)
	    {
		if (ReallyAbort())
		{
		    break;
		}
		else
		{
		    ui = `skip_event;
		}
	    }


	} while (!contains(
	[`back, `next, `cancel, `abort, `add_manual, `add_select, `delete],ui));

	return $["ui": ui];
    }

// ===== MAIN =====

/**
 * just calls ComplexDialog
 * @return map passed from ComplexDialog
 */
define map sound_complex() ``{

    list to_delete	= [];
    boolean config_mode = false;

    return ComplexDialog ();
}

}//EOF
