package BonusClient;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import opencard.core.service.SmartCard;
import opencard.core.service.CardRequest;
//import com.gemplus.opencard.service.platform.gp.
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.border.TitledBorder;
import java.util.Arrays;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class BonusWnd extends JFrame  {
    private static byte[] BONUS_AID = {
                         (byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x62,
                         (byte)0x03, (byte)0x01, (byte)0x0C, (byte)0x05, (byte)0x01
            };
    private static BonusCommunication BC;
    public static void main(String[] args) {
        BonusWnd bonuswnd = new BonusWnd();
    }

    private void jbInit() throws Exception {
        this.setJMenuBar(MainMenu);
        this.setResizable(false);
        this.addWindowListener(new BonusWnd_this_windowAdapter(this));
        Connection.setText("Connection");
        Connect.setText("Connect");
        Connect.addActionListener(new BonusWnd_Connect_actionAdapter(this));
        Disconnect.setText("Disconnect");
        Disconnect.addActionListener(new BonusWnd_Disconnect_actionAdapter(this));
        Manager.setLayout(null);
        Purchase.setLayout(null);
        Procedures.setText("Procedures");
        Buy.setText("Buy");
        Buy.addActionListener(new BonusWnd_Buy_actionAdapter(this));
        Credits.setText("Credits");
        Credits.addActionListener(new BonusWnd_Credits_actionAdapter(this));
        bonusLabel.setHorizontalAlignment(SwingConstants.CENTER);
        bonusLabel.setText("no");
        bonusLabel.setBounds(new Rectangle(179, 87, 28, 15));
        CreditManager.setBackground(Color.white);
        CreditManager.setFont(new java.awt.Font("Dialog", Font.PLAIN, 22));
        CreditManager.setForeground(Color.darkGray);
        CreditManager.setBorder(BorderFactory.createEtchedBorder());
        CreditManager.setHorizontalAlignment(SwingConstants.CENTER);
        CreditManager.setHorizontalTextPosition(SwingConstants.CENTER);
        CreditManager.setText("Credit Manager");
        CreditManager.setBounds(new Rectangle(121, 14, 159, 51));
        jLabel2.setText("You Have");
        jLabel2.setBounds(new Rectangle(119, 87, 53, 15));
        jLabel3.setText("Credits");
        jLabel3.setBounds(new Rectangle(220, 87, 50, 15));
        RemCredits.setBounds(new Rectangle(121, 185, 158, 23));
        RemCredits.setText("Remove All Credits");
        RemCredits.addActionListener(new BonusWnd_RemCredits_actionAdapter(this));
        SubtractCredits.setBounds(new Rectangle(260, 129, 97, 23));
        SubtractCredits.setText("Substract");
        SubtractCredits.addActionListener(new
                BonusWnd_SubtractCredits_actionAdapter(this));
        removeValue.setText("");
        removeValue.setHorizontalAlignment(SwingConstants.CENTER);
        removeValue.setBounds(new Rectangle(161, 132, 80, 20));
        jLabel4.setText("Remove Credits");
        jLabel4.setBounds(new Rectangle(48, 137, 98, 15));
        jLabel5.setBackground(Color.white);
        jLabel5.setFont(new java.awt.Font("Dialog", Font.PLAIN, 22));
        jLabel5.setBorder(BorderFactory.createEtchedBorder());
        jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);
        jLabel5.setText("Purchase");
        jLabel5.setBounds(new Rectangle(139, 14, 122, 51));
        jLabel6.setText("Product value");
        jLabel6.setBounds(new Rectangle(42, 113, 86, 15));
        buyValue.setHorizontalAlignment(SwingConstants.CENTER);
        buyValue.setBounds(new Rectangle(152, 108, 78, 20));
        BuyIt.setBounds(new Rectangle(262, 108, 71, 20));
        BuyIt.setText("Buy It");
        BuyIt.addActionListener(new BonusWnd_BuyIt_actionAdapter(this));
        Result.setHorizontalAlignment(SwingConstants.CENTER);
        Result.setBounds(new Rectangle(69, 183, 262, 28));
        PINValidationPanel.setLayout(null);
        PINField.setText("");
        PINField.setBounds(new Rectangle(138, 112, 116, 22));
        GoValidate.setBounds(new Rectangle(273, 111, 71, 23));
        GoValidate.setText("Go");
        GoValidate.addActionListener(new BonusWnd_GoValidate_actionAdapter(this));
        jLabel7.setFont(new java.awt.Font("Dialog", Font.PLAIN, 22));
        jLabel7.setBorder(BorderFactory.createEtchedBorder());
        jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel7.setText("PIN Verification");
        jLabel7.setBounds(new Rectangle(112, 15, 176, 50));
        jLabel8.setText("Insert PIN");
        jLabel8.setBounds(new Rectangle(57, 119, 62, 15));
        File.setText("File");
        LogIn.setText("Log in");
        LogIn.addActionListener(new BonusWnd_LogIn_actionAdapter(this));
        Exit.setText("Exit");
        Exit.addActionListener(new BonusWnd_Exit_actionAdapter(this));
        ChangePINPanel.setLayout(null);
        jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 22));
        jLabel1.setBorder(BorderFactory.createEtchedBorder());
        jLabel1.setPreferredSize(new Dimension(55, 15));
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("Change PIN");
        jLabel1.setBounds(new Rectangle(123, 14, 154, 49));
        oldPINField.setBounds(new Rectangle(203, 95, 116, 22));
        newPINField1.setBounds(new Rectangle(203, 128, 116, 22));
        newPINField2.setBounds(new Rectangle(203, 159, 116, 22));
        jLabel9.setText("Old PIN");
        jLabel9.setBounds(new Rectangle(78, 102, 99, 15));
        jLabel10.setText("Enter new PIN");
        jLabel10.setBounds(new Rectangle(78, 133, 99, 15));
        jLabel11.setText("Re-enter new PIN");
        jLabel11.setBounds(new Rectangle(78, 164, 99, 15));
        ChangePIN.setBounds(new Rectangle(144, 211, 112, 23));
        ChangePIN.setToolTipText("");
        ChangePIN.setText("Change");
        ChangePIN.addActionListener(new BonusWnd_jButton1_actionAdapter(this));
        ChangeIPIN.setText("Change PIN");
        ChangeIPIN.addActionListener(new BonusWnd_ChangeIPIN_actionAdapter(this));
        ResultLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
        ResultLabel.setHorizontalAlignment(SwingConstants.CENTER);
        ResultLabel.setBounds(new Rectangle(79, 182, 243, 31));
        MainMenu.add(File);
        MainMenu.add(Connection);
        MainMenu.add(Procedures);
        Connection.add(Connect);
        Connection.add(Disconnect);
        Procedures.add(Credits);
        Procedures.add(Buy);
        Procedures.add(ChangeIPIN);
        Manager.add(CreditManager, null);
        Manager.add(bonusLabel, null);
        Manager.add(jLabel3, null);
        Manager.add(jLabel2, null);
        Manager.add(SubtractCredits, null);
        Manager.add(removeValue, null);
        Manager.add(jLabel4, null);
        Manager.add(RemCredits, null);
        Purchase.add(jLabel5);
        Purchase.add(buyValue);
        Purchase.add(jLabel6);
        Purchase.add(BuyIt);
        Purchase.add(Result);
        PINValidationPanel.add(jLabel7);
        PINValidationPanel.add(GoValidate);
        PINValidationPanel.add(PINField);
        PINValidationPanel.add(jLabel8);
        PINValidationPanel.add(ResultLabel);
        File.add(LogIn);
        File.addSeparator();
        File.add(Exit);
        ChangePINPanel.add(jLabel1);
        ChangePINPanel.add(oldPINField);
        ChangePINPanel.add(jLabel9);
        ChangePINPanel.add(newPINField1);
        ChangePINPanel.add(newPINField2);
        ChangePINPanel.add(jLabel10);
        ChangePINPanel.add(jLabel11);
        ChangePINPanel.add(ChangePIN);
        Connect.setEnabled(true);
        Disconnect.setEnabled(false);
    }

    JMenuBar MainMenu = new JMenuBar();
    JMenu Connection = new JMenu();
    JMenuItem Connect = new JMenuItem();
    JMenuItem Disconnect = new JMenuItem();
    JPanel Manager = new JPanel();
    JPanel Purchase = new JPanel();
    GridBagLayout gridBagLayout2 = new GridBagLayout();
    JMenu Procedures = new JMenu();
    JMenuItem Credits = new JMenuItem();
    JMenuItem Buy = new JMenuItem();
    JLabel bonusLabel = new JLabel();
    JLabel CreditManager = new JLabel();
    TitledBorder titledBorder1 = new TitledBorder("");
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JButton RemCredits = new JButton();
    JButton SubtractCredits = new JButton();
    JTextField removeValue = new JTextField();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JTextField buyValue = new JTextField();
    JButton BuyIt = new JButton();
    JLabel Result = new JLabel();
    JPanel PINValidationPanel = new JPanel();
    JPasswordField PINField = new JPasswordField();
    JButton GoValidate = new JButton();
    JLabel jLabel7 = new JLabel();
    JLabel jLabel8 = new JLabel();
    JMenu File = new JMenu();
    JMenuItem LogIn = new JMenuItem();
    JMenuItem Exit = new JMenuItem();
    JPanel ChangePINPanel = new JPanel();
    JLabel jLabel1 = new JLabel();
    JPasswordField oldPINField = new JPasswordField();
    JPasswordField newPINField1 = new JPasswordField();
    JPasswordField newPINField2 = new JPasswordField();
    JLabel jLabel9 = new JLabel();
    JLabel jLabel10 = new JLabel();
    JLabel jLabel11 = new JLabel();
    JButton ChangePIN = new JButton();
    JMenuItem ChangeIPIN = new JMenuItem();
    JLabel ResultLabel = new JLabel();

    public BonusWnd() {
    try {
        jbInit();
        ClassLoader loadImg = getClass().getClassLoader();
        Image im = Toolkit.getDefaultToolkit().getImage(loadImg.getResource("BonusClient/6-pin.gif"));
        setIconImage(im);
        BC = new BonusCommunication();
        this.getContentPane().add(PINValidationPanel);
        this.getContentPane().validate();
        this.getContentPane().repaint();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

    public void Connect_actionPerformed(ActionEvent e) {
        try {
            BC.startFramework();
            BC.connectToCard();
            BC.selectApplet(BONUS_AID);

            Connect.setEnabled(false);
            Disconnect.setEnabled(true);
        } catch (Exception ex) {
                ex.printStackTrace();
        }
    }

    public void this_windowClosing(WindowEvent e) {
        try{
            BC.stopFramework();
        }catch (Exception ex){
        }finally{
            System.exit(0);
        }
    }

    public void Credits_actionPerformed(ActionEvent e) {
        this.getContentPane().remove(getContentPane().getComponent(0));
        this.getContentPane().add(Manager);
        this.getContentPane().validate();
        this.getContentPane().repaint();
    }

    public void Buy_actionPerformed(ActionEvent e) {
        Result.setText("");
        this.getContentPane().remove(getContentPane().getComponent(0));
        this.getContentPane().add(Purchase);
        this.getContentPane().validate();
        this.getContentPane().repaint();
    }

    public void BuyIt_actionPerformed(ActionEvent e) {
        try{
            BC.makePurchase(buyValue.getText());
            String bonus = BC.getBonus();
            Result.setText("Your bonus now is "+bonus);
            bonusLabel.setText(bonus);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,
                                          ex.getMessage(),
                                          "Warning",
                                          JOptionPane.WARNING_MESSAGE);
        }finally{
            buyValue.setText("");
        }
    }

    public void RemCredits_actionPerformed(ActionEvent e) {
        try{
            System.out.println(BC.eraseBonus());
            bonusLabel.setText("no");
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,
                                          ex.getMessage(),
                                          "Warning",
                                          JOptionPane.WARNING_MESSAGE);
        }
    }

    public void LogIn_actionPerformed(ActionEvent e) {
        ResultLabel.setText("");
        this.getContentPane().remove(getContentPane().getComponent(0));
        this.getContentPane().add(PINValidationPanel);
        this.getContentPane().validate();
        this.getContentPane().repaint();
    }

    public void Exit_actionPerformed(ActionEvent e) {
        try{
            BC.stopFramework();
        }catch (Exception ex){
        }finally{
            System.exit(0);
        }
    }

    public void SubtractCredits_actionPerformed(ActionEvent e) {
        try{
            BC.substractBonus(removeValue.getText());
            bonusLabel.setText(BC.getBonus());
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,
                                          ex.getMessage(),
                                          "Warning",
                                          JOptionPane.WARNING_MESSAGE);
        }finally{
            removeValue.setText("");
        }
    }

    public void Disconnect_actionPerformed(ActionEvent e) {
        try{
            BC.stopFramework();
        }catch (Exception ex){
        }
    }

    public void ChangeIPIN_actionPerformed(ActionEvent e) {
        this.getContentPane().remove(getContentPane().getComponent(0));
        this.getContentPane().add(ChangePINPanel);
        this.getContentPane().validate();
        this.getContentPane().repaint();
    }

    public void GoValidate_actionPerformed(ActionEvent e) {
        char[] Password = PINField.getPassword();
        try{
            if(BC.validate(Password))
                ResultLabel.setText("Authentication succeed");
            else{
                ResultLabel.setText("Authentication failed");
                return;
            }
            String getInfo = BC.getBonus();
            if(getInfo != null) bonusLabel.setText(getInfo);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,
                                          ex.getMessage(),
                                          "Warning",
                                          JOptionPane.WARNING_MESSAGE);
        }finally{
            Arrays.fill(Password,'0');
            PINField.setText("");
        }

    }

    public void ChangePIN_actionPerformed(ActionEvent e) {
        char[] newPIN = newPINField1.getPassword();
        char[] newPINconf = newPINField2.getPassword();
        if(Arrays.equals(newPIN,newPINconf)){
            char[] oldPIN = oldPINField.getPassword();
            try{
                if(BC.changePIN(oldPIN,newPIN)){
                    JOptionPane.showMessageDialog(this,
                                                  "Your PIN changed",
                                                  "Process complete",
                                                  JOptionPane.
                                                  INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(this,
                                                  "Authentication failed",
                                                  "Warning",
                                                  JOptionPane.WARNING_MESSAGE);

                }
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,
                                              ex.getMessage(),
                                              "Warning",
                                          JOptionPane.WARNING_MESSAGE);
            }finally{
                Arrays.fill(oldPIN,'0');
                newPINField1.setText("");
                newPINField2.setText("");
                oldPINField.setText("");
            }
        }else{
        JOptionPane.showMessageDialog(this,
                          "Please check your new PIN",
                          "Warning",
                      JOptionPane.WARNING_MESSAGE);
        Arrays.fill(newPIN,'0');
        Arrays.fill(newPINconf,'0');
        }
    }
}


