import sale.*;
import sale.events.*;
import sale.stdforms.*;
import data.events.*;
import data.ooimpl.*;
import log.*;
import log.stdforms.*;
import java.io.*;
public class Office extends SalesPoint
{
  
  
  private static String password;
  
  
  public Office(String name)
  {
    super(name);
    
    ((MoneyBagImpl)Shop.getTheShop().getStock
     ("coin slot")).addStockChangeListener(new StockChangeAdapter()
     {
       
       public void commitAddStockItems (StockChangeEvent sce)
       {
	 
	 if (getCurrentProcess() == null && hasUseableDisplay (null))
	   try {
	     setFormSheet (null, getDefaultFormSheet());
	   }
	   catch (InterruptedException iexc) {
	     System.err.println ("Update interrupted");
	   }
       }
       
       public void commitRemoveStockItems (StockChangeEvent sce)
       {
	 
	 if (getCurrentProcess() == null && hasUseableDisplay (null))
	   try {
	     setFormSheet (null, getDefaultFormSheet());
	   }
	   catch (InterruptedException iexc) {
	     System.err.println ("Update interrupted");
	   }
       }
     });
    
    Shop.getTheShop().getTimer().addTimerListener(new TimerAdapter()
    {
      public void onGoneAhead (TimerEvent trEvt)
      {
	
	if (hasUseableDisplay(null)) {
	  try {
	    setFormSheet(null, getDefaultFormSheet());
	  }
	  catch (InterruptedException ie) {}
	}
      }
    });
  }
  
  
  public FormSheet getDefaultFormSheet()
  {
    return new FormSheet("Office", new DefaultOfficeFormCreator(this), false);
  }
  
  public MenuSheet getDefaultMenuSheet()
  {
    
    MenuSheet msSubMenu = new MenuSheet("Maintenance");
    
    msSubMenu.add(new MenuSheetItem ("Advance time", new sale.Action()
    {
      public void doAction(SaleProcess p, SalesPoint sp)
      {
        
	Shop.getTheShop().getTimer().goAhead();
      }
    }));
    
    msSubMenu.add (new MenuSheetItem ("See log file", new sale.Action()
    {
      public void doAction (SaleProcess p, SalesPoint sp)
      {
	try {
	  
	  FileInputStream fis = new FileInputStream ("machine.log");
	  LogInputStream lis = new LogInputStream (fis);
	  LogTableForm ltf = new LogTableForm ("View log file", lis);
	  
	  ltf.removeButton (FormSheet.BTNID_CANCEL);
	  
	  setFormSheet (null, ltf);
	}
	
	catch (FileNotFoundException fnfexc) {
	  try {
	    setFormSheet(null, new MsgForm("Error", "Log file not found."));
	  }
	  catch (InterruptedException iexc) {}
	}
	
	catch (IOException ioexc) {
	  try {
	    setFormSheet(null, new MsgForm("Error",
	      "Log file corrupt. It might be empty."));
	  }
	  catch (InterruptedException iexc) {}
	}
	
	catch (InterruptedException iexc) {
	  try {
	    setFormSheet (null, new MsgForm ("Error", iexc.toString()));
	  }
	  catch (InterruptedException iexc2) {}
	}
      }
    }));
    
    MenuSheet msMenu = new MenuSheet("office menu");
    
    msMenu.add(msSubMenu);
    
    return msMenu;
  }
  
  public static void setPassword(String password)
  {
    Office.password = password;
  }
  
  public static boolean testPassword(String password)
  {
    return password.equals(Office.password);
  }
}