package phoneycall;

import java.util.*;
import javax.microedition.lcdui.*;

import auditory.*;
import gui.*;

/**
 * The CallersScreen in PhoneyCall
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class WaitingScreen extends AbstractState
{
    private AudioPlayer      player;
    private Form             form;
    private int              delay, interval, number;
    private Ringer           ringer;
    private String[]         callers;
    private StringItem       text;
    private Timer            timer;
    
    private static WaitingScreen     instance;
    
    
    /**
     * Explicit Value Constructor
     *
     * @param context  The ApplicationContext
     */
    private WaitingScreen(ApplicationContext context)
    {
        super(context);
        
        player = new AudioPlayer();
        
        form = new Form("");
        text = new StringItem("","Waiting for a call...");
        form.append(text);
        
        form.addCommand(CANCEL_COMMAND);
        form.addCommand(OK_COMMAND);
        
        timer = new Timer();
    }
    
    
    
    /**
     * Construct an instance of this class
     *
     * @param context     The ApplicationContext
     * @return            The (singleton) instance of this State
     */
    public static WaitingScreen createInstance(ApplicationContext context)
    {
        if (instance == null) instance = new WaitingScreen(context);
        
        return instance;
    }
    
    
    /**
     * Enter this instance of an AbstractScreen
     */
    public void enter()
    {
        super.enter();
        ringer = new Ringer();
        timer.schedule(ringer, delay*1000, (interval*1000)/number);
    }
    
    
    /**
     * 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)
    {
        instance = createInstance(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()
    {
        // Stop the audio
        player.stop();
        
        // Reset the message
        text.setText("Waiting for a call...");
        
        // Stop the TimerTask
        ringer.cancel();
        
        // Return to the CallCountScreen
        CallCountScreen.enter(context);
    }
    
    
    /**
     * Handle an OK_COMMAND
     */
    public void handleOK()
    {
        // Stop the audio
        player.stop();
        
        // Reset the message
        text.setText("Waiting for a call...");
        
        if (number == 0) ringer.cancel();
    }
    
    
    
    /**
     * Set the collection of callers
     *
     * @param callers  The collection of callers
     */
    public void setCallers(String[] callers)
    {
        this.callers = callers;
    }
    
    
    /**
     * Set the delay
     *
     * @param delay   The delay in minutes
     */
    public void setDelay(int delay)
    {
        this.delay = delay;
    }
    
    
    /**
     * Set the interval
     *
     * @param interval   The interval in minutes
     */
    public void setInterval(int interval)
    {
        this.interval = interval;
    }
    
    
    /**
     * Set the number of calls
     *
     * @param number   The number of calls
     */
    public void setNumber(int number)
    {
        this.number = number;
    }
    
    
    /**
     * The TimerTask used by the WaitingScreen
     *
     * Note: This is nested class (specifically, a named inner class)
     */
    private class Ringer extends TimerTask
    {
        public void run()
        {
            number--;
            if (number < 0) number = 0;
            
            text.setText("Incoming call from "+callers[number]);
            player.play("ringtone.wav");
        }
    }
}
