/*
 * MyFirst solo Project Spring 2014
 * Matching Card Game
 */

package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import model.MatchingGame;
import model.MatchingStatus;

/**
 * This class displays the score of the game on the South part of the GUI.
 * 
 * @author Brian Dennis
 * @version 3-28-2014
 */
@SuppressWarnings("serial")
public class ScorePanel extends JPanel implements Observer, ActionListener {
    
    /** the dimensions of the panels that hold scoring details. */
    private static final Dimension PANEL_SIZE = new Dimension(105, 25);
    
    /** the size of most of the struts. */
    private static final int STRUT_SIZE = 10;
    
    /** the size of the deck. */
    private final int myDeckSize;
    
    /** the overall frame of the GUI. */
    private final MatchingGameGUI myFrame;
    
    /** the label for score. */
    private JLabel myTurnLabel;
    
    /** the label for matches that are made. */
    private JLabel myMatchesMadeLabel;
    
    /** the label for matches that are left. */
    private JLabel myMatchesLeftLabel;
    
    /** the label for displaying success rate in making matches. */
    private JLabel mySuccessRateLabel;
    
    /**
     * This constructor sets the variables.
     * @param theGame the model of the game
     * @param theFrame the frame of the GUI
     */
    public ScorePanel(final MatchingGame theGame, final MatchingGameGUI theFrame) {
        
        super();
        myFrame = theFrame;
        myDeckSize = theGame.getDeck().size() / 2;
        start();
    }

    /**
     * This method starts making the score panel.
     */
    private void start() {
        
        final Border border = BorderFactory.createCompoundBorder(
                                  BorderFactory.createLineBorder(Color.BLACK, 2),
                                  BorderFactory.createMatteBorder(4, 4, 4, 4, Color.BLUE));
        setLayout(new FlowLayout());
        setBackground(Color.GREEN);
        setPreferredSize(new Dimension(50, 48));

        final Box box = new Box(BoxLayout.X_AXIS);
        setBorder(border);
        box.add(restartPanel());
        box.add(Box.createHorizontalStrut(550));
        box.add(turnsExpiredPanel());
        box.add(Box.createHorizontalStrut(STRUT_SIZE));
        box.add(matchesMadePanel());
        box.add(Box.createHorizontalStrut(STRUT_SIZE));
        box.add(matchesLeftPanel());
        box.add(Box.createHorizontalStrut(STRUT_SIZE));
        box.add(successRatePanel());
        
        add(box);
        setVisible(true);
    }
    
    /**
     * The JPanel that holds the button used to restart the game.
     * 
     * @return the JPanel
     */
    private JPanel restartPanel() {
        
        final JPanel restartPanel = new JPanel(new GridLayout());
        restartPanel.setBackground(Color.WHITE);
        restartPanel.setPreferredSize(PANEL_SIZE);
        final JButton restartButton = new JButton("Click to restart");
        restartButton.setMnemonic(KeyEvent.VK_R);
        restartPanel.add(restartButton);
        final Border border = BorderFactory.createLineBorder(Color.BLUE);
        restartButton.setBorder(border);
        restartButton.addActionListener(this);
        
        return restartPanel;
    }
    /**
     * creates a JPanel for viewing the turns expired integer.
     * 
     * @return the panel for the turns expired
     */
    private JPanel turnsExpiredPanel() {
        
        final JPanel turnsExpired = new JPanel(new GridLayout());
        turnsExpired.setBackground(Color.WHITE);
        turnsExpired.setPreferredSize(PANEL_SIZE);
        myTurnLabel = new JLabel("Turns Expired: 0", JLabel.CENTER);
        turnsExpired.add(myTurnLabel);
        final Border border = BorderFactory.createLineBorder(Color.BLUE);
        turnsExpired.setBorder(border);
        
        return turnsExpired;
    }
    
    /**
     * This method creates the JPanel displaying how many matches have been made.
     * 
     * @return the JPanel
     */
    private JPanel matchesMadePanel() {
        
        final JPanel matchesMade = new JPanel(new GridLayout());
        matchesMade.setBackground(Color.WHITE);
        matchesMade.setPreferredSize(PANEL_SIZE);
        myMatchesMadeLabel = new JLabel("Matches Made: 0", JLabel.CENTER);
        matchesMade.add(myMatchesMadeLabel);
        final Border border = BorderFactory.createLineBorder(Color.BLUE);
        matchesMade.setBorder(border);
        
        return matchesMade;
    }
    
    /**
     * This method creates the JPanel displaying how many matches are left.
     * 
     * @return the JPanel
     */
    private JPanel matchesLeftPanel() {
        
        final JPanel matchesLeft = new JPanel(new GridLayout());
        matchesLeft.setBackground(Color.WHITE);
        matchesLeft.setPreferredSize(PANEL_SIZE);
        myMatchesLeftLabel = new JLabel("Matches Left: " + myDeckSize, JLabel.CENTER);
        matchesLeft.add(myMatchesLeftLabel);
        final Border border = BorderFactory.createLineBorder(Color.BLUE);
        matchesLeft.setBorder(border);
        
        return matchesLeft;
    }
    
    /**
     * This method creates the JPanel displaying the success rate of making matches.
     * 
     * @return the JPanel
     */
    private JPanel successRatePanel() {
        
        final JPanel successRate = new JPanel(new GridLayout());
        successRate.setBackground(Color.WHITE);
        successRate.setPreferredSize(new Dimension(135, 25));
        mySuccessRateLabel = new JLabel("Success Rate: 0%", JLabel.CENTER);
        successRate.add(mySuccessRateLabel);
        final Border border = BorderFactory.createLineBorder(Color.BLUE);
        successRate.setBorder(border);
        
        return successRate;
    }
    
    @Override
    public void update(final Observable theMatchingGame, final Object arg1) {
        
        final MatchingStatus status = ((MatchingGame) theMatchingGame).getStatus();
        
        final double matchesMade = status.getMatchesMade();
        final double turnsTaken = status.getTurnsTaken();
        final double percentage = matchesMade / turnsTaken;
        
        myTurnLabel.setText("Turns Expired: " + status.getTurnsTaken());
        myMatchesMadeLabel.setText("Matches Made: " + status.getMatchesMade());
        myMatchesLeftLabel.setText("Matches Left: " + status.getMatchesLeft());
        mySuccessRateLabel.setText("Success Rate: " 
                                      + MessageFormat.format("{0,number,#.##%}", percentage));
    }

    @Override
    public void actionPerformed(final ActionEvent arg0) {
     

        final List<String> noArgs = new ArrayList<String>();
        myFrame.dispose();
        MatchingGameMain.changeValues(noArgs);
        start();
    }
}