/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Lists.java
 *
 * Created on 2009.11.26., 18:40:21
 */
package gui;

import connectors.TableGenerator;
import connectors.VisibleException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import mobirentdesktop.entities.Customer;
import mobirentdesktop.entities.DVD;
import mobirentdesktop.entities.Order;
import mobirentdesktop.entities.Transporter;
import peaconnector.RemoteException;

/**
 *
 * @author Budai Attila
 */
public class Lists extends javax.swing.JFrame {

    private static Lists instance;
    private ArrayList<?> list;
    private String torol = "";
    private String addd = "";

    /** Creates new form Lists */
    private Lists() {
        initComponents();
        this.set.setVisible(false);
        this.add.setVisible(false);
        this.modify.setVisible(false);
        this.delete.setVisible(false);

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        getOrderListButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        ugyfelButton = new javax.swing.JButton();
        dvdListButton = new javax.swing.JButton();
        futar = new javax.swing.JButton();
        exit = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        modify = new javax.swing.JButton();
        delete = new javax.swing.JButton();
        add = new javax.swing.JButton();
        set = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MobiRent - Adminisztrátor");

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(table);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        getOrderListButton.setText("Rendelési Lista");
        getOrderListButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getOrderListButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(getOrderListButton);

        jButton2.setText("Teljesített megrendelések");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        ugyfelButton.setText("Ügyfelek");
        ugyfelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ugyfelButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(ugyfelButton);

        dvdListButton.setText("DVD lista");
        dvdListButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dvdListButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(dvdListButton);

        futar.setText("Futárok");
        futar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                futarActionPerformed(evt);
            }
        });
        jToolBar1.add(futar);

        exit.setText("Kilépés");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        jToolBar1.add(exit);

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        modify.setText("Módosít");
        modify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyActionPerformed(evt);
            }
        });
        jToolBar2.add(modify);

        delete.setText("Töröl");
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });
        jToolBar2.add(delete);

        add.setText("Hozzáad");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });
        jToolBar2.add(add);

        set.setText("Visszahív");
        set.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setActionPerformed(evt);
            }
        });
        jToolBar2.add(set);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void getOrderListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getOrderListButtonActionPerformed
        table.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {},
                    {},
                    {},
                    {}
                },
                new String[]{}));
        try {
            this.set.setVisible(true);
            this.add.setVisible(false);
            this.modify.setVisible(false);
            this.delete.setVisible(false);

            list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getOrderList());
        } catch (RemoteException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        } catch (VisibleException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_getOrderListButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        table.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {},
                    {},
                    {},
                    {}
                },
                new String[]{}));
        try {
            this.set.setVisible(false);
            this.add.setVisible(false);
            this.modify.setVisible(false);
            this.delete.setVisible(false);
            list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getDeliveredOrders());
        } catch (RemoteException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        } catch (VisibleException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void ugyfelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ugyfelButtonActionPerformed
        torol = "ugyfelList";
        table.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {},
                    {},
                    {},
                    {}
                },
                new String[]{}));
        try {
            this.set.setVisible(false);
            this.add.setVisible(false);
            this.modify.setVisible(false);
            this.delete.setVisible(true);
            list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getCustomerList());
        } catch (RemoteException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        } catch (VisibleException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ugyfelButtonActionPerformed

    private void dvdListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dvdListButtonActionPerformed
        torol = "dvdList";
        addd = "dvd";
        table.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {},
                    {},
                    {},
                    {}
                },
                new String[]{}));
        try {
            this.set.setVisible(false);
            this.add.setVisible(true);
            this.modify.setVisible(true);
            this.delete.setVisible(true);
            list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getDvdList());
        } catch (RemoteException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        } catch (VisibleException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_dvdListButtonActionPerformed

    public static Lists getInstance() {
        if (instance == null) {
            instance = new Lists();
        }

        return instance;
    }

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        try {
            Connector.getINSTANCE().getPc().endSession();
            System.exit(0);
        } catch (RemoteException ex) {
            System.out.println(ex.getMessage());
            System.exit(-1);
        }
    }//GEN-LAST:event_exitActionPerformed

    private void modifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyActionPerformed
        AddDvdForm adf = new AddDvdForm();
        this.setGlassed(true);
        if (table.getSelectedRow() != -1) {
            adf.setFelirat("Dvd Módosítása");
            adf.update((DVD) list.get(table.getSelectedRow()));
        } else {
            System.out.println("vmi");
            new ExceptionHandlerDialog(this, true, "Nincs kiválasztva elem.");
            // Lists.getInstance().setGlassed(true);
            return;
        }
        AddDvdForm.modify = true;
        adf.setVisible(true);

    }//GEN-LAST:event_modifyActionPerformed

    public ArrayList<?> getList() {
        return list;
    }

    public void setList(ArrayList<?> list) {
        this.list = list;
    }

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        this.setGlassed(true);
        if (addd.equals("dvd")) {
            AddDvdForm adf = new AddDvdForm();
            AddDvdForm.modify = false;
            adf.setFelirat("DVD Hozzáadása");
            adf.setVisible(true);
        } else {
            AddTransporter at = new AddTransporter();
            at.setVisible(true);
        }
    }//GEN-LAST:event_addActionPerformed

    private void setActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setActionPerformed
        if (table.getSelectedRow() != -1) {
            try {
                Connector.getINSTANCE().getDfs().setOrderNeed((Order) list.get(table.getSelectedRow()));
                list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getOrderList());
            } catch (VisibleException ex) {
                Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                new ExceptionHandlerDialog(this, true, ex.getMessage());
            }
        }
    }//GEN-LAST:event_setActionPerformed
    public void setGlassed(boolean b) {
        if (b) {
            this.getRootPane().setGlassPane(new MobirentGlassPane());
            this.getRootPane().getGlassPane().setVisible(true);
        } else {
            this.getRootPane().getGlassPane().setVisible(false);
        }
    }

    public JTable getTable() {
        return table;
    }

    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        try {
            if (torol.equals("dvdList")) {
                Connector.getINSTANCE().getDfs().removeDvd((DVD) list.get(table.getSelectedRow()));
                list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getDvdList());
            } else if (torol.equals("futarList")) {
                Connector.getINSTANCE().getDfs().delTransporter((Transporter) list.get(table.getSelectedRow()));
                list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getTransporterList());
            } else if (torol.equals("ugyfelList")) {
                Connector.getINSTANCE().getDfs().removeCustomer((Customer) list.get(table.getSelectedRow()));
                list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getCustomerList());
            }

        } catch (VisibleException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        }


    }//GEN-LAST:event_deleteActionPerformed

    private void futarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_futarActionPerformed
        torol = "futarList";
        addd = " ";
        table.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {},
                    {},
                    {},
                    {}
                },
                new String[]{}));
        try {
            this.set.setVisible(false);
            this.add.setVisible(true);
            this.modify.setVisible(false);
            this.delete.setVisible(true);
            list = TableGenerator.buildToThis(table, Connector.getINSTANCE().getDfs().getTransporterList());
        } catch (RemoteException ex) {
            Logger.getLogger(Lists.class.getName()).log(Level.SEVERE, null, ex);
        } catch (VisibleException ex) {
            new ExceptionHandlerDialog(this, true, ex.getMessage());
        }
    }//GEN-LAST:event_futarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JButton delete;
    private javax.swing.JButton dvdListButton;
    private javax.swing.JButton exit;
    private javax.swing.JButton futar;
    private javax.swing.JButton getOrderListButton;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JButton modify;
    private javax.swing.JButton set;
    private javax.swing.JTable table;
    private javax.swing.JButton ugyfelButton;
    // End of variables declaration//GEN-END:variables
}
