package tictactoe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UserGUI implements UserInterface, ActionListener {
    
    private GameInterface game;
    private JFrame frame;
    private JPanel tttPanel;
    private boolean firstDisplay = true;
    private JLabel topLabel;
//    private JLabel bottomLabel;
    private JButton[][] buttons;
    private String waitingMsg = "  Waiting for the other player to move ...";
    
    public UserGUI(GameInterface game) {
        System.out.println("Creating new UserGUI");
        this.game = game;
        frame = new JFrame("Tic Tac Toe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tttPanel = new JPanel();
        tttPanel.setLayout(new BorderLayout());
        topLabel = new JLabel(waitingMsg);
        tttPanel.add(topLabel,BorderLayout.NORTH);
//        bottomLabel = new JLabel("This is also a test");
//        tttPanel.add(bottomLabel,BorderLayout.SOUTH);
        frame.getContentPane().add(tttPanel); // component added to content pane
        game.joinGame(this);
        //frame.pack(); // sets the frame's size to accommodate its components
        //frame.setVisible(true); // displays the frame
    }
    
    public void setId(char id){
        frame.setTitle("Tic Tac Toe for user "+id);
        if (id =='O')
            frame.setLocation(400,0);
    }
    
    public void boardChanged(char[][] board) {
        if (firstDisplay) {
            System.out.println("Setting up buttons");
            setupButtons(board.length);
            frame.setVisible(true);
            firstDisplay = false;
        }
        for (int i=0;i<buttons.length;i++)
            for (int j=0;j<buttons.length;j++)
                buttons[i][j].setText(""+board[i][j]);
        System.out.println("Must reset board");
        
    }
    
    public void makeAMove() {
       topLabel.setText("  Please make a move ...");
//        bottomLabel.setText("Please make a move");
    }
    
    public void inform(String s) {
        topLabel.setText("  " +s);
    }
    
    private void setupButtons(int size) {
        JPanel buttonPanel;
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(size,size));
        buttons = new JButton[size][size];
        for (int i=0;i<size;i++)
            for (int j=0;j<size;j++) {
                buttons[i][j] = new JButton("                          ");
                buttonPanel.add(buttons[i][j]);
                buttons[i][j].addActionListener(this);
            }
        tttPanel.add(buttonPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        System.out.println("Got a button push");
        for (int i=0;i<buttons.length;i++)
            for (int j=0;j<buttons.length;j++)
                if (e.getSource() == buttons[i][j]) {
                    System.out.println("Got button at "+i+" " + j);
                    topLabel.setText(waitingMsg);
//                    bottomLabel.setText(" ");
                    game.move(i,j,this);
                }
    }

}

