package part1;

import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.WindowConstants;

/**
 * Context and controler for a Tape Player
 *
 * @author $Author: cdent $
 *
 * @version $Revision: 1.2 $
 *
 */
public class TapeContext implements PlayerContext {

    private static final String PLAYER_NAME = "Tape Player";

    private PlayerCommand playCommand;
    private PlayerCommand fastForwardCommand;
    private PlayerCommand rewindCommand;
    private PlayerCommand stopCommand;

    private JFrame frame = new JFrame(PLAYER_NAME);

    public void show() {
	frame.getContentPane().setLayout(new GridLayout(1, 4));
	frame.setSize(400, 50);
	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	frame.show();
    }

    private void addButton(PlayerCommand command) {
	frame.getContentPane().add(command.getButton());
    }

    
    public void setPlayCommand(PlayerCommand command) {
	this.playCommand = command;
	addButton(command);
    }

    public void setFastForwardCommand(PlayerCommand command) {
	this.fastForwardCommand = command;
	addButton(command);
    }

    public void setStopCommand(PlayerCommand command) {
	this.stopCommand = command;
	addButton(command);
    }

    public void setRewindCommand(PlayerCommand command) {
	this.rewindCommand = command;
	addButton(command);
    }

    public void commandChanged(TapePlayCommand command) {
	System.out.println("=====> We are playing. Cassettes, wow.");
	fastForwardCommand.falsify();
	rewindCommand.falsify();
	stopCommand.falsify();
    }
    
    public void commandChanged(TapeFastForwardCommand command) {
	if (playCommand.isPressed()) {
	    System.out.println("=====> We are cueing. What's that crazy shrieking?");
	} else {
	    System.out.println("=====> We are fast fowarding.");
	}
	rewindCommand.falsify();
	stopCommand.falsify();
    }

    public void commandChanged(TapeStopCommand command) {
	playCommand.falsify();
	rewindCommand.falsify();
	fastForwardCommand.falsify();
	System.out.println("=====> All stop.");
    }

    public void commandChanged(TapeRewindCommand command) {
	if (playCommand.isPressed()) {
	    System.out.println("=====> We are reverse-cueing. Oh, my ears!");
	} else {
	    System.out.println("=====> We are rewinding.");
	}
	fastForwardCommand.falsify();
	stopCommand.falsify();
    }
	
}
	