class BonusWnd_jButton1_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_jButton1_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {

        adaptee.ChangePIN_actionPerformed(e);
    }
}


class BonusWnd_ChangeIPIN_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_ChangeIPIN_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.ChangeIPIN_actionPerformed(e);
    }
}


class BonusWnd_Disconnect_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_Disconnect_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Disconnect_actionPerformed(e);
    }
}


class BonusWnd_SubtractCredits_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_SubtractCredits_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.SubtractCredits_actionPerformed(e);
    }
}


class BonusWnd_Exit_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_Exit_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Exit_actionPerformed(e);
    }
}


class BonusWnd_LogIn_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_LogIn_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.LogIn_actionPerformed(e);
    }
}


class BonusWnd_RemCredits_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_RemCredits_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.RemCredits_actionPerformed(e);
    }
}


class BonusWnd_BuyIt_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_BuyIt_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.BuyIt_actionPerformed(e);
    }
}


class BonusWnd_Buy_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_Buy_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Buy_actionPerformed(e);
    }
}


class BonusWnd_Credits_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_Credits_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Credits_actionPerformed(e);
    }
}


class BonusWnd_this_windowAdapter extends WindowAdapter {
    private BonusWnd adaptee;
    BonusWnd_this_windowAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
    }
}


class BonusWnd_GoValidate_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_GoValidate_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.GoValidate_actionPerformed(e);
    }
}


class BonusWnd_Connect_actionAdapter implements ActionListener {
    private BonusWnd adaptee;
    BonusWnd_Connect_actionAdapter(BonusWnd adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {

        adaptee.Connect_actionPerformed(e);
    }
}
