package ui;  
   
 import java.awt.EventQueue;  
 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JOptionPane;  
 import javax.swing.JTextField;  
 import domain.Customer;  
 import techserv.CustomerDA;  
 import javax.swing.JButton;  
 import java.awt.Font;  
 import java.awt.Color;  
   
 public class CustomerUI {  
   
      private JFrame frame;  
      private JTextField custIdTF;  
      private JTextField custNameTF;  
      private JTextField paytermTF;  
      private JTextField addressTF;  
      private JTextField companyTF;  
      private JButton nextBtn;  
      private JButton previousBtn;  
      private JButton addBtn;  
      private JButton updateBtn;  
      private JButton deleteBtn;  
      private CustListener custListener;  
      private JButton firstBtn;  
      private JButton lastBtn;  
      private CustomerDA custDA;  
        
      /**  
       * Launch the application.  
       */  
      public static void main(String[] args) {  
           EventQueue.invokeLater(new Runnable() {  
                public void run() {  
                     try {  
                          CustomerUI window = new CustomerUI();  
                          window.frame.setVisible(true);  
                     } catch (Exception e) {  
                          e.printStackTrace();  
                     }  
                }  
           });  
      }  
   
      /**  
       * Create the application.  
       */  
      public CustomerUI() {  
           custDA = new CustomerDA();  
           custListener = new CustListener();  
           initialize();  
           updateCustomerForm();  
             
 //          Set text fields to read mode  
           areTextFieldsEditable(false);  
      }  
   
      /**  
       * Initialize the contents of the frame.  
       * Sets all of the components and linked to an action listener that handles the control  
       */  
      private void initialize() {  
           frame = new JFrame();  
           frame.setBounds(100, 100, 570, 300);  
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
           frame.getContentPane().setLayout(null);  
             
           JLabel custIdLbl = new JLabel("Customer ID:");  
           custIdLbl.setBounds(58, 10, 76, 16);  
           frame.getContentPane().add(custIdLbl);  
             
           custIdTF = new JTextField();  
           custIdTF.setBounds(141, 7, 376, 22);  
           frame.getContentPane().add(custIdTF);  
           custIdTF.setColumns(10);  
           custIdTF.setEditable(false);  
             
           JLabel custNameLbl = new JLabel("Customer name:");  
           custNameLbl.setBounds(38, 39, 96, 16);  
           frame.getContentPane().add(custNameLbl);  
             
           custNameTF = new JTextField();  
           custNameTF.setBounds(141, 36, 376, 22);  
           custNameTF.setColumns(10);  
           frame.getContentPane().add(custNameTF);  
             
           JLabel payTermLbl = new JLabel("Payterm:");  
           payTermLbl.setBounds(82, 68, 52, 16);  
           frame.getContentPane().add(payTermLbl);  
             
           paytermTF = new JTextField();  
           paytermTF.setBounds(141, 65, 376, 22);  
           paytermTF.setColumns(10);  
           frame.getContentPane().add(paytermTF);  
             
           JLabel addressLbl = new JLabel("Address:");  
           addressLbl.setBounds(83, 97, 51, 16);  
           frame.getContentPane().add(addressLbl);  
             
           addressTF = new JTextField();  
           addressTF.setBounds(141, 94, 376, 22);  
           addressTF.setColumns(10);  
           frame.getContentPane().add(addressTF);  
             
           JLabel companyLbl = new JLabel("Company:");  
           companyLbl.setBounds(76, 126, 58, 16);  
           frame.getContentPane().add(companyLbl);  
             
           companyTF = new JTextField();  
           companyTF.setBounds(141, 123, 376, 22);  
           companyTF.setColumns(10);  
           frame.getContentPane().add(companyTF);  
             
           nextBtn = new JButton(">");  
           nextBtn.setForeground(Color.BLUE);  
           nextBtn.setBounds(289, 191, 125, 25);  
           nextBtn.addActionListener(custListener);  
           frame.getContentPane().add(nextBtn);  
             
           previousBtn = new JButton("<");  
           previousBtn.setForeground(Color.BLUE);  
           previousBtn.setBounds(152, 191, 125, 25);  
           previousBtn.addActionListener(custListener);  
           frame.getContentPane().add(previousBtn);  
             
           addBtn = new JButton("ADD");  
           addBtn.setFont(new Font("Tahoma", Font.BOLD, 13));  
           addBtn.setBounds(15, 228, 173, 25);  
           addBtn.addActionListener(custListener);  
           frame.getContentPane().add(addBtn);  
             
           updateBtn = new JButton("UPDATE");  
           updateBtn.setFont(new Font("Tahoma", Font.BOLD, 13));  
           updateBtn.setBounds(200, 228, 166, 25);  
           updateBtn.addActionListener(custListener);  
           frame.getContentPane().add(updateBtn);  
             
           deleteBtn = new JButton("DELETE");  
           deleteBtn.setFont(new Font("Tahoma", Font.BOLD, 13));  
           deleteBtn.setBounds(378, 228, 173, 25);  
           deleteBtn.addActionListener(custListener);  
           frame.getContentPane().add(deleteBtn);  
             
           firstBtn = new JButton("<<");  
           firstBtn.addActionListener(custListener);  
           firstBtn.setForeground(Color.BLUE);  
           firstBtn.setBounds(15, 191, 125, 25);  
           frame.getContentPane().add(firstBtn);  
             
           lastBtn = new JButton(">>");  
           lastBtn.addActionListener(custListener);  
           lastBtn.setForeground(Color.BLUE);  
           lastBtn.setBounds(426, 191, 125, 25);  
             
           frame.getContentPane().add(lastBtn);            
           frame.setLocationRelativeTo(null);  
           frame.setResizable(false);  
           frame.setTitle("Customer Management");  
      }  
        
 //     Updates form fields with current customer navigated  
      private void updateCustomerForm() {  
           Customer cust = custDA.getCurrentCustomer();  
             
           custIdTF.setText(cust.getCustId());  
           custNameTF.setText(cust.getCustName());  
           paytermTF.setText(cust.getPayTerm());  
           addressTF.setText(cust.getAddress());  
           companyTF.setText(cust.getCompany());  
      }  
        
 //     Sets form fields to read or edit mode  
      private void areTextFieldsEditable(boolean flag) {  
           custIdTF.setEditable(flag);  
           custNameTF.setEditable(flag);  
           paytermTF.setEditable(flag);  
           addressTF.setEditable(flag);  
           companyTF.setEditable(flag);  
      }  
        
 //     An action listener class that uses the methods from CustomerDA to give actions  
 //     in the components when users interact with it  
      class CustListener implements ActionListener {  
   
           @Override  
           public void actionPerformed(ActionEvent ev) {  
                String ac = ev.getActionCommand();  
                  
                switch(ac) {  
                case ">": nextCust(); break;  
                case "<": prevCust(); break;  
                case ">>": lastCust(); break;  
                case "<<": firstCust(); break;  
                case "ADD": setEditMode("ADD"); break;  
                case "UPDATE": setEditMode("UPDATE"); break;  
                case "SAVE ADD": addCust();   
                                     resetEditMode(); break;  
                case "SAVE UPDATE": updateCust();   
                                         resetEditMode(); break;  
                case "CANCEL": resetEditMode(); break;  
                case "DELETE": deleteCust(); break;  
                }  
           }  
   
 //          Resets form to read mode  
           private void resetEditMode() {  
                nextBtn.setText(">");  
                previousBtn.setText("<");  
                  
                areTextFieldsEditable(false);  
                addBtn.setEnabled(true);  
                updateBtn.setEnabled(true);  
                deleteBtn.setEnabled(true);  
                firstBtn.setEnabled(true);  
                lastBtn.setEnabled(true);  
                  
                updateCustomerForm();  
           }  
             
 //          Utilizes form to add or update mode  
           private void setEditMode(String mode) {                 
                areTextFieldsEditable(true);  
                  
                if(mode.equals("ADD")) clearForm();  
                else if(mode.equals("UPDATE")) custIdTF.setEditable(false);  
                       
                  
                nextBtn.setText("CANCEL");  
                previousBtn.setText("SAVE " + mode);  
                  
                firstBtn.setEnabled(false);  
                lastBtn.setEnabled(false);  
                addBtn.setEnabled(false);  
                addBtn.setEnabled(false);  
                updateBtn.setEnabled(false);  
                deleteBtn.setEnabled(false);  
           }  
             
           private void clearForm() {  
                custIdTF.setText("");  
                custNameTF.setText("");  
                paytermTF.setText("");  
                addressTF.setText("");  
                companyTF.setText("");  
           }  
   
 //          Confirmation to delete selected customer  
           private void deleteCust() {  
                int res = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete customer?", "",   
                          JOptionPane.YES_NO_OPTION);  
                  
                if(res == 0) {  
                     custDA.deleteCustomer(custIdTF.getText());  
                     updateCustomerForm();  
                }  
           }  
   
 //          Update customer record to database from form fields  
           private void updateCust() {  
                custDA.updateCustomer(custIdTF.getText(),  
                                              new Customer(  
                                              custIdTF.getText(),  
                                              custNameTF.getText(),  
                                              paytermTF.getText(),  
                                              addressTF.getText(),  
                                              companyTF.getText())  
                                              );  
                updateCustomerForm();  
           }  
   
 //          Add customer record to database from form fields  
           private void addCust() {  
                custDA.addCustomer(     new Customer(  
                                         custIdTF.getText(),  
                                         custNameTF.getText(),  
                                         paytermTF.getText(),  
                                         addressTF.getText(),  
                                         companyTF.getText())  
                                         );  
                updateCustomerForm();  
           }  
   
 //          Navigate to previous customer record  
           private void prevCust() {  
                custDA.getPreviousCustomer();  
                updateCustomerForm();  
           }  
   
 //          Navigate to next customer record  
           private void nextCust() {  
                custDA.getNextCustomer();  
                updateCustomerForm();  
           }  
             
 //          Navigate to first customer record  
           private void firstCust() {  
                custDA.getFirstCustomer();  
                updateCustomerForm();  
           }  
             
 //          Navigate to last customer record  
           private void lastCust() {  
                custDA.getLastCustomer();  
                updateCustomerForm();  
           }  
             
      }  
 }