package shared.view.desktop;

import controller.client.BookCatalog;
import controller.client.LoginController;
import shared.model.vo.Book;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by niralittle on 28.10.2014.
 */
public abstract class MainWindow {
    private static JFrame view;
    private static String USER;
    private static ActionListener loginListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String username = userText.getText();
            String password = new String(passwordText.getPassword());
            if (LoginController.verifyLoginPassword(username, password)) {
                placeCatalogComponents();
                USER = username;
            } else {
                JOptionPane.showMessageDialog(view,
                        (username.isEmpty() && password.isEmpty())
                        ? "Fill all the blanks to proceed"
                        : "Unsuccessful, please check credentials",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
            }        }
    };



    private static JTextField userText;
    private static JPasswordField passwordText;

    public static void main(String[] args) {
        placeLoginComponents();
    }

    private static void placeLoginComponents() {
        view = new JFrame("Log in to Library");
        view.setSize(300, 160);
        view.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        view.add(panel);


        panel.setLayout(null);

        JLabel userLabel = new JLabel("User");
        userLabel.setBounds(10, 10, 80, 25);
        panel.add(userLabel);

        userText = new JTextField(20);
        userText.setBounds(100, 10, 160, 25);
        panel.add(userText);

        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10, 40, 80, 25);
        panel.add(passwordLabel);

        passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 40, 160, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("login");
        loginButton.setBounds(10, 80, 80, 25);
        loginButton.addActionListener(loginListener);
        panel.add(loginButton);
        view.setVisible(true);
    }

    private static void placeCatalogComponents() {
        final List<Book> books = BookCatalog.getAllBooks();

        view.setVisible(false);
        view.getContentPane().removeAll();
        view.setSize(300, 500);

        JPanel panel = new JPanel();
        view.add(panel);
        view.setTitle("Book Catalog");

        panel.setLayout(new BorderLayout());

        String[] listData = new String[books.size()];
        for (int i = 0; i < listData.length; i++) {
            listData[i] = books.get(i).getTitle() + " (" + books.get(i).getAuthors() + ")";
        }
        final JList<String> list = new JList<>(listData);

        JScrollPane listPane = new JScrollPane(list);

        JPanel topHalf = new JPanel();
        topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
        JPanel listContainer = new JPanel(new GridLayout(1,1));
        listContainer.setBorder(BorderFactory.createTitledBorder("List"));
        listContainer.add(listPane);

        topHalf.setBorder(BorderFactory.createEmptyBorder(5,5,0,5));
        topHalf.add(listContainer);

        topHalf.setPreferredSize(new Dimension(100, 430));
        view.add(topHalf, BorderLayout.NORTH);
        JButton sendButton = new JButton("Send order for processing");
        sendButton.setBounds(10, 80, 80, 25);
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Book> orderedBooks = new ArrayList<>();
                if (orderedBooks.isEmpty()) {
                    JOptionPane.showMessageDialog(view,
                            "Select at least one book.",
                            "Warning",
                            JOptionPane.WARNING_MESSAGE);
                } else {
                    for (int index : list.getSelectedIndices()) {
                        orderedBooks.add(books.get(index));
                    }
                    BookCatalog.createOrder(orderedBooks, USER);
                    JOptionPane.showMessageDialog(view,
                            "Order created.",
                            "Order",
                            JOptionPane.INFORMATION_MESSAGE);

                }
            }
        });
        panel.add(sendButton);
        JPanel bottomHalf = new JPanel(new BorderLayout());
        bottomHalf.setPreferredSize(new Dimension(450, 135));

        view.setVisible(true);

        JOptionPane.showMessageDialog(view,
                "Welcome to Library app.",
                "Welcome",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

@@ -2741,25 +2741,25 @@
 iew.setSize(
-3
+4
 00, 500);%0A%0A 
@@ -3099,16 +3099,82 @@
 rs() + %22
+, %22%0A                    + books.get(i).getNumberOfPages()+ %22 pages
 )%22;%0A    
@@ -3846,19 +3846,18 @@
 d order 
-for
+to
  process
@@ -4068,95 +4068,49 @@
     
-List%3CBook%3E orderedBooks = new ArrayList%3C%3E();%0A                if (orderedBooks.isEmpty()
+if (list.getSelectedIndices().length == 0
 ) %7B%0A
@@ -4311,32 +4311,32 @@
 RNING_MESSAGE);%0A
-
                 
@@ -4336,32 +4336,97 @@
        %7D else %7B%0A
+                    List%3CBook%3E orderedBooks = new ArrayList%3C%3E();%0A
                 

order processing fixed