package com.sun.solarisinstall.panels;

import com.sun.wizards.core.*;
import com.sun.wizards.*;
import com.sun.solarisinstall.*;

import java.util.*;

public class DiskTextPanel implements DiskTableListener
{
  private DiskTableManager dtm;

  private String name;

  private int freeSpace;
  private int freeSpaceCyls;
  private int roundError;
  private int totalSpace;
  private int totalSpaceCyls;

  private int mode = 1;
  private String[] sliceNames;
  private int[] sliceSizes;
  private int[] sliceSizesCyls;
  private int[] startCyls;
  private int[] sliceMinSizes;
  private int[] sliceMinSizesCyls;
  
  
  public DiskTextPanel( DiskTableManager manager ) 
    {
      dtm = manager;
    }
  
  public void tableUpdate( DiskTableEvent dte ) 
    {
      if( dte.getName().equals( name ) ) {
	int idx = dte.getSliceNum();
	
	freeSpace = dte.getFreeSpace();
	freeSpaceCyls = dte.getFreeSpaceCyls();
	roundError = dte.getRoundError();

	if( idx != DiskTableEvent.NO_SLICE ) {
	  sliceNames[idx] = dte.getSliceName();
	  sliceSizes[idx] = dte.getSliceSize();
	  sliceMinSizes[idx] = dte.getSliceMinSize();
	  sliceSizesCyls[idx] = dte.getSliceSizeCyls();
	  sliceMinSizesCyls[idx] = dte.getSliceMinSizeCyls();
	  startCyls[idx] = dte.getStartCyl();
	}
      }
    }

  public void modifyDisk(String diskName)
    {
      int numSlices = dtm.getNumSlices(diskName);
      sliceNames = new String[ numSlices ];
      sliceSizes = new int[ numSlices ];
      sliceMinSizes = new int[ numSlices ];
      sliceSizesCyls = new int[ numSlices ];
      sliceMinSizesCyls = new int[ numSlices ];
      startCyls =  new int[ numSlices ];
      
      mode = dtm.MB_MODE;
      if (dtm.isSwapAtFront()) {
	 mode = promptForMode();
	if (mode == dtm.MB_MODE)
	  modifyDiskMb(diskName);
	else if (mode == dtm.CYL_MODE)
	  modifyDiskCyl(diskName);
      } else {
	mode = dtm.CYL_MODE;
	modifyDiskCyl(diskName);
      }
    }

