package com.apixandru.games.rummikub.swing;

import com.apixandru.games.rummikub.brotocol.connect.WaitingRoomListener;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.EAST;
import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NORTHWEST;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 * @author Alexandru-Constantin Bledea
 * @since March 29, 2016
 */
public class WaitingRoom extends JPanel implements WaitingRoomListener {

    private final JButton btnStart = new JButton("Start");

    private final DefaultListModel<String> playerListModel = new DefaultListModel<>();

    private WaitingRoom() {
        super(new BorderLayout());

        final JList<String> list = new JList<>(playerListModel);
        list.setVisibleRowCount(6);
        list.setFixedCellWidth(150);

        add(new JScrollPane(list), CENTER);
        add(createButtonPane(), EAST);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Waiting room");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setContentPane(new WaitingRoom());

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(WaitingRoom::createAndShowGUI);
    }

    private JPanel createButtonPane() {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = NORTHWEST;
        gbc.fill = HORIZONTAL;

        gbc.weighty = 1;
        gbc.weightx = 1;
        buttonPane.add(Box.createVerticalGlue(), gbc);
        gbc.weighty = 0;
        gbc.weightx = 0;

        gbc.gridy++;
        buttonPane.add(btnStart, gbc);
        buttonPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        return buttonPane;
    }

    @Override
    public void playerJoined(final String playerName) {
        playerListModel.addElement(playerName);
    }

    @Override
    public void playerLeft(final String playerName) {
        playerListModel.removeElement(playerName);
    }

}

@@ -318,43 +318,8 @@
 ne;%0A
-import javax.swing.SwingUtilities;%0A
 impo
@@ -1289,22 +1289,21 @@
    p
-rivate
+ublic
  static 
 void
@@ -1298,20 +1298,35 @@
  static 
-void
+WaitingRoomListener
  createA
@@ -1458,17 +1458,26 @@
    f
-rame.setC
+inal WaitingRoom c
 onte
@@ -1482,17 +1482,19 @@
 tentPane
-(
+ = 
 new Wait
@@ -1502,16 +1502,58 @@
 ngRoom()
+;%0A        frame.setContentPane(contentPane
 );%0A%0A    
@@ -1606,126 +1606,37 @@
 e);%0A
-    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        SwingUtilities.invokeLater(WaitingRoom::createAndShowGUI);
+%0A        return contentPane;%0A
 %0A   

create and show gui returns the listener