import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by loomisdf on 4/10/2016.
 */
public class DatabaseFrame{

    private JFrame frame;
    private JLabel descriptionLabel;
    private JPanel panel;
    private JPanel loginScreen;
    private JButton goButton;
    private JTable table;
    private DefaultTableModel tableModel;
    private JComboBox dropdown;
    private JTabbedPane userScreen;

    public DatabaseFrame() {
        frame = new JFrame("Database Frame");
        panel = new JPanel();
        descriptionLabel = new JLabel("Welcome to the DB");

        // Various creation operations
        createDropdown();
        createButtons();
        creteTable();
        createUserScreen();
        createLoginScreen();

        // Setup JFrame
        frame.setSize(500, 550);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.WHITE);

        // Add things to panel
        panel.add(userScreen);
        panel.add(loginScreen);

        // Add panel to frame
        frame.add(panel);

        // Set the frame visible
        frame.setVisible(true);
    }

    private void createLoginScreen() {
        loginScreen = new JPanel();

        // Create Labels
        JLabel userLabel = new JLabel("Uesrname:");
        JLabel passLabel = new JLabel("Password:");

        // Create Inputs
        JTextField userNameInput = new JTextField(15);
        JPasswordField passInput = new JPasswordField(15);

        // Button Functionality
        JButton loginButton = new JButton("Login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginScreen.setVisible(false);
                userScreen.setVisible(true);
            }
        });

        loginScreen.add(userLabel);
        loginScreen.add(userNameInput);

        loginScreen.add(passLabel);
        loginScreen.add(passInput);

        loginScreen.add(loginButton);
        loginScreen.setLayout(new BoxLayout(loginScreen, BoxLayout.PAGE_AXIS));
    }

    private void createUserScreen() {
        userScreen = new JTabbedPane();
        JPanel card1 = new JPanel();

        JPanel optionPanel = new JPanel();
        optionPanel.add(descriptionLabel);
        optionPanel.add(dropdown);
        optionPanel.add(goButton);
        optionPanel.setLayout(new FlowLayout());

        card1.add(optionPanel);
        card1.add(new JScrollPane(table));

        card1.setLayout(new BoxLayout(card1, BoxLayout.Y_AXIS));

        JPanel card2 = new JPanel();
        card2.add(new JLabel("User Page"));

        userScreen.addTab("Store", card1);
        userScreen.addTab("User Page", card2);

        userScreen.setVisible(false);
    }

    private void createDropdown() {
        String[] options = {"Show All Games", "Show All Categories"};
        dropdown = new JComboBox(options);
    }

    private void creteTable() {
        table = new JTable();
        tableModel = new DefaultTableModel(0, 0);
    }

    public void replaceTable(Object rowData[][], Object columns[]) {
        tableModel.setColumnIdentifiers(columns);
        table.setModel(tableModel);

        // Remove Rows
        if (tableModel.getRowCount() > 0) {
            for (int i = tableModel.getRowCount() - 1; i > -1; i--) {
                tableModel.removeRow(i);
            }
        }

        // Add new rows
        for(int r = 0; r < rowData.length; r++) {
            tableModel.addRow(rowData[r]);
        }
    }

    private void createButtons() {
        goButton = new JButton("Go!");
        goButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String operation = (String) dropdown.getSelectedItem();
                switch(operation) {
                    case("Show All Games"): {
                        TableInfo t = new TableInfo(Game.getAllGames());
                        replaceTable(t.rowData, t.columns);
                        break;
                    }
                    case("Show All Categories") : {
                        TableInfo t = new TableInfo(Category.getAllCategories());
                        replaceTable(t.rowData, t.columns);
                        break;
                    }

                }
            }
        });
    }
}

@@ -287,18 +287,32 @@
 ionLabel
+, userGreeting
 ;%0A
-
     priv
@@ -528,16 +528,47 @@
 creen;%0A%0A
+    private User currentUser;%0A%0A
     publ
@@ -1485,16 +1485,185 @@
 word:%22);
+%0A        JLabel userNotFoundWarning = new JLabel(%22User not found!%22);%0A        userNotFoundWarning.setForeground(Color.red);%0A        userNotFoundWarning.setVisible(false);
 %0A%0A      
@@ -2040,83 +2040,410 @@
     
-loginScreen.setVisible(false);%0A                userScreen.setVisible(true);
+currentUser = User.getUserByProfileName(userNameInput.getText());%0A                if(currentUser != null) %7B%0A                    loginScreen.setVisible(false);%0A                    userGreeting.setText(%22Hello %22 + currentUser.profileName);%0A                    userScreen.setVisible(true);%0A                %7D%0A                else %7B%0A                    userNotFoundWarning.setVisible(true);%0A                %7D
 %0A   
@@ -2646,32 +2646,78 @@
 d(loginButton);%0A
+        loginScreen.add(userNotFoundWarning);%0A
         loginScr
@@ -2860,24 +2860,48 @@
 abbedPane();
+%0A%0A        // Store Panel
 %0A        JPa
@@ -3271,24 +3271,45 @@
 .Y_AXIS));%0A%0A
+        // User Page%0A
         JPan
@@ -3345,41 +3345,87 @@
     
-card2.add(new JLabel(%22User Page%22)
+userGreeting = new JLabel(%22Hello Unknown User%22);%0A        card2.add(userGreeting
 );%0A%0A

implement login screen functionality by searching for user name only