/**
 * File:
 *   lvm_pv.ycp
 *
 * Module:
 *   lvm_pvs
 *
 * Summary:
 *   lvm_pv: creates a volume group (i.e. "system")
 *   and adds the physical volumes 
 *
 * Authors:
 *   mike <mike@suse.de>
 *
 *
 *
 * $Id: inst_lvm_pv.ycp 20683 2005-01-07 18:40:29Z nashif $
 *
 *
 *----------------------------------------------------
 * IMPORTANT: when you read this code notice:
 *
 * vg  = volume group
 * vgs = volume groups
 *
 * pv  = physical volume
 * pvs = physical volumes
 *
 * lv  = logical volume
 * lvs = logical volumes
 *----------------------------------------------------
 *
 */

{

    textdomain "storage"; 

    import "Installation";
    import "Mode";
    import "Storage";
    import "Wizard";
    import "Label";
    
    include "partitioning/lvm_ui_dialogs.ycp";
    include "partitioning/lvm_ui_lib.ycp";
    include "partitioning/lvm_pv_lib.ycp";
    include "partitioning/lvm_lv_lib.ycp";
    include "partitioning/partition_defines.ycp";


    string  current_vg     = "none";    
    boolean test_mode      = Mode::test ();
    map<string,map>     targetMap      = Storage::GetTargetMap();
    map<integer,map>     modify_targets = (map<integer,map>)Storage::GetModifyTargets();
    
    list    lvm_vgs      = get_vgs( targetMap );
    list<map>    parts 	 = get_possible_pvs( targetMap );
    list    table 	 = get_pv_widget_table( parts );

    // heading text
    Wizard::SetContents( _("Logical Volume Manager -- Physical Volume Setup"),
			 get_pv_content( table, lvm_vgs, "_________"), 
			 "", true, true);
    Wizard::SetHelpText( getPvHelptext() );


    
    //////////////////////////////////////////////////////////////////////
    // First we typically have to create a volumegroup
    // check if there is already a lvm group
    //////////////////////////////////////////////////////////////////////
    
    if ( size( lvm_vgs ) == 0 )
    {
        map vg = DlgCreateVolumeGroup( lvm_vgs );
	map addVG = addVolumeGroup( vg, targetMap, lvm_vgs, modify_targets );
	
	// has the user cancelled the dialog? 
	if ( ! addVG["cancelled"]:false )
	{
	    targetMap       = addVG["targets"]:$[];
	    lvm_vgs         = addVG["lvm_vgs"]:[];
	    current_vg      = addVG["current_vg"]:"none";
	    modify_targets  = addVG["modify_targets"]:$[];
	}
	else
	{
	    return( `back );
	}
    }
    else
    {
	// there are already vgs ... select the first one
	current_vg = lvm_vgs[0]:"none"; 
    }

    
    // Display current vg:
    new_vg_list( lvm_vgs );
    UI::ChangeWidget( `id(`vg), `Value, current_vg);
    
    

    //////////////////////////////////////////////////////////////////////
    // mainloop
    //////////////////////////////////////////////////////////////////////

    string id  =  "";
    symbol ret = `next;

    repeat
	{
	/////////////////////////////////////////////////////////////////
	// Show the current state:
	parts 	 = get_possible_pvs( targetMap );
	table 	 = get_pv_widget_table( parts );
	UI::ChangeWidget( `id(`pv_table), `Items, table);

	if( current_vg != nil )
	    {
	    string vg_size_str = (string)get_vg_size( parts, current_vg, 
	                                              `string );
	    y2debug( "**** %1", vg_size_str );
	    UI::ChangeWidget( `id(`pv_size), `Value, vg_size_str );
	    }
	

        UI::SetFocus(`id(`pv_table));

	if( id != "" )
	    {
	    UI::ChangeWidget( `id(`pv_table), `CurrentItem, id);
	    }

        /////////////////////////////////////////////////////////////////
        // Wait for User input


	ret = (symbol)Wizard::UserInput();
	y2milestone("#################### EE %1", ret);


	////////////////////////////////////////
	// toggle between pv_add and pv_delete 
	////////////////////////////////////////

	if( ret ==  `pv_table )
	    {
	    id = (string)UI::QueryWidget(`id(`pv_table), `CurrentItem);

	    if( isItemPv(targetMap,id) )
		{
		ret = `pv_delete;
		}
	    else
		{
		ret = `pv_add;
		}
	    }
	

	////////////////////////////////////////
	// user has changed the current volume group
	////////////////////////////////////////
	
	if( ret == `vg )
	    {
	    current_vg = (string)UI::QueryWidget( `id(`vg), `Value);
	    y2milestone("TTT %1", current_vg );
	    }

        ////////////////////////////////////////
        // Add a new volume group:
        ////////////////////////////////////////

        if( ret == `vg_add_vg  )
	    {
                
            map vg = DlgCreateVolumeGroup( lvm_vgs );
            map addVG = addVolumeGroup(vg, targetMap, lvm_vgs, modify_targets );

            // has the user cancelled the dialog?
            if( !addVG["cancelled"]:false )
		{
                targetMap       = addVG["targets"]:$[];
                modify_targets  = addVG["modify_targets"]:$[];
                lvm_vgs         = addVG["lvm_vgs"]:[];
                current_vg      = addVG["current_vg"]:"none";
		}
	    }


	////////////////////////////////////////
	// Remove volume group:
	////////////////////////////////////////
	
	if( ret == `vg_remove_vg && current_vg != nil )
	    {
	    map ret = HandleRemoveVg( targetMap, modify_targets, current_vg );
	    if( size(ret)>0 )
		{
		targetMap       = ret["targets"]:targetMap;
		modify_targets  = ret["modify_targets"]:modify_targets;
		lvm_vgs  	= get_vgs( targetMap );
		current_vg = lvm_vgs[0]:"";
		// Display current vg:
		new_vg_list( lvm_vgs );
		UI::ChangeWidget( `id(`vg), `Value, current_vg);
		y2milestone( " current_vg %1" , current_vg );
		}
	    }


	////////////////////////////////////////
	// Add a new physical volume:
	////////////////////////////////////////
	
	
	if( ret == `pv_add && current_vg != nil )
	    {
	    id = (string)UI::QueryWidget(`id(`pv_table), `CurrentItem);
	    y2debug( "WWW ID ADD GROUP %1", id);
	    
	    if( id!=nil && CheckItemIsNotPv(targetMap,id) )
		{
		map addPV = addPhysicalVolume( modify_targets, targetMap, 
		                               id, current_vg );
            
		// has an error occured
		if( !addPV["error"]:false )
		    {
		    targetMap      = addPV["targets"]:$[];
		    modify_targets  = addPV["modify_targets"]:$[];
		    }
		}
	    }

	////////////////////////////////////////
	// Delete a new physical volume:
	////////////////////////////////////////
	
	if( ret == `pv_delete && current_vg != nil )
	    {
	    id = (string)UI::QueryWidget(`id(`pv_table), `CurrentItem);

	    if( id!=nil && CheckItemIsPv(targetMap,id) )
		{
		string vg = Storage::IdFromTarget( targetMap, id )["lvm_group"]:"";
		y2debug( "vg=%1", vg );

		if( check_pv_delete( targetMap, id, vg ) == `nodelete )
		    {
		    ret = `again;
		    continue;
		    }
	    
		map deletePV = removePhysicalVolume( modify_targets, targetMap,
		                                     id, vg );
		// has an error occured
		if( !deletePV["error"]:false )
		    {
		    targetMap      = deletePV["targets"]:$[];
		    modify_targets  = deletePV["modify_targets"]:$[];
		    }
		}
	    }

	
	////////////////////////////////////////
	// Finish this step
	////////////////////////////////////////
	
	if( ret == `next || ret == `back )
	    {
	    // due to lvm tools, we need a already created pv before
	    // creating a vg so we patch the execution stack

	    if( ret == `back )
		{
		Wizard::RestoreNextButton();
		}

	    if( checkModifyTargets( modify_targets, targetMap ) != `next )
		{
		ret = `again;
		}
	    else
		{
		Storage::SetModifyTargets( modify_targets );
		Storage::SetTargetMap( targetMap );
		}
	    }

    } until (ret == `next || ret == `back || ret == `cancel || ret == `abort );
    
    return ret;
}

// end
