package phoneycall;

import javax.microedition.lcdui.*;

import gui.*;


/**
 * The SingleCallScreen in PhoneyCall
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SingleCallScreen extends AbstractState
{
    private   Form          form;
    private   TextField     delay;
    
    private static SingleCallScreen    instance;
    
    
    
    /**
     * Explicit Value Constructor
     *
     * @param context  The ApplicationContext
     */
    private SingleCallScreen(ApplicationContext context)
    {
        super(context);
        
        form = new Form("Single Call Details");
        
        delay = new TextField("Delay (sec.):", "5", 6, TextField.NUMERIC);
        delay.setLayout(Item.LAYOUT_EXPAND|Item.LAYOUT_NEWLINE_AFTER);
        
        form.append(delay);
        
        form.addCommand(CANCEL_COMMAND);
        form.addCommand(OK_COMMAND);
    }
    
    
    /**
     * Enter this AbstractState
     *
     * Specifically, construct an instance if necessary, perform any
     * necessary operations, and call the instances enter() method
     *
     * @param context     The ApplicationContext
     * @return            The (singleton) instance of this State
     */
    public static AbstractState enter(ApplicationContext context)
    {
        if (instance == null) instance = new SingleCallScreen(context);
        
        instance.enter();
        
        return instance;
    }
    
    
    /**
     * Get the Displayable associated with this AbstractState
     *
     * @returns   The Displayable
     */
    protected Displayable getDisplayable()
    {
        return form;
    }
    
    
    
    /**
     * Handle a CANCEL_COMMAND
     */
    public void handleCancel()
    {
        CallCountScreen.enter(context);
    }
    
    
    /**
     * Handle an OK_COMMAND
     */
    public void handleOK()
    {
        CallersScreen   next;
        int             n;
        WaitingScreen   waiting;
        
        // Set the attributes of the WaitingScreen
        waiting = WaitingScreen.createInstance(context);
        try
        {
            n = Integer.parseInt(delay.getString());
        }
        catch (NumberFormatException nfe)
        {
            n = 5;
        }
        waiting.setDelay(n);
        waiting.setNumber(1);
        waiting.setInterval(n);
        
        // Set the attributes of the CallersScreen
        next = CallersScreen.createInstance(context);
        next.setPreviousState(this);
        
        // Enter the CallersScreen
        next.enter();
    }
    
}
