package clueless.view;
import clueless.controller.GameController;
import clueless.events.EndTurnEvent;
import clueless.events.EndTurnEventListener;
import clueless.events.DisconnectEvent;
//import clueless.events.GameInitializationEvent;
import clueless.events.MakeAccusationEvent;
import clueless.events.MakeSuggestionEvent;
import clueless.events.MessageEvent;
import clueless.events.MessageEventListener;
import clueless.events.ServerClientInfoEvent;
import clueless.events.TextStatusEvent;
import clueless.events.TextStatusEventListener;
import clueless.messaging.AvailableMoveMessage;
import clueless.messaging.EndTurnMessage;
import clueless.messaging.GameInitializationMessage;
import clueless.messaging.ShowTextStatusMessage;
import clueless.messaging.SuggestionDisprovedNeedFeedBackMessage;
import clueless.messaging.TextStatusMessage;
import clueless.model.*;
import clueless.model.Character;
import clueless.model.decks.Card;
import clueless.view.NotebookView;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.EventListener;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultCaret;
/**
* MainGameView
*
* Class provides human interface to the controller for playing the game
*
* @author G2
*/
//TODO implements TextStatusEventListener
public class MainGameView extends AbstractView implements MessageEventListener, EndTurnEventListener{
private GameController controller;
private SetupView setupView;
private AccusationView accusationView;
private SuggestionView suggestionView;
private BoardView boardView;
private UserHandView hand;
private NotebookView notebook;
private JTextPane statusTextPane;
private JPanel suggestionPanel = new JPanel();
private JButton makeAccusation = new JButton("Make Accusation");
private JButton makeSuggestion = new JButton("Make Suggestion");
private JButton endTurn = new JButton("End Turn");
public MainGameView(GameController controller){
this.controller = controller;
this.jfrm = new JFrame("Clue-Less by M4");
this.jfrm.setResizable(false);
this.accusationView = new AccusationView(controller);
this.suggestionView = new SuggestionView(controller);
this.boardView = new BoardView(controller);
controller.addView(this);
initializeView();
controller.addMessageEventListener(this);
controller.addEndTurnEventListener(this);
}
private void initializeView()
{
//Get instances of various view singleton classes
notebook = NotebookView.getInstance();
hand = UserHandView.getInstance(controller);
//Format notebook panel before adding to jfrm
JPanel notebookPanel = notebook.getNotebookPanel();
notebookPanel.setSize(300, 750);
notebookPanel.setVisible(true);
//Format gameboard
JLayeredPane gameboardPane = boardView.getBoardPanel();
gameboardPane.setSize(800, 600);
gameboardPane.setVisible(true);
//Format UserHand panel before adding to jfrm
JPanel userHandPanel = hand.getUserHandPanel();
userHandPanel.setSize(300, 750);
userHandPanel.setVisible(true);
//TODO: Disable make accusation & suggest buttons until it is the players turn
makeAccusation.setEnabled(false);
makeSuggestion.setEnabled(false);
endTurn.setEnabled(false);
makeAccusation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Show accusation view
controller.fireMakeAccusationEvent(new MakeAccusationEvent(this, null));
}
});
makeSuggestion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Show suggestion view
controller.fireMakeSuggestionEvent(new MakeSuggestionEvent(this, null));
}
});
endTurn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
//Send EndTurn message to server
controller.fireEndTurnEvent(new EndTurnEvent(this));
//Disable buttons until next turn
makeSuggestion.setEnabled(false);
makeAccusation.setEnabled(false);
endTurn.setEnabled(false);
}
});
suggestionPanel.add(makeAccusation);
suggestionPanel.add(makeSuggestion);
suggestionPanel.add(endTurn);
//Create scrolling pane for text status display
JPanel statusPanel = new JPanel();
statusPanel.setSize(new Dimension(320, 190));
statusTextPane = new JTextPane();
statusTextPane.setEditable(false);
statusTextPane.setPreferredSize(new Dimension(320, 190));
DefaultCaret caret = (DefaultCaret)statusTextPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane scroll = new JScrollPane(statusTextPane);
scroll.setPreferredSize(new Dimension(320, 190));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
statusPanel.add(scroll);
//Setup layout manager
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
//Set column widths for Hand, Board, and Notebook respectively
gridBag.columnWidths = new int[]{300, 700, 300};
gridBag.rowHeights = new int[]{700, 200};
jfrm.getContentPane().setLayout(gridBag);
//Set constraints for Notebook Panel
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 2;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.anchor = constraints.EAST;
constraints.weightx = 1;
constraints.fill = constraints.BOTH;
jfrm.getContentPane().add(notebookPanel, constraints); //can be null
//Set constraints for User Hand Panel
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 2;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.anchor = constraints.WEST;
jfrm.getContentPane().add(userHandPanel, constraints); //can be null
//Set constraints for Suggestion panel
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.anchor = constraints.NORTH;
jfrm.getContentPane().add(suggestionPanel, constraints);
//Set constraints for text status panel
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.fill = constraints.BOTH;
constraints.anchor = constraints.NORTH;
//jfrm.getContentPane().add(statusTextPane, constraints);
jfrm.getContentPane().add(statusPanel, constraints);
//Set constraints for Game Board panel
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.fill = constraints.BOTH;
constraints.anchor = constraints.NORTH;
jfrm.getContentPane().add(gameboardPane, constraints);
/** Intercept Window Closure -- Confirm and gracefully exit */
jfrm.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
jfrm.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {
System.exit(0);
}
public void windowClosing(WindowEvent arg0) {
int choice = JOptionPane.showConfirmDialog(jfrm, "Are you sure you wish to quit?",
"Quit Game", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
controller.fireDisconnectEvent(new DisconnectEvent(this));
jfrm.dispose();
}
}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
});
jfrm.setSize(1400, 950);
}
public void gameSetup(){
this.setupView = new SetupView(controller);
}
//Paint static game data (Board background, Own Deck of Cards)
public void initialView()
{
jfrm.setVisible(true);
}
//Not tied to Players' turns
public void updateNotebook()
{
/*TODO: CC - Not sure what's supposed to go on here -- seems like the UI state would be
* preserved simply by keep the panel open and not repainting.
*/
}
//Player's Turn Actions
public void makeMove()
{
}
public void showSuggestionView(Location location)
{
if(location instanceof Room)
{
suggestionView.displayCards((Room)location);
}
else
{
JOptionPane.showMessageDialog(null, "You may only make suggestions from a room, not hallways.");
}
}
public void showAccusationView()
{
accusationView.displayCards();
}
public void counterTheory()
{
}
public void quitGame()
{
int choice = JOptionPane.showConfirmDialog(jfrm, "Are you sure you wish to quit?",
"Quit Game", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
controller.fireDisconnectEvent(new DisconnectEvent(this));
jfrm.dispose();
}
}
//Player's Wait / Watch
public void updateView()
{
}
public void repaint()
{
jfrm.repaint();
}
@Override
public void handleMessageEvent(MessageEvent event) {
//System.out.println("MainViewListener received message: " + event.getMessage().toString());
if(event.getMessage() instanceof GameInitializationMessage)
{
GameInitializationMessage gameInit = (GameInitializationMessage)event.getMessage();
//TODO: enable make accusation / suggestion button if players turn
//TODO: this should really only occur after a move has been made (is it possible to decline moving?)
if(gameInit.isTurn())
{
makeAccusation.setEnabled(true);
makeSuggestion.setEnabled(true);
endTurn.setEnabled(true);
}
//Print some diagnostics
this.controller.fireTextStatusEvent(new TextStatusEvent(this, "Received hand."));
jfrm.repaint();
//Display the hand
//Card[] cards = ((Card[])((Vector<Card>)gameInit.getHand()).toArray());
//this.hand.displayHand(cards);
}
else if(event.getMessage() instanceof SuggestionDisprovedNeedFeedBackMessage){
System.out.println("feedback reqired");
SuggestionDisprovedNeedFeedBackMessage message = (SuggestionDisprovedNeedFeedBackMessage)event.getMessage();
PickADisprovedCardView disproveCardView = new PickADisprovedCardView(controller, message.getCards(), message.getSuggestion());
}
else if(event.getMessage() instanceof AvailableMoveMessage)
{
//highlightany available moves
//Enable make accusation / suggestion / end turn button
makeAccusation.setEnabled(true);
makeSuggestion.setEnabled(true);
endTurn.setEnabled(true);
}
else if(event.getMessage() instanceof ShowTextStatusMessage)
{
//Append status message to text dialog
if(this.statusTextPane != null)
{
this.statusTextPane.setText(statusTextPane.getText() + ((ShowTextStatusMessage)event.getMessage()).getMessageText() + "\n\n");
this.statusTextPane.repaint();
}
}
}
@Override
public void handleEndTurnEvent(EndTurnEvent event) {
makeAccusation.setEnabled(false);
makeSuggestion.setEnabled(false);
endTurn.setEnabled(false);
}
}