package part1;

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

/**
 * The context and controller for a VHS Player.
 *
 * @author $Author: cdent $
 *
 * @version $Revision: 1.2 $
 *
 */
public class VHSContext implements PlayerContext {

    private static final String PLAYER_NAME = "VHS 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(2, 2));
	frame.setSize(300, 100);
	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(VHSPlayCommand command) {
	System.out.println("=====> We are playing. Quality not so good.");
	fastForwardCommand.falsify();
	rewindCommand.falsify();
	stopCommand.falsify();
    }
    
    public void commandChanged(VHSFastForwardCommand command) {
	if (playCommand.isPressed()) {
	    System.out.println("=====> We are cueing (with pictures). Better control.");
	} else {
	    System.out.println("=====> We are fast fowarding (no pictures). Bzzz");
	}
	rewindCommand.falsify();
	stopCommand.falsify();
    }

    public void commandChanged(VHSStopCommand command) {
	playCommand.falsify();
	rewindCommand.falsify();
	fastForwardCommand.falsify();
	System.out.println("=====> All stop. Listen to the whirs.");
    }

    public void commandChanged(VHSRewindCommand command) {
	if (playCommand.isPressed()) {
	    System.out.println("=====> We are reverse-cueing (with pictures). No buffers!");
	} else {
	    System.out.println("=====> We are rewinding (no pictures). Whirr.");
	}
	fastForwardCommand.falsify();
	stopCommand.falsify();
    }
	
}
	
