package zustand;

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import ssoe.*;
import java.awt.event.*;


public class MainWindow extends JFrame {

	/** create grafic objects */
  GridLayout gridLayout1 = new GridLayout();
  JPanel jPanel1 = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JPanel jPanel4 = new JPanel();
  GridLayout gridLayout2 = new GridLayout();
  JTextField card1_text_field = new JTextField();
  JTextField card3_text_field = new JTextField();
  JTextField card2_text_field = new JTextField();
  JButton reset_button = new JButton();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  GridLayout gridLayout3 = new GridLayout();
  GridLayout gridLayout4 = new GridLayout();
  JRadioButton jRadioButton1 = new JRadioButton();
  JRadioButton jRadioButton2 = new JRadioButton();
  JRadioButton jRadioButton3 = new JRadioButton();
  JButton insert_button = new JButton();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  GridLayout gridLayout5 = new GridLayout();
  JLabel jLabel1 = new JLabel();
  JTextField machine_text_field = new JTextField();
  JPanel jPanel7 = new JPanel();
  JPanel jPanel8 = new JPanel();
  GridLayout gridLayout6 = new GridLayout();
  GridLayout gridLayout7 = new GridLayout();
  JButton add_2_button = new JButton();
  JButton add_10_button = new JButton();
  JButton add_5_button = new JButton();
  JButton add_20_button = new JButton();
  JButton finish_button = new JButton();
  JButton abort_button = new JButton();
  ButtonGroup group = new ButtonGroup();


	/** init machine */
  Machine mach = new Machine();
  Card c1 = new Card(700);
  Card c2 = new Card(1895);
  Card c3 = new Card(2200);


  public MainWindow() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
 
 	/** layout grafic objects */
  private void jbInit() throws Exception {
    gridLayout1.setColumns(4);
    gridLayout1.setHgap(25);
    gridLayout1.setVgap(25);
    this.getContentPane().setLayout(gridLayout1);
    jPanel1.setLayout(gridLayout2);
    card1_text_field.setText("jTextField1");
    card3_text_field.setText("jTextField2");
    card2_text_field.setText("jTextField3");
    reset_button.setText("Reset Cards");
    reset_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        reset_button_actionPerformed(e);
      }
    });
    gridLayout2.setRows(4);
    gridLayout2.setColumns(1);
    gridLayout2.setHgap(50);
    gridLayout2.setVgap(50);
    jPanel2.setLayout(gridLayout3);
    gridLayout3.setRows(2);
    gridLayout3.setColumns(1);
    jPanel5.setLayout(gridLayout4);
    gridLayout4.setRows(3);
    gridLayout4.setColumns(1);
    jRadioButton1.setText("Select Card 1");
    jRadioButton2.setText("Select Card 2");
    jRadioButton3.setText("Select Card 3");
    jPanel3.setLayout(gridBagLayout1);
    insert_button.setText("Insert");
    insert_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        insert_button_actionPerformed(e);
      }
    });
    jPanel4.setLayout(gridLayout5);
    gridLayout5.setRows(4);
    gridLayout5.setColumns(1);
    gridLayout5.setVgap(15);
    jLabel1.setText("The Machine");
    machine_text_field.setPreferredSize(new Dimension(30, 21));
    machine_text_field.setText("--,--");
    jPanel7.setLayout(gridLayout6);
    gridLayout6.setRows(2);
    gridLayout6.setColumns(2);
    gridLayout6.setHgap(25);
    gridLayout6.setVgap(25);
    jPanel8.setLayout(gridLayout7);
    gridLayout7.setRows(2);
    gridLayout7.setColumns(1);
    gridLayout7.setVgap(25);
    add_2_button.setText("+2");
    add_2_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        add_2_button_actionPerformed(e);
      }
    });
    add_10_button.setText("+10");
    add_10_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        add_10_button_actionPerformed(e);
      }
    });
    add_5_button.setText("+5");
    add_5_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        add_5_button_actionPerformed(e);
      }
    });
    add_20_button.setToolTipText("");
    add_20_button.setText("+20");
    add_20_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        add_20_button_actionPerformed(e);
      }
    });
    finish_button.setText("Finish");
    finish_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        finish_button_actionPerformed(e);
      }
    });
    abort_button.setText("abort");
    abort_button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        abort_button_actionPerformed(e);
      }
    });
    this.getContentPane().add(jPanel1, null);
    jPanel1.add(card1_text_field, null);
    jPanel1.add(card2_text_field, null);
    jPanel1.add(card3_text_field, null);
    jPanel1.add(reset_button, null);
    this.getContentPane().add(jPanel2, null);
    jPanel2.add(jPanel5, null);
    jPanel5.add(jRadioButton1, null);
    jPanel5.add(jRadioButton2, null);
    jPanel5.add(jRadioButton3, null);
    jPanel2.add(jPanel6, null);
    this.getContentPane().add(jPanel3, null);
    jPanel3.add(insert_button, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 2, 11));
    this.getContentPane().add(jPanel4, null);
    jPanel4.add(jLabel1, null);
    jPanel4.add(machine_text_field, null);
    jPanel4.add(jPanel7, null);
    jPanel7.add(add_2_button, null);
    jPanel7.add(add_5_button, null);
    jPanel7.add(add_10_button, null);
    jPanel7.add(add_20_button, null);
    jPanel4.add(jPanel8, null);
    jPanel8.add(finish_button, null);
    jPanel8.add(abort_button, null);
    group.add(jRadioButton1);
    group.add(jRadioButton2);
    group.add(jRadioButton3);
    jRadioButton1.setSelected(true);
    this.refresh();
    this.setSize(650,400);
	}

  private void refresh(){
    Card c = mach.get_card();
    String s;
    if(c==null){s="--";}
      else {s=String.valueOf(mach.money()+c.get_money());};
    machine_text_field.setText(s);
    card1_text_field.setText(String.valueOf(c1.get_money()));
    card2_text_field.setText(String.valueOf(c2.get_money()));
    card3_text_field.setText(String.valueOf(c3.get_money()));
  }

  private void reset_cards(){
    c1.set_money(700);
    c2.set_money(1895);
    c3.set_money(2200);
  }//ende reset_card

  private void card_ejected(){
    insert_button.setEnabled(true);
    reset_button.setEnabled(true);
    this.refresh();
  }

  private void card_inserted(){
    insert_button.setEnabled(false);
    reset_button.setEnabled(false);
    this.refresh();
  }

  private void add_money(int m){
    mach.add_money(m);
    this.refresh();
  }


  void reset_button_actionPerformed(ActionEvent e) {
    this.reset_cards();
    this.refresh();
  }

  void insert_button_actionPerformed(ActionEvent e) {
    Card card;
    if(jRadioButton1.isSelected()){card=c1;}
    else if(jRadioButton2.isSelected()){card=c2;}
          else card =c3;
    mach.insert_card(card);
    if(mach.get_card()!=null){this.card_inserted();};
  }

  void finish_button_actionPerformed(ActionEvent e) {
    mach.finish();
    this.card_ejected();
  }

  void abort_button_actionPerformed(ActionEvent e) {
    mach.abort();
    this.card_ejected();
  }

  void add_2_button_actionPerformed(ActionEvent e) {
    this.add_money(200);
  }

  void add_5_button_actionPerformed(ActionEvent e) {
    this.add_money(500);
  }

  void add_10_button_actionPerformed(ActionEvent e) {
    this.add_money(1000);
  }

  void add_20_button_actionPerformed(ActionEvent e) {
    this.add_money(2000);
  }
 
}