/* Vending Machine Program 7
 * @author Kevin Yang
 * @version Program 7
 */
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.text.NumberFormat;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class SodaMachineFrame extends JFrame{
   private JPanel east, center, west, depPanel, changePanel, salesPanel;
   private JTextField depField,changeField,salesField; 
   private JButton nickB,dimeB,quartB,halfB,dollarB,returnB;
   private NumberFormat moneyFormat;
   private SodaMachine machine;
   private Component[] westComp;
   private ArrayList<Soda> sodaList = new ArrayList<Soda>();
   public SodaMachineFrame(SodaMachine machine){
      this.setResizable(false);
      this.machine = machine;
      addPanels();
      addFields();
      addEastButtons();
      attachEast();
      attachCenter();
      attachWest(machine);
      attachPanels();                        
      pack();
   }
   
   private void addPanels(){
      east = new JPanel();
      center = new JPanel();
      west = new JPanel();
      depPanel = new JPanel();
      changePanel = new JPanel();
      salesPanel = new JPanel();
      
      east.setPreferredSize(new Dimension(150,300));
      center.setPreferredSize(new Dimension(150,300));
      west.setPreferredSize(new Dimension(150,300));
      
      east.setLayout(new GridLayout(0,1));
      center.setLayout(new GridLayout(0,1));
      west.setLayout(new GridLayout(0,1));
      
      east.setBorder(new EtchedBorder());
      center.setBorder(new EtchedBorder());
      west.setBorder(new EtchedBorder());
            
      
      depPanel.setBorder(new EtchedBorder());
      changePanel.setBorder(new EtchedBorder());
      salesPanel.setBorder(new EtchedBorder());
   }
   
   private void addFields(){
      moneyFormat = NumberFormat.getCurrencyInstance();
      
      depField = new JTextField(moneyFormat.format(0.00),6);
      changeField = new JTextField(moneyFormat.format(0.00),6);
      salesField = new JTextField(moneyFormat.format(0.00),6);
      
      depField.setHorizontalAlignment(JTextField.RIGHT);
      changeField.setHorizontalAlignment(JTextField.RIGHT);
      salesField.setHorizontalAlignment(JTextField.RIGHT);
      
      depField.setEditable(false);
      changeField.setEditable(false);
      salesField.setEditable(false);
   }
   
   private void addEastButtons(){
      nickB = new JButton("Nickel");
      dimeB = new JButton("Dime");
      quartB = new JButton("Quarter");
      halfB = new JButton("Half Dollar");
      dollarB = new JButton("Dollar");    
      nickB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             machine.deposit(5);
             depField.setText(moneyFormat.format(new Double(machine.getDeposits()*0.01)));
             changeField.setText(moneyFormat.format(new Double(0.00)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(true);
         }
      });
      
      dimeB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             machine.deposit(10);
             depField.setText(moneyFormat.format(new Double(machine.getDeposits()*0.01)));
             changeField.setText(moneyFormat.format(new Double(0.00)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(true);
         }
      });
      
      quartB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             machine.deposit(25);
             depField.setText(moneyFormat.format(new Double(machine.getDeposits()*0.01)));
             changeField.setText(moneyFormat.format(new Double(0.00)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(true);
         }
      });
      
      halfB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             machine.deposit(50);
             depField.setText(moneyFormat.format(new Double(machine.getDeposits()*0.01)));
             changeField.setText(moneyFormat.format(new Double(0.00)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(true);
         }
      });
      
      dollarB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             machine.deposit(100);
             depField.setText(moneyFormat.format(new Double(machine.getDeposits()*0.01)));
             changeField.setText(moneyFormat.format(new Double(0.00)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(true);
         }
      });
   }
   
   private void attachEast(){
      east.add(nickB);
      east.add(dimeB);
      east.add(quartB);
      east.add(halfB);
      east.add(dollarB);
   }
   
   private void attachCenter(){
      returnB = new JButton("Coin Return");
      depPanel.add(new JLabel("Amount Deposited"));
      depPanel.add(depField);
      returnB.setEnabled(false);
      returnB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             depField.setText(moneyFormat.format(new Double(0.00)));
             changeField.setText(moneyFormat.format(new Double(machine.returnDeposits()*0.01)));
             enableSoda(machine.getDeposits());
             returnB.setEnabled(false);
         }
      });
      
      depPanel.add(returnB);
      
      changePanel.add(new JLabel("Change Returned"));
      changePanel.add(changeField);
      salesPanel.add(new JLabel("Total Sales"));
      salesPanel.add(salesField);
      
      center.add(depPanel);
      center.add(changePanel);
      center.add(salesPanel);
   }
   
   private void attachWest(SodaMachine mach){
      JButton tempB;
      String name = new String();
      int count = 0;
      for(int i = 0; i < mach.getCount(); i++){
         this.sodaList.add(new Soda(mach.getSodaName(i),mach.getSodaCount(i)));
      }
      for(int i = 0; i < mach.getCount(); i++){
         name = sodaList.get(i).getName();
         count = sodaList.get(i).getCount();
         tempB = new JButton(name);
         tempB.setEnabled(false);
         tempB.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             depField.setText(moneyFormat.format(new Double(0.00)));
             returnB.setEnabled(false);
             for(int i = 0; i < machine.getCount(); i++){
               if((sodaList.get(i).getName()).equals(((JButton)e.getSource()).getText())){
                     changeField.setText(moneyFormat.format(new Double(machine.dispenseSoda(i)*0.01)));
                     sodaList.set(i, new Soda(((JButton)e.getSource()).getText(),sodaList.get(i).getCount() - 1));
                     break;
               } 
               else{
                     ((JButton)e.getSource()).setEnabled(false);
               }
             }
             enableSoda(0);
             salesField.setText(moneyFormat.format(new Double(machine.getSales()*0.01)));
             machine.returnDeposits();
         }
      });

         west.add(tempB);
         westComp = west.getComponents();
      }
   }
   
   private void attachPanels(){
      getContentPane().add(east, BorderLayout.EAST);
      getContentPane().add(center, BorderLayout.CENTER);
      getContentPane().add(west, BorderLayout.WEST);
   }
   
   private void enableSoda(int deposits){
      if(deposits >= 125){
         for(int i = 0; i < westComp.length; i++){
            if(sodaList.get(i).getCount() > 0){
               westComp[i].setEnabled(true);
            }
            else{
               westComp[i].setEnabled(false);
            }
         }
      }
      else{
         for(int i = 0; i < westComp.length; i++){
            westComp[i].setEnabled(false);
         }
      }
   }
}