  public int promptForMode() {
    
    String promptText = 
      	MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_CLI_PROMPT_FOR_MODE);
    String helpText ="";
    int result = -1;
    while (true) {
      TTYDisplay.showNewline();
      Vector tableData = new Vector();
      Vector rowData = new Vector();
      rowData.addElement("1) MB");
      tableData.addElement(rowData);
      rowData = new Vector();
      rowData.addElement("2) Cyls");   
      tableData.addElement(rowData);
      TTYDisplay.displayTable( tableData );
      TTYDisplay.showNewline();
      result =
	TTYDisplay.queryValue(promptText, 1, 1, 2, helpText);
      if (result > 0 || result < 3)
	return (result);
    }
  }

  public void modifyDiskCyl(String diskName)
    {
      name = diskName;
      int numSlices = dtm.getNumSlices(diskName);
    
      freeSpaceCyls = dtm.getFreeSpaceCyls( diskName );
      totalSpaceCyls = dtm.getDiskSizeCyls( diskName );
      
      for( int k = 0; k < numSlices; k++ ) {
	sliceNames[k] = dtm.getSliceName( diskName, k);
	sliceSizesCyls[k] = dtm.getSliceSizeCyls( diskName, k);
	sliceMinSizesCyls[k] = dtm.getSliceMinSizeCyls( diskName, k);
	startCyls[k] = dtm.getStartCyl(diskName,k);
      }
      
      Object[] arg = new Object[] { String.valueOf(numSlices) };
      String promptText =
	MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_CLI_DT_PROMPT, arg);
      String helpText ="";
      
      int result = -1;
      while( true ) {
	TTYDisplay.showNewline();
	int count = 0;

	Vector tableData = new Vector();
	Vector rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_NAME));
	rowData.addElement(diskName);
	tableData.addElement(rowData);
	rowData = new Vector(); 	
	rowData.addElement(MsgCat.getString(
			  "com.sun.solarisinstall.SolarisInstall",
			  SolarisInstallResources.MSG_CLI_DT_TOTAL) + "Cyl");
	rowData.addElement(String.valueOf(totalSpaceCyls));
	tableData.addElement(rowData);
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_USED) + " Cyl");
	rowData.addElement(String.valueOf(totalSpaceCyls - 
					  (freeSpaceCyls)));
	tableData.addElement(rowData);
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_FREE) + " Cyl");
	rowData.addElement(String.valueOf(freeSpaceCyls));
	tableData.addElement(rowData);
	TTYDisplay.displayTable(tableData);
	
	tableData = new Vector();
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_NUM));
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DISK_NAME));
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_START) + " Cyl");
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DISK_SIZE) + " Cyl");
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_MIN) + " Cyl");
	tableData.addElement( rowData );

	for(int i = 0; i < numSlices; i++) {
	  if(i == 2) i++;
	  rowData = new Vector();
	  rowData.addElement(i + ".");
	  if(!sliceNames[i].equals(DiskTableEvent.NO_NAME)) { 
	    rowData.addElement(sliceNames[i]);
	    rowData.addElement(String.valueOf(startCyls[i]) + " Cyl");
	    rowData.addElement(String.valueOf(sliceSizesCyls[i]) + 
				MsgCat.getString(
				"com.sun.solarisinstall.SolarisInstall", 
				SolarisInstallResources.MSG_SINGLESPACE_CYLS));
	    rowData.addElement(String.valueOf(sliceMinSizesCyls[i]) +
				MsgCat.getString(
				"com.sun.solarisinstall.SolarisInstall", 
				SolarisInstallResources.MSG_SINGLESPACE_CYLS));
	  } else {
	    rowData.addElement(MsgCat.getString(
			       "com.sun.solarisinstall.SolarisInstall",
			       SolarisInstallResources.MSG_CLI_DT_UNUSED));
	  }
	  tableData.addElement(rowData);
	}
	TTYDisplay.showNewline();
       	TTYDisplay.displayTable( tableData );

	TTYDisplay.showNewline();

	arg = new Object[] { String.valueOf(numSlices) };
	TTYDisplay.showText(MsgCat.getString(
			    "com.sun.solarisinstall.SolarisInstall",
			    SolarisInstallResources.MSG_CLI_DT_DONE, arg));
	TTYDisplay.showNewline();

	result =
	  TTYDisplay.queryValue(promptText, numSlices, 0, numSlices, helpText);
	if( (result != numSlices) && (result != 2) ) {
	  modifySliceCyls(diskName, result);
	} else {
	  try {
	    Vector errs = dtm.commit( this );
	    if( errs.size() != 0 ) {
	      printErrors( errs );
	    }
	    return;
	  } catch( DiskTableException ex ) {
	    printErrors( ex.getErrors() );
	  }
	}
      }
    }


  public void modifyDiskMb( String diskName ) 
    {
      name = diskName;
      int numSlices = dtm.getNumSlices( diskName ); 
      freeSpace = dtm.getFreeSpace( diskName );
      roundError = dtm.getRoundError( diskName );
      totalSpace = dtm.getDiskSize( diskName );
      
      for( int k = 0; k < numSlices; k++ ) {
	sliceNames[k] = dtm.getSliceName( diskName, k);
	sliceSizes[k] = dtm.getSliceSize( diskName, k);
	sliceMinSizes[k] = dtm.getSliceMinSize( diskName, k);
      }
      
      Object[] arg = new Object[] { String.valueOf(numSlices) };
      String promptText =
	MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_CLI_DT_PROMPT, arg);
      String helpText ="";
      
      int result = -1;
      while( true ) {
	TTYDisplay.showNewline();
	int count = 0;

	Vector tableData = new Vector();
	Vector rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_NAME));
	rowData.addElement(diskName);
	tableData.addElement(rowData);
	rowData = new Vector(); 	
	rowData.addElement(MsgCat.getString(
			  "com.sun.solarisinstall.SolarisInstall",
			  SolarisInstallResources.MSG_CLI_DT_TOTAL));
	rowData.addElement(String.valueOf(totalSpace));
	tableData.addElement(rowData);
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_USED));
	rowData.addElement(String.valueOf(totalSpace - 
					  (freeSpace + roundError)));
	tableData.addElement(rowData);
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_FREE));
	rowData.addElement(String.valueOf(freeSpace));
	tableData.addElement(rowData);
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_ROUND));
	rowData.addElement(String.valueOf(roundError));
	tableData.addElement(rowData);
	TTYDisplay.displayTable(tableData);
	
	tableData = new Vector();
	rowData = new Vector(); 
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_NUM));
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DISK_NAME));
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DISK_SIZE));
	rowData.addElement(MsgCat.getString(
			   "com.sun.solarisinstall.SolarisInstall",
			   SolarisInstallResources.MSG_CLI_DT_MIN));
	tableData.addElement( rowData );

	for(int i = 0; i < numSlices; i++) {
	  if(i == 2) i++;
	  rowData = new Vector();
	  rowData.addElement(i + ".");
	  if(!sliceNames[i].equals(DiskTableEvent.NO_NAME)) { 
	    rowData.addElement(sliceNames[i]);
	    rowData.addElement(String.valueOf( sliceSizes[i]) + 
				MsgCat.getString(
				"com.sun.solarisinstall.SolarisInstall", 
				SolarisInstallResources.MSG_SINGLESPACE_MB));
	    rowData.addElement(String.valueOf(sliceMinSizes[i]) +
				MsgCat.getString(
				"com.sun.solarisinstall.SolarisInstall", 
				SolarisInstallResources.MSG_SINGLESPACE_MB));
	  } else {
	    rowData.addElement(MsgCat.getString(
			       "com.sun.solarisinstall.SolarisInstall",
			       SolarisInstallResources.MSG_CLI_DT_UNUSED));
	  }
	  tableData.addElement(rowData);
	}
	TTYDisplay.showNewline();
       	TTYDisplay.displayTable( tableData );

	TTYDisplay.showNewline();

	arg = new Object[] { String.valueOf(numSlices) };
	TTYDisplay.showText(MsgCat.getString(
			    "com.sun.solarisinstall.SolarisInstall",
			    SolarisInstallResources.MSG_CLI_DT_DONE, arg));
	TTYDisplay.showNewline();

	result =
	  TTYDisplay.queryValue(promptText, numSlices, 0, numSlices, helpText);
	if( (result != numSlices) && (result != 2) ) {
	  modifySliceMb( diskName, result );
	} else {
	  try {
	    Vector errs = dtm.commit( this );
	    if( errs.size() != 0 ) {
	      printErrors( errs );
	    }
	    return;
	  } catch( DiskTableException ex ) {
	    printErrors( ex.getErrors() );
	  }
	}
      }
    }
  
  public void modifySliceCyls(String diskName, int sliceNum) {
    if( dtm.isDefaultSwapSlice( diskName, sliceNum ) ) {
      return;
    }
    
    String name = modifySliceName( diskName, sliceNum );
    if( !name.equals("") ) {
      modifyStartCyl(diskName, sliceNum);
      modifySliceSizeCyls( diskName, sliceNum);
    }
  }
  public void modifySliceMb( String diskName, int sliceNum ) {
    if( dtm.isDefaultSwapSlice( diskName, sliceNum ) ) {
      return;
    }
    
    String name = modifySliceName( diskName, sliceNum );
    if(!name.equals("")) {
      modifySliceSizeMb( diskName, sliceNum);
    }
  }

  public String modifySliceName( String diskName, int sliceNum ) {
    String prompt =
      MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
		       SolarisInstallResources.MSG_CLI_DT_PROMPT2);
    String help = "";
 
    while( true ) {
      String name = TTYDisplay.queryValue(prompt, TTYDisplay.NO_DEFAULT,
					  help);
      
      DiskTableEvent dte = dtm.current( diskName, sliceNum, this );
      
      if( name.equals("") ) {
	dte.setSliceName( DiskTableEvent.NO_NAME );
	dte.setSliceSize( DiskTableEvent.NO_SIZE );
	dte.setSliceSizeCyls(DiskTableEvent.NO_SIZE);
	dte.setStartCyl(DiskTableEvent.NO_SIZE);
      } else {
	dte.setSliceName(name);
	dte.setSliceSize(DiskTableEvent.NO_SIZE );
	dte.setSliceSizeCyls(DiskTableEvent.NO_SIZE);
	dte.setStartCyl(DiskTableEvent.NO_SIZE);
      }
      
      try {
	dtm.changeTable( dte, this, mode );
	return name;
      } catch( DiskTableException ex ) {
	printErrors( ex.getErrors() );
      }
    }
  }
  public int modifyStartCyl(String diskName, int sliceNum) {
      
    int firstAvail = ProfileUtil.getDiskInfo().getFirstCylToUse(diskName);
    
    String prompt =
      MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
		       SolarisInstallResources.MSG_CLI_DT_PROMPT_STARTCYL);
    String help = "";
    
    while( true ) {
      
      int start = TTYDisplay.queryValue(prompt, firstAvail, 
					0, 
					dtm.getDiskSizeCyls(diskName), help);
      
      DiskTableEvent dte = dtm.current( diskName, sliceNum, this );
      dte.setStartCyl(start);
      try {
	dtm.changeTable( dte, this, mode );
	return start;
      } catch( DiskTableException ex ) {
	printErrors( ex.getErrors() );
      }
    }
  }
  
  public int modifySliceSizeCyls(String diskName, int sliceNum) {

    String prompt =
      MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
		       SolarisInstallResources.MSG_CLI_DT_PROMPT_SIZECYL);
    String help = "";
    while( true ) {
      DiskTableEvent dte = dtm.current( diskName, sliceNum, this );
      int min = sliceMinSizesCyls[sliceNum];
      int diskSize =  dtm.getDiskSizeCyls(diskName);
      int size = 0;
      if (min <= diskSize) {
	size = TTYDisplay.queryValue(prompt, sliceSizesCyls[sliceNum], 
				     min, 
				     diskSize, help);
      } else {
	Vector v = new Vector();
	Object[] msgargs = { dte.getSliceName(), diskName };
	v.addElement(MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
		     SolarisInstallResources.MSG_MIN_SIZE_EXCEEDS_DISK,
			 msgargs) + "\n" +
		     MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_NEED_LARGER_DISK,
			 msgargs));
	printErrors(v);
	dte.setSliceName(DiskTableEvent.NO_NAME);
	dte.setStartCyl(DiskTableEvent.NO_SIZE);
	size = DiskTableEvent.NO_SIZE;
      }
      dte.setSliceSizeCyls(size);
      try {
	dtm.changeTable( dte, this, mode );
	return size;
      } catch( DiskTableException ex ) {
	printErrors( ex.getErrors() );
      }
    }

  }

  public int modifySliceSizeMb( String diskName, int sliceNum ) {

    String prompt =
      MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
		       SolarisInstallResources.MSG_CLI_DT_PROMPT3);
    String help = "";
    while( true ) {
      DiskTableEvent dte = dtm.current(diskName, sliceNum, this);
      int min = sliceMinSizes[sliceNum];
      int diskSize = dtm.getDiskSize(diskName);
      int size = 0;
      if (min <= diskSize) {
	size = TTYDisplay.queryValue(prompt, sliceSizes[sliceNum], 
				     min, 
				     diskSize, help);
      } else {
	Vector v = new Vector();
	Object[] msgargs = { dte.getSliceName(), diskName };
	v.addElement(MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_MIN_SIZE_EXCEEDS_DISK,
			 msgargs) + "\n" +
		     MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			 SolarisInstallResources.MSG_NEED_LARGER_DISK,
			 msgargs));
	printErrors(v);
	dte.setSliceName(DiskTableEvent.NO_NAME);
	size = DiskTableEvent.NO_SIZE;
      }
      dte.setSliceSize(size);
      try {
	dtm.changeTable( dte, this, mode );
	return size;
      } catch( DiskTableException ex ) {
	printErrors( ex.getErrors() );
      }
    }
  }

  public void printErrors( Vector errors ) {
    TTYDisplay.showNewline();
    TTYDisplay.showText(MsgCat.getString(
			"com.sun.solarisinstall.SolarisInstall",
			SolarisInstallResources.MSG_CLI_DT_ERROR));
    TTYDisplay.showNewline();
    for( Enumeration e = errors.elements(); e.hasMoreElements();) {
      TTYDisplay.showText((String) e.nextElement());
      TTYDisplay.queryValue("<"+MsgCat.getString("com.sun.wizards.Wizard",
			    WizardResources.MSG_CLI_PRESS_ENTER)+">", "");

    }
  }

}
