package com.apixandru.games.rummikub.swing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.Socket;
import java.util.prefs.Preferences;

import static javax.swing.JOptionPane.OK_CANCEL_OPTION;
import static javax.swing.JOptionPane.QUESTION_MESSAGE;

/**
 * @author Alexandru-Constantin Bledea
 * @since January 09, 2016
 */
final class ServerData {

    private static final Logger log = LoggerFactory.getLogger(ServerData.class);

    private static final String KEY_ADDRESS = "address";
    private static final String KEY_USERNAME = "username";

    private static final String OPT_CONNECT = "Connect";
    private static final String OPT_CANCEL = "Cancel";

    private static final String[] OPTIONS = {OPT_CONNECT, OPT_CANCEL};

    private static final int CHOICE_CONNECT = 0;

    /**
     *
     */
    private ServerData() {
    }

    /**
     * @return
     */
    public static ConnectionData getConnectionData() {
        final Preferences prefs = Preferences.userNodeForPackage(ServerData.class);

        final JTextField tfUsername = newField(prefs, KEY_USERNAME);
        final JTextField tfAddress = newField(prefs, KEY_ADDRESS);

        while (true) {
            final int option = JOptionPane.showOptionDialog(
                    null,
                    createLoginPanel(tfUsername, tfAddress),
                    "Connection Data",
                    OK_CANCEL_OPTION,
                    QUESTION_MESSAGE,
                    null,
                    OPTIONS,
                    OPT_CONNECT);

            if (CHOICE_CONNECT != option) {
                return null;
            }

            final String address = tfAddress.getText();
            final String username = tfUsername.getText();

            prefs.put(KEY_ADDRESS, address);
            prefs.put(KEY_USERNAME, username);

            try {
                return new ConnectionData(new Socket(address, 50122), username);
            } catch (IOException ex) {
                log.debug("Could not connect to " + address);
                JOptionPane.showMessageDialog(null,
                        "Cannot connect to " + address + "!",
                        "Cannot connect",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    /**
     * @param username
     * @param address
     * @return
     */
    private static JPanel createLoginPanel(final JTextField username, final JTextField address) {
        final JPanel panel = new JPanel(new BorderLayout(5, 5));
        panel.add(createLabels(), BorderLayout.WEST);
        panel.add(newPanel(address, username), BorderLayout.CENTER);
        return panel;
    }

    /**
     * @param components
     * @return
     */
    private static JPanel newPanel(final Component... components) {
        final JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
        for (Component component : components) {
            controls.add(component);
        }
        return controls;
    }

    /**
     * @param prefs
     * @param key
     * @return
     */
    private static JTextField newField(final Preferences prefs, final String key) {
        return new JTextField(prefs.get(key, ""));
    }

    /**
     * @return
     */
    private static JPanel createLabels() {
        return newPanel(new JLabel("Server", SwingConstants.RIGHT), new JLabel("Name", SwingConstants.RIGHT));
    }

    /**
     *
     */
    static final class ConnectionData {

        final Socket socket;
        final String username;

        /**
         * @param socket
         * @param username
         */
        ConnectionData(final Socket socket, final String username) {
            this.socket = socket;
            this.username = username;
        }

    }

}

@@ -1097,24 +1097,141 @@
 ta.class);%0A%0A
+        String address = System.getProperty(%22rk.address%22);%0A        String username = System.getProperty(%22rk.user%22);%0A%0A
         fina
@@ -1284,16 +1284,26 @@
 USERNAME
+, username
 );%0A     
@@ -1361,16 +1361,25 @@
 _ADDRESS
+, address
 );%0A%0A    
@@ -1386,23 +1386,124 @@
     
-while (true) %7B%0A
+boolean showDialog = null == address %7C%7C null == username;%0A%0A        while (true) %7B%0A            if (showDialog) %7B%0A    
     
@@ -1559,16 +1559,20 @@
 Dialog(%0A
+    
         
@@ -1609,16 +1609,20 @@
         
+    
 createLo
@@ -1674,16 +1674,20 @@
         
+ 
+   
 %22Connect
@@ -1717,16 +1717,20 @@
         
+    
 OK_CANCE
@@ -1739,16 +1739,20 @@
 OPTION,%0A
+    
         
@@ -1793,38 +1793,46 @@
                 
+    
 null,%0A
+    
                 
@@ -1864,16 +1864,20 @@
         
+  
+  
 OPT_CONN
@@ -1879,24 +1879,28 @@
 _CONNECT);%0A%0A
+    
             
@@ -1927,24 +1927,28 @@
 = option) %7B%0A
+    
             
@@ -1972,24 +1972,28 @@
             
+    
 %7D%0A%0A         
@@ -1995,28 +1995,19 @@
         
-final String
+   
  address
@@ -2038,36 +2038,27 @@
             
-final String
+   
  username = 
@@ -2084,32 +2084,36 @@
 );%0A%0A            
+ 
+   
 prefs.put(KEY_AD
@@ -2121,32 +2121,36 @@
 RESS, address);%0A
+    
             pref
@@ -2180,16 +2180,29 @@
 rname);%0A
+            %7D
 %0A       
@@ -2328,24 +2328,59 @@
 ption ex) %7B%0A
+                showDialog = true;%0A
             
@@ -3429,16 +3429,39 @@
 ram key%0A
+     * @param override%0A
      * @
@@ -3555,16 +3555,39 @@
 ring key
+, final String override
 ) %7B%0A    
@@ -3608,24 +3608,43 @@
  JTextField(
+null == override ? 
 prefs.get(ke
@@ -3649,16 +3649,27 @@
 key, %22%22)
+ : override
 );%0A    %7D

allow bypassing the dialog screen