/*
 *************************************************************
 *
 *     YaST2      SuSE Labs                        -o)
 *     --------------------                        /\\
 *                                                _\_v
 *           www.suse.de / www.suse.com
 * ----------------------------------------------------------
 *
 * Author:        Michael Hager <mike@suse.de>
 *
 * Description:   Create a proposal for partitioning
 *
 *
 *
 *
 *
 *************************************************************

 $Id: inst_part_proposal.ycp 19964 2004-10-26 15:37:35Z fehr $

*/

{
  textdomain "storage";
  import "Arch";
  import "Wizard";
  import "Mode";
  import "Popup";
  import "Storage";

  include "partitioning/custom_part_check_generated.ycp";
  include "partitioning/do_proposal.ycp";

  boolean test_mode               = Mode::test ();
  boolean demo_mode               = Mode::test ();
  string  part_proposal_mode      = Storage::GetPartProposalMode();
				  
  map<string,map>     targetMap               = Storage::GetTargetMap();
  boolean run_in_testsuite        = Storage::GetTestsuite();
				  
  boolean proposal_firsttime      = Storage::GetPartProposalFirst();
  boolean orig_proposal_firsttime = proposal_firsttime;
  boolean no_suggestion_possible = true;

  if ( test_mode && (size(targetMap) == 0) )
  {
      if ( demo_mode ) {
	  y2warning("***** Demo mode active - using fake demo values *****");
	  targetMap = (map<string,map>)SCR::Read(.target.yast2, "demo_target_map.ycp");
      }
      else {// ! demo_mode
	  y2warning("***** Test mode active - using fake values *****");
	  targetMap = (map<string,map>)SCR::Read(.target.yast2, "test_target_map.ycp");
      }
      Storage::SetTargetMap( targetMap );
  }  


  //////////////////////////////////////////////////////
  // This module is only for i386 fdsik-lable
  
  if ( Arch::s390 () )
      {
      return Storage::GetExitKey();
      }	




  // ///////////// DELETE THIS LINE FOR PRODUCTIOIN VERSION
  //
  // proposal_firsttime  = true;
  // orig_proposal_firsttime  = true;






  string title        = "";
  string accept_str   = "";
  string modify_str   = "";
  string detailed_str = "";

  // Title for dialogue
  title = _("Suggested Partitioning");
  // Radiobutton for partition dialog
  accept_str   = _("&Accept proposal as-is");
  // Radiobutton for partition dialog
  modify_str   = _("Ba&se partition setup on this proposal");
  // Radiobutton for partition dialog
  detailed_str = _("&Create custom partition setup");

  term contents =
     `VBox (
	`VSpacing (0.5),
	`HBox (
	    `HSpacing (2),
	    `Top (`RadioButtonGroup (`id ("part_option"),
		     `VBox (
		          // Label text
			  `HBox (`RichText (`id ("richtext"), _("One moment please ..."))
					   ),
			  `VSpacing (1),
			  // Frame description in suggested partition for mode accept modify ..
			  `HCenter(`Frame (_("Choose"), `HVSquash (
			     `VBox (
				    `VSpacing (0.2),
				    // Radio Button Label in Suggested Partitioning
				    `Left (`RadioButton (`id ("accept"), accept_str, part_proposal_mode == "accept")),
				    `VSpacing (0.2),
				    // Radio Button Label in Suggested Partitioning
				    `Left (`RadioButton (`id ("modify"), modify_str, part_proposal_mode == "modify")),
				    `VSpacing (0.2),
				    // Radio Button Label in Suggested Partitioning
				    // difficult option: there follows a dialog where the user can choose either the partition
				    // assistant or the expert partitioner.
				    `Left (`RadioButton (`id ("detailed"), detailed_str, part_proposal_mode == "detailed")),
				    `VSpacing (0.2)
				    )
			     )
			   ))
			  ))
		  ),
	    `HSpacing (2)
	    ),
	`VSpacing(2) );


  // help on suggested partitioning
  string help_text =  _("<p>
YaST has checked your hard disks and proposes the displayed partition setup 
for your hard drive.</p>");
  // help text continued
  help_text =  help_text + _("<p>
You can continue with this proposal based on YaST's ideas.
Activate the second radio button in this case.</p>
");

  // help text continued
  help_text =  help_text + _("<p>
If YaST's suggestion does not fit your intentions, create
your own partition setup starting with the partitions as 
currently present on the disks. Activate the third radio button 
in this case. This is also the option to choose for
advanced options like RAID and LVM.</p>
");


  y2debug( "analyse target: %1 ", targetMap);


  if ( proposal_firsttime )
  { 
      map prop = do_proposal();
      proposal_firsttime = false;
      no_suggestion_possible = !prop["ok"]:false;
      if( !no_suggestion_possible )
	  {
	  targetMap = prop["target"]:$[];
	  }
  }



  symbol ret = nil;

  if( !run_in_testsuite )
      {
      /////////////////////////////////////////////////////////////////////
      // next step to visualize the hit the new target map from hit

      if( no_suggestion_possible && orig_proposal_firsttime )
	  {
	  y2milestone( "Could not create a proposal %1 %2 ",
		       no_suggestion_possible, orig_proposal_firsttime );
	  
	  return( Storage::GetExitKey() );
	  }
      else
	  {
	  string changes = TargetChangesToRichText(targetMap,$[]);
	  y2milestone( "current proposal: %1", changes );

	  if( size(changes) == 0 )
	      {
	      changes = "<p></p>" +
		        // popup text, richtext format
		       _("<p>You have rejected the proposal. Use <b>Modify</b> or <b>Discard</b></p>");
	      }
	  Wizard::SetContents( title, contents, help_text, 
	                       (boolean)WFM::Args(0), (boolean)WFM::Args(1) );
	  UI::ChangeWidget( `id("richtext"), `Value, changes);

	  if ( Stage::initial () )
	      Wizard::SetTitleIcon( "partitioner" );
	  
	  }

      repeat
          {
          Wizard::SetFocusToNextButton();
          ret = (symbol)Wizard::UserInput();
     
          string part_proposal_mode = 
	      (string) UI::QueryWidget(`id("part_option"), `CurrentButton);
         
	  if( ret == `abort && Popup::ReallyAbort(true) )
	      return `abort;
     
         
	  if (ret != `back) 
              {
	      Storage::SetPartProposalFirst( false );

	      string target_is = "";
             
	      Storage::SetPartProposalMode( part_proposal_mode );
     
	      if ( part_proposal_mode == "accept")
		  {
		  symbol doagain = `next;
		      
		  target_is = "SUGGESTION";
		  list retval = check_created_partiton_table( targetMap , true, true );
		  targetMap   = retval[0]:$[];
		  doagain     = retval[1]:`next;

		  if ( doagain == `again ) return `again;
		  }
	      else if( part_proposal_mode == "modify" )
		  {
		  target_is = "PROP_MODIFY";
		  }
	      else if( part_proposal_mode == "detailed" )
		  {
		  if( Storage::GetPartMode()!="CUSTOM" )
		      {
		      target_is = "NORMAL";
		      }
		  else
		      {
		      target_is = "CUSTOM";
		      }
		  Storage::SetPartDisk( "" );
		  }
		  
	      Storage::SetPartMode( target_is );
	      Storage::SetTargetMap( targetMap );
	      }

	  } until ( ret == `next || ret == `back || ret == `cancel );
      }
  Storage::SaveExitKey( ret );

  return ret;
}